-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventory.java
More file actions
274 lines (259 loc) · 6.02 KB
/
Inventory.java
File metadata and controls
274 lines (259 loc) · 6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.event.*;
public class Inventory extends JFrame implements ActionListener,ListSelectionListener
{
JLabel l1,l2,l3,l4,l5,l6;
JTextField t1,t3,t4,t5;
JButton b1,b2,b3,b4,b5;
JPanel p1,p2,p3,p4;
JList<String> lt1;
JList<Object>lt2;
JTextArea ta;
Container c;
Connection cnn=null;
PreparedStatement ps;
String descr;
int item_code,qty_onhand,reord_lvl;float unit_price;
String cnstring,sqli,sqlu,sqld,sql;
void connect()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
cnstring="jdbc:mysql://localhost/invent";
cnn=DriverManager.getConnection(cnstring,"jav160","mice");
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Error in connection"+e.toString(),"Message",JOptionPane.INFORMATION_MESSAGE);
}
}
void listControl()
{
try
{
connect();
sql="select item_code from invent_mast";
ps=cnn.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
Vector<Object> row=new Vector<Object>();
while(rs.next())
{
row.addElement(rs.getObject(1));
}
rs.close();
ps.close();
cnn.close();
lt2.setListData(row) ;
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null,"Error in loading values to the list"+ex.toString(),"Message",JOptionPane.INFORMATION_MESSAGE);
}
}
public static void main(String args[])
{
Inventory a1=new Inventory();
}
public Inventory()
{
setTitle("Data Entry Inventory Master");
c=getContentPane();
c.setLayout(new BoxLayout(c,BoxLayout.Y_AXIS));
p1=new JPanel();
p1.setLayout(new GridLayout(5,2,5,5));
l1=new JLabel("Item Code");
l2=new JLabel("Description");
l3=new JLabel("Quantity On Hand");
l4=new JLabel("Unit Price");
l5=new JLabel("Reorder Level");
t1=new JTextField(20);
t3=new JTextField(20);
t3.addActionListener(this);
t4=new JTextField(20);
t5=new JTextField(20);
t5.setEditable(false); // no value can be typed inside that text box
lt1=new JList<String>(new String[]{"Pen","Pencil","Eraser"});
p1.add(l1);
p1.add(t1);
p1.add(l2);
p1.add(new JScrollPane(lt1));
p1.add(l3);
p1.add(t3);
p1.add(l4);
p1.add(t4);
p1.add(l5);
p1.add(t5);
c.add(p1);
p2=new JPanel();
p2.setLayout(new GridLayout(1,3,5,5));
b1=new JButton("Insert");
b2=new JButton("Update");
b3=new JButton("Delete");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
p2.add(b1);
p2.add(b2);
p2.add(b3);
c.add(p2);
p3=new JPanel();
p3.setLayout(new GridLayout(1,2,5,5));
b4=new JButton("Clear");
b5=new JButton("Close");
b4.addActionListener(this);
b5.addActionListener(this);
p3.add(b4);
p3.add(b5);
c.add(p3);
p4=new JPanel();
p4.setLayout(new BorderLayout());
l6=new JLabel("Selected item Code");
lt2=new JList<Object>();
listControl();
lt2.addListSelectionListener(this);
p4.add(l6,"North");
p4.add(new JScrollPane(lt2),"Center");
c.add(p4);
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void valueChanged(ListSelectionEvent ex)
{
try{
connect();
int item=Integer.parseInt(lt2.getSelectedValue().toString());
sql="select* from invent_mast where item_code=?";
ps=cnn.prepareStatement(sql);
ps.setInt(1,item);
ResultSet rs=ps.executeQuery();
if(rs.next())
{
t1.setText(rs.getString(1));
lt1.setSelectedValue(rs.getObject(2),true);
t3.setText(rs.getString(3));
t4.setText(rs.getString(4));
t5.setText(rs.getString(5));
}
rs.close();
ps.close();
cnn.close();
}
catch(Exception e1)
{
JOptionPane.showMessageDialog(null,"Error in selecting values from the list"+e1.toString(),"Message",JOptionPane.INFORMATION_MESSAGE);
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
insert();
if(e.getSource()==b2)
update();
if(e.getSource()==b3)
delete();
if(e.getSource()==b4)
clear();
if(e.getSource()==b5)
close();
if(e.getSource()==t3)
checkQty();
}
void checkQty()
{
int q=Integer.parseInt(t3.getText());
if(q<100)
{
JOptionPane.showMessageDialog(null,"Quantity on hand must be greater than 100","Message",JOptionPane.INFORMATION_MESSAGE);
t3.setText("");
t3.requestFocus(); //puts cursor inside the text box(t3)
}
else
{
int r=q/2;
t5.setText(String.valueOf(r));
t4.requestFocus();
}
}
void insert()
{
try
{
connect();
item_code=Integer.parseInt(t1.getText());
descr=lt1.getSelectedValue().toString();
qty_onhand=Integer.parseInt(t3.getText());
unit_price=Float.parseFloat(t4.getText());
reord_lvl=Integer.parseInt(t5.getText());
sqli="insert into invent_mast(item_code,descr,qty_onhand,unit_price,reord_lvl)Values(?,?,?,?,?)";
ps=cnn.prepareStatement(sqli);
ps.setInt(1,item_code); ps.setString(2,descr);ps.setInt(3,qty_onhand);ps.setFloat(4,unit_price);ps.setInt(5,reord_lvl);
int tot=ps.executeUpdate();
JOptionPane.showMessageDialog(null,"Row inserted"+tot);
cnn.close();
listControl();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Error in connection"+e.toString(),"Message",JOptionPane.INFORMATION_MESSAGE);
}
}
void update()
{
try
{
connect();
item_code=Integer.parseInt(t1.getText());
descr=lt1.getSelectedValue().toString();
qty_onhand=Integer.parseInt(t3.getText());
unit_price=Float.parseFloat(t4.getText());
reord_lvl=Integer.parseInt(t5.getText());
sqlu="update invent_mast set descr=?,qty_onhand=?,unit_price=?,reord_lvl=? where item_code=?";
ps=cnn.prepareStatement(sqlu);
ps.setString(1,descr);ps.setInt(2,qty_onhand);ps.setFloat(3,unit_price);ps.setInt(4,reord_lvl);ps.setInt(5,item_code);
int tot=ps.executeUpdate();
JOptionPane.showMessageDialog(null,"Row updated"+tot);
cnn.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Error in connection"+e.toString(),"Message",JOptionPane.INFORMATION_MESSAGE);
}
}
void delete()
{
try
{
connect();
item_code=Integer.parseInt(t1.getText());
sqld="delete from invent_mast where item_code=?";
ps=cnn.prepareStatement(sqld);
ps.setInt(1,item_code);
int tot=ps.executeUpdate();
JOptionPane.showMessageDialog(null,"Row deleted"+tot);
cnn.close();
listControl();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Error in connection"+e.toString(),"Message",JOptionPane.INFORMATION_MESSAGE);
}
}
void clear()
{
t1.setText("");
t3.setText("");
t4.setText("");
t5.setText("");
lt1.clearSelection();
}
void close()
{
setVisible(false);
}
}