-
Notifications
You must be signed in to change notification settings - Fork 1
/
AddNewSupplier.java
208 lines (184 loc) · 6.28 KB
/
AddNewSupplier.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
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 AddNewSupplier extends JFrame implements ActionListener
{
JFrame jf;
JTextField t1,t2,t3,t4,t5,tr,tq;
JLabel l1,l2,l3,l4,l5,l6;
JButton b0,b1,b2;
Font f;
Connection con;
PreparedStatement ps;
Statement stmt;
ResultSet rs;
DefaultTableModel model = new DefaultTableModel();
JTable tabGrid = new JTable(model);
JScrollPane scrlPane = new JScrollPane(tabGrid);
AddNewSupplier()
{
jf=new JFrame();
f = new Font("Times New Roman",Font.BOLD,20);
jf.setLayout(null);
l6=new JLabel("New Supplier details");
l6.setFont(new Font("Times New Roman",Font.BOLD,25));
l6.setBounds(250,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.setEditable(false);
// t1.setBounds(320,120,100,25);
// 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 email id");
jf.add(t5);
b0 = new JButton("Save",new ImageIcon("images//save.png"));
b0.setBounds(150,330,110,35);b0.setToolTipText("click to save supplier details");
jf.add(b0);b0.addActionListener(this);
b1 = new JButton("Clear",new ImageIcon("images//clear.png"));
b1.setBounds(300,330,110,35);b1.setToolTipText("click to clear all textfilds");
jf.add(b1); b1.addActionListener(this);
b2= new JButton("All",new ImageIcon("images//all.png"));
b2.setBounds(450,330,110,35);b2.setToolTipText("click to view all supplier details");
jf.add(b2); b2.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("Add New 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)
{
String mob = t4.getText();
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(((t2.getText()).equals(""))||((t3.getText()).equals(""))||((t4.getText()).equals(""))||((t5.getText()).equals("")))
{
JOptionPane.showMessageDialog(this,"* Detail are Missing !","Warning!!!",JOptionPane.WARNING_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.");
ps=con.prepareStatement("insert into supplier (sname,saddress,sphoneno,semailid)values(?,?,?,?)");
ps.setString(1,t2.getText());
ps.setString(2,t3.getText());
ps.setString(3,t4.getText());
ps.setString(4,t5.getText());
ps.executeUpdate();
int reply=JOptionPane.showConfirmDialog(null,"Supplier added successfully.Do you want add more supplier?","Added Supplier",JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION)
{
jf.setVisible(false);
new AddNewSupplier();
}
else if (reply == JOptionPane.NO_OPTION)
{
jf.setVisible(false);
}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)
{//clear
//t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
t5.setText("");
}
else if(ae.getSource()==b2)
{//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 group by sid asc" );
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 AddNewSupplier();
}
}