-
Notifications
You must be signed in to change notification settings - Fork 1
/
UpdateSupplier.java
259 lines (231 loc) · 7.94 KB
/
UpdateSupplier.java
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
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.table.DefaultTableModel;
import java.sql.*;
import java.sql.ResultSet;
public class UpdateSupplier extends JFrame implements ActionListener
{
JFrame jf;
JLabel l1,l2,l3,l4,l5,l6;
JTextField t1,t2,t3,t4,t5;
JButton b0,b1,b2,b3;
GridBagLayout gbl;
GridBagConstraints gbc;
Font f;
Connection con;
PreparedStatement ps;
Statement stmt;
ResultSet rs;
DefaultTableModel model = new DefaultTableModel();
JTable tabGrid = new JTable(model);
JScrollPane scrlPane = new JScrollPane(tabGrid);
UpdateSupplier()
{
jf=new JFrame();
Dimension screen= Toolkit.getDefaultToolkit().getScreenSize();
f = new Font("Times New Roman",Font.BOLD,20);
jf.setLayout(null);
l6=new JLabel("Update Supplier");
l6.setFont(new Font("Times New Roman",Font.BOLD,25));
l6.setBounds(300,50,300,40);l6.setForeground(Color.blue);
jf.add(l6);
l1= new JLabel("Supplier id *");
//l1.setFont(f);
l1.setBounds(150,120,130,25);
jf.add(l1);
t1=new JTextField(20);
t1.setBounds(320,120,100,25);t1.setToolTipText("Enter supplier id");
jf.add(t1);
l2 = new JLabel("Supplier name*");
//l2.setFont(f);
l2.setBounds(150,160,170,25);
jf.add(l2);
t2=new JTextField(20);
t2.setBounds(320,160,200,25);t2.setToolTipText("Enter supplier name");
jf.add(t2);
l3 = new JLabel("Supplier address*");
//l3.setFont(f);
l3.setBounds(150,200,170,25);
jf.add(l3);
t3=new JTextField(20);
t3.setBounds(320,200,250,25);t3.setToolTipText("Enter supplier address");
jf.add(t3);
l4 = new JLabel("Supplier phone no*");
//l4.setFont(f);
l4.setBounds(150,240,170,25);
jf.add(l4);
t4=new JTextField(20);
t4.setBounds(320,240,100,25);t4.setToolTipText("Enter supplier phone no");
jf.add(t4);
l5 = new JLabel("Supplier email id*");
//l5.setFont(f);
l5.setBounds(150,280,170,25);
jf.add(l5);
t5=new JTextField(20);
t5.setBounds(320,280,200,25);t5.setToolTipText("Enter supplier emailid");
jf.add(t5);
b0 = new JButton("Open",new ImageIcon("images//open.png"));
b0.setBounds(150,330,110,35);b0.setToolTipText("click to open supplier details");
jf.add(b0); b0.addActionListener(this);
b1 = new JButton("Update",new ImageIcon("images//update.png"));
b1.setBounds(300,330,110,35);b1.setToolTipText("click to update supplier details");
jf.add(b1);b1.addActionListener(this);
b2 = new JButton("Clear",new ImageIcon("images//clear.png"));
b2.setBounds(450,330,110,35);b2.setToolTipText("click to clear all textfilds");
jf.add(b2);b2.addActionListener(this);
b3 = new JButton("All",new ImageIcon("images//all.png"));
b3.setBounds(600,330,110,35);b3.setToolTipText("click to view all supplier details");
jf.add(b3); b3.addActionListener(this);
scrlPane.setBounds(0,380,900,600);
jf.add(scrlPane);
tabGrid.setFont(new Font ("Times New Roman",0,15));
model.addColumn("S_ID");
model.addColumn("S_NAME");
model.addColumn("S_ADDRESS");
model.addColumn("S_PHONENO");
model.addColumn("S_EMAILID");
jf.setTitle("Update Supplier");
jf.setSize(900,700);
jf.setLocation(20,20);
jf.setResizable(false);
jf.getContentPane().setBackground(Color.cyan);
jf.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b0)
{
if(((t1.getText()).equals(""))&&((t2.getText()).equals("")))
{
JOptionPane.showMessageDialog(this,"Please enter supplier id or name !","Warning!!!",JOptionPane.WARNING_MESSAGE);
}
else
{//fetch
try
{
int foundrec = 0;
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/medical_store","root","");
System.out.println("Connected to database.");
ps=con.prepareStatement("select * from supplier where sid='"+t1.getText()+"' or sname='"+t2.getText()+"'");
rs=ps.executeQuery();
while(rs.next())
{
t1.setText(rs.getString(1));
t2.setText(rs.getString(2));
t3.setText(rs.getString(3));
t4.setText(rs.getString(4));
t5.setText(rs.getString(5));
foundrec = 1;
}
if (foundrec == 0)
{
JOptionPane.showMessageDialog(null,"Record is not available","Dialog",JOptionPane.WARNING_MESSAGE);
}
con.close();
}
catch(SQLException se)
{
System.out.println(se);
JOptionPane.showMessageDialog(null,"SQL Error:"+se);
}
catch(Exception e)
{
System.out.println(e);
JOptionPane.showMessageDialog(null,"Error:"+e);
}
}
}
else if(ae.getSource()==b1)
{//update
String email=t5.getText();
Pattern p=Pattern.compile("[_a-z_A-Z_0-9]*[0-9]*@[a-zA-Z0-9]*.[a-zA-Z0-9]*");
Matcher m=p.matcher(email);
boolean matchFound=m.matches();
if(((t1.getText()).equals(""))&&((t2.getText()).equals("")))
{
JOptionPane.showMessageDialog(this,"Please enter supplier id or name !","Warning!!!",JOptionPane.ERROR_MESSAGE);
}
else if(((t2.getText()).equals(""))||((t3.getText()).equals(""))||((t4.getText()).equals(""))||((t5.getText()).equals("")))
{
JOptionPane.showMessageDialog(this,"* Detail are Missing !","Warning!!!",JOptionPane.ERROR_MESSAGE);
}
else if(!matchFound)
{
JOptionPane.showMessageDialog(this,"Invalid email id!","Warning!!!",JOptionPane.WARNING_MESSAGE);
}
else
{
try
{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/medical_store","root","");
System.out.println("Connected to database.");
stmt=con.createStatement();
String str1="UPDATE supplier SET sid='"+t1.getText()+"',sname='"+t2.getText()+"',saddress='"+t3.getText()+"',sphoneno='"+t4.getText()+"',semailid='"+t5.getText()+"' where sid='"+t1.getText()+"' or sname='"+t2.getText()+"' ";
stmt.executeUpdate(str1);
JOptionPane.showMessageDialog(null, "Record is updated");
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
t5.setText("");
con.close();
}
catch(SQLException se)
{
System.out.println(se);
JOptionPane.showMessageDialog(null,"SQL Error:"+se);
}
catch(Exception e)
{
System.out.println(e);
JOptionPane.showMessageDialog(null,"Error:"+e);
}
}
}
else if(ae.getSource()==b2)
{//clear
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
t5.setText("");
}
else if(ae.getSource()==b3)
{//list
int r = 0;
try
{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/medical_store","root","");
System.out.println("Connected to database.");
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery("SELECT * from supplier" );
while(rs.next())
{
model.insertRow(r++, new Object[]{rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5) });
}
con.close();
}
catch(SQLException se)
{
System.out.println(se);
JOptionPane.showMessageDialog(null,"SQL Error"+se);
}
catch(Exception e)
{
System.out.println(e);
JOptionPane.showMessageDialog(null,"Error:"+e);
}
}
}
public static void main(String args[])
{
new UpdateSupplier();
}
}