-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmployee.java
179 lines (156 loc) · 6.07 KB
/
Employee.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
package com.applications;
import net.proteanit.sql.DbUtils;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class Employee {
Connection con;
PreparedStatement pst;
private JPanel Main;
private JTextField txtName;
private JTextField txtSalary;
private JTextField txtMobile;
private JButton saveButton;
private JButton updateButton;
private JButton deleteButton;
private JButton searchButton;
private JTable table1;
private JTextField txtid;
private JScrollPane table_1;
public Employee() {
connect();
table_load();
saveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String empname, salary, mobile;
empname = txtName.getText();
salary = txtSalary.getText();
mobile = txtMobile.getText();
try {
pst = con.prepareStatement("insert into employee(empname,salary,mobile)values(?,?,?)");
pst.setString(1, empname);
pst.setString(2, salary);
pst.setString(3, mobile);
pst.executeUpdate();
JOptionPane.showMessageDialog(null, "Record Added");
table_load();
txtName.setText("");
txtSalary.setText("");
txtMobile.setText("");
txtName.requestFocus();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
});
searchButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
String empid = txtid.getText();
pst = con.prepareStatement("select empname,salary,mobile from employee where id = ?");
pst.setString(1, empid);
ResultSet rs = pst.executeQuery();
if (rs.next() == true) {
String empname = rs.getString(1);
String emsalary = rs.getString(2);
String emmobile = rs.getString(3);
txtName.setText(empname);
txtSalary.setText(emsalary);
txtMobile.setText(emmobile);
} else {
txtName.setText("");
txtSalary.setText("");
txtMobile.setText("");
JOptionPane.showMessageDialog(null, "Invalid Employee No");
}
}
catch (SQLException ex) {
ex.printStackTrace();
}
}
}
);
updateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String empid,empname,salary,mobile;
empname = txtName.getText();
salary = txtSalary.getText();
mobile = txtMobile.getText();
empid = txtid.getText();
try {
pst = con.prepareStatement("update employee set empname = ?,salary = ?,mobile = ? where id = ?");
pst.setString(1, empname);
pst.setString(2, salary);
pst.setString(3, mobile);
pst.setString(4, empid);
pst.executeUpdate();
JOptionPane.showMessageDialog(null, "Record Update");
table_load();
txtName.setText("");
txtSalary.setText("");
txtMobile.setText("");
txtName.requestFocus();
}
catch (SQLException e1)
{
e1.printStackTrace();
}
}
});
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String empid;
empid = txtid.getText();
try {
pst = con.prepareStatement("delete from employee where id = ?");
pst.setString(1, empid);
pst.executeUpdate();
JOptionPane.showMessageDialog(null, "Record Delete");
table_load();
txtName.setText("");
txtSalary.setText("");
txtMobile.setText("");
txtName.requestFocus();
}
catch (SQLException e1)
{
e1.printStackTrace();
}
}
});
}
public static void main(String[] args) {
JFrame frame = new JFrame("Employee");
frame.setContentPane(new Employee().Main);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public void connect() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Name of database/Table created", "Database username", "Write here password for your database");
System.out.println("Successs");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
catch (SQLException ex) {
ex.printStackTrace();
}
}
void table_load() {
try {
pst = con.prepareStatement("select * from employee");
ResultSet rs = pst.executeQuery();
table1.setModel(DbUtils.resultSetToTableModel(rs));
}
catch (SQLException e) {
e.printStackTrace();
}
}
}