-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathViewEmployee.java
More file actions
320 lines (283 loc) · 8.34 KB
/
ViewEmployee.java
File metadata and controls
320 lines (283 loc) · 8.34 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import java.lang.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.sql.*;
public class ViewEmployee extends JFrame implements ActionListener
{
JLabel welcomeLabel, userLabel, eNameLabel, phoneLabel, roleLabel, salaryLabel;
JTextField userTF, phoneTF1, phoneTF2, eNameTF, roleTF, salaryTF;
JButton refreshBtn, loadBtn, updateBtn, delBtn, backBtn, logoutBtn;
JPanel panel;
ImageIcon limg, lbtnImg;
Font pixelPowerline;
String userId;
public ViewEmployee(String userId)
{
super("View Employee");
limg = new ImageIcon ("library.png");
this.setIconImage (limg.getImage());
this.userId = userId;
this.setSize(600,420);
this.setLocation(300,150);
this.setResizable (false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try
{
pixelPowerline = Font.createFont(Font.TRUETYPE_FONT, new File("pixelPowerline.ttf")).deriveFont(15f);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont (Font.createFont(Font.TRUETYPE_FONT, new File ("pixelPowerline.ttf")));
}
catch(IOException | FontFormatException e){}
panel = new JPanel();
panel.setLayout(null);
lbtnImg = new ImageIcon ("rsz_logout.png");
welcomeLabel = new JLabel("Welcome, "+userId);
welcomeLabel.setBounds(410, 40, 400, 30);
welcomeLabel.setFont (pixelPowerline);
panel.add(welcomeLabel);
logoutBtn = new JButton(lbtnImg);
logoutBtn.setBounds(550, 45, 24, 24);
logoutBtn.addActionListener(this);
panel.add(logoutBtn);
refreshBtn = new JButton("Refresh");
refreshBtn.setBounds(400, 350, 120, 30);
refreshBtn.addActionListener(this);
panel.add(refreshBtn);
userLabel = new JLabel("User ID : ");
userLabel.setBounds(100, 100, 100, 30);
panel.add(userLabel);
userTF = new JTextField();
userTF.setBounds(200, 100, 120, 30);
panel.add(userTF);
loadBtn = new JButton("Load");
loadBtn.setBounds(400, 100, 120, 30);
loadBtn.addActionListener(this);
panel.add(loadBtn);
eNameLabel = new JLabel("Employee Name : ");
eNameLabel.setBounds(100, 150, 120, 30);
panel.add(eNameLabel);
eNameTF = new JTextField();
eNameTF.setBounds(200, 150, 120, 30);
panel.add(eNameTF);
phoneLabel = new JLabel("Phone No. : ");
phoneLabel.setBounds(100, 200, 120, 30);
panel.add(phoneLabel);
phoneTF1 = new JTextField();
phoneTF1.setBounds(200, 200, 35, 30);
phoneTF1.setEnabled(false);
panel.add(phoneTF1);
phoneTF2 = new JTextField();
phoneTF2.setBounds(235, 200, 85, 30);
panel.add(phoneTF2);
roleLabel = new JLabel("Role : ");
roleLabel.setBounds(100, 250, 120, 30);
panel.add(roleLabel);
roleTF = new JTextField();
roleTF.setBounds(200, 250, 120, 30);
panel.add(roleTF);
salaryLabel = new JLabel("Salary : ");
salaryLabel.setBounds(100, 300, 120, 30);
panel.add(salaryLabel);
salaryTF = new JTextField();
salaryTF.setBounds(200, 300, 120, 30);
panel.add(salaryTF);
updateBtn = new JButton("Update");
updateBtn.setBounds(100, 350, 120, 30);
updateBtn.setEnabled(false);
updateBtn.addActionListener(this);
panel.add(updateBtn);
delBtn = new JButton("Delete");
delBtn.setBounds(250, 350, 120, 30);
delBtn.setEnabled(false);
delBtn.addActionListener(this);
panel.add(delBtn);
backBtn = new JButton ("< Back");
backBtn.setBounds (5,5,100,30);
backBtn.addActionListener (this);
panel.add (backBtn);
this.add(panel);
}
public void actionPerformed(ActionEvent ae)
{
String text = ae.getActionCommand();
if(text.equals(backBtn.getText()))
{
ManageEmployee me = new ManageEmployee(userId);
me.setVisible(true);
this.setVisible(false);
}
if(text.equals(refreshBtn.getText()))
{
updateBtn.setEnabled(false);
delBtn.setEnabled(false);
userTF.setEnabled(true);
userTF.setText("");
eNameTF.setText("");
phoneTF1.setText("");
phoneTF2.setText("");
roleTF.setText("");
salaryTF.setText("");
}
else if(text.equals(logoutBtn.getText()))
{
HomeWindow hw = new HomeWindow();
hw.setVisible(true);
this.setVisible(false);
}
else if(text.equals(loadBtn.getText()))
{
loadFromDB();
}
else if(text.equals(updateBtn.getText()))
{
updateInDB();
}
else if(text.equals(delBtn.getText()))
{
deleteFromDB();
}
else{}
}
public void loadFromDB()
{
String loadId = userTF.getText();
String query = "SELECT `employeeName`, `phoneNumber`, `role`, `salary` FROM `employee` WHERE `userId`='"+loadId+"';";
Connection con = null;//for connection
Statement st = null;//for query execution
ResultSet rs = null;//to get row by row result from DB
System.out.println(query);
try
{
Class.forName("com.mysql.jdbc.Driver");//load driver
System.out.println("driver loaded");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/oop1","root","");
System.out.println("connection done");//connection with database established
st = con.createStatement();//create statement
System.out.println("statement created");
rs = st.executeQuery(query);//getting result
System.out.println("results received");
boolean flag = false;
String eName = null;
String phnNo = null;
String role = null;
double salary = 0.0;
while(rs.next())
{
eName = rs.getString("employeeName");
phnNo = rs.getString("phoneNumber");
role = rs.getString("role");
salary = rs.getDouble("salary");
flag=true;
eNameTF.setText(eName);
phoneTF1.setText("+880");
phoneTF2.setText(phnNo.substring(4,14));
roleTF.setText(role);
salaryTF.setText(""+salary);
userTF.setEnabled(false);
updateBtn.setEnabled(true);
delBtn.setEnabled(true);
}
if(!flag)
{
eNameTF.setText("");
phoneTF1.setText("");
phoneTF2.setText("");
roleTF.setText("");
salaryTF.setText("");
JOptionPane.showMessageDialog(this,"Invalid ID");
}
}
catch(Exception ex)
{
System.out.println("Exception : " +ex.getMessage());
}
finally
{
try
{
if(rs!=null)
rs.close();
if(st!=null)
st.close();
if(con!=null)
con.close();
}
catch(Exception ex){}
}
}
public void updateInDB()
{
String newId = userTF.getText();
String eName = eNameTF.getText();
String phnNo = phoneTF1.getText()+phoneTF2.getText();
String role = roleTF.getText();
double salary=0.0;
try
{
salary = Double.parseDouble(salaryTF.getText());
}
catch(Exception e){}
String query = "UPDATE employee SET employeeName='"+eName+"', phoneNumber = '"+phnNo+"', role = '"+role+"', salary = "+salary+" WHERE userId='"+newId+"'";
Connection con=null;//for connection
Statement st = null;//for query execution
System.out.println(query);
try
{
Class.forName("com.mysql.jdbc.Driver");//load driver
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/oop1","root","");
st = con.createStatement();//create statement
st.executeUpdate(query);
st.close();
con.close();
JOptionPane.showMessageDialog(this, "Success !!!");
updateBtn.setEnabled(false);
delBtn.setEnabled(false);
userTF.setEnabled(true);
userTF.setText("");
eNameTF.setText("");
phoneTF1.setText("");
phoneTF2.setText("");
roleTF.setText("");
salaryTF.setText("");
}
catch(Exception e)
{
System.out.println(e.getMessage());
JOptionPane.showMessageDialog(this, "Oops !!!");
}
}
public void deleteFromDB()
{
String newId = userTF.getText();
String query1 = "DELETE from employee WHERE userId='"+newId+"';";
String query2 = "DELETE from login WHERE userId='"+newId+"';";
System.out.println(query1);
System.out.println(query2);
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/oop1", "root", "");
Statement stm = con.createStatement();
stm.execute(query1);
stm.execute(query2);
stm.close();
con.close();
JOptionPane.showMessageDialog(this, "Success !!!");
updateBtn.setEnabled(false);
delBtn.setEnabled(false);
userTF.setEnabled(true);
userTF.setText("");
eNameTF.setText("");
phoneTF1.setText("");
phoneTF2.setText("");
roleTF.setText("");
salaryTF.setText("");
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(this, "Oops !!!");
}
}
}