-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnector.java
More file actions
176 lines (157 loc) · 6.95 KB
/
Connector.java
File metadata and controls
176 lines (157 loc) · 6.95 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
package wagesmanagementsystem;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class Connector {
private static final String dLoc = "jdbc:ucanaccess://WMSDB.accdb";
Connection conn;
PreparedStatement prep;
ResultSet rs;
public Connector() {
try {
conn = DriverManager.getConnection(dLoc);
System.out.println("Connection Successful");
} catch (SQLException e) {
System.out.println("Problem While Connecting");
System.out.println(e.getMessage());
}
}
public ResultSet runSelect(String query) {
try {
prep = conn.prepareStatement(query);
rs = prep.executeQuery();// only for select queries
System.out.println("Query executed successfully");
return rs;
} catch (SQLException e) {
System.out.println("Problem while executing query");
System.out.println(e.getMessage());
}
return null;
}
public void validateLogin(String user, String name, String pass,JFrame window,JFrame frame_login){
if(user.equals("employee")){
try {
prep = conn.prepareStatement("SELECT empname, pass from employee WHERE empname= ? AND pass= ?");
prep.setString(1, name);
prep.setString(2,pass);
rs = prep.executeQuery();// only for select queries
System.out.println("Query executed successfully");
if(!rs.next()){
System.out.println("Username or password is incorrect.");
JOptionPane.showMessageDialog(window, "Username or password is incorrect");
} else{
JOptionPane.showMessageDialog(window, "Logged in as " + user +": " + name);
EmployeeUI empUI = new EmployeeUI(name, pass);
empUI.pack();
empUI.setVisible(true);
window.setVisible(false);
frame_login.setVisible(false);
}
} catch (SQLException e) {
System.out.println("Problem while executing query");
System.out.println(e.getMessage());
}
} else if(user.equals("manager")){
try {
prep = conn.prepareStatement("SELECT managername, pass from manager WHERE managername=? AND pass=?");
prep.setString(1, name);
prep.setString(2,pass);
rs = prep.executeQuery();// only for select queries
System.out.println("Query executed successfully");
if(!rs.next()){
System.out.println("Username or password is incorrect.");
JOptionPane.showMessageDialog(window, "Username or password is incorrect");
} else{
JOptionPane.showMessageDialog(window, "Logged in as " + user +": " + name);
ManagerUI mngUI = new ManagerUI(name, pass);
mngUI.pack();
mngUI.setVisible(true);
window.setVisible(false);
frame_login.setVisible(false);
}
} catch (SQLException e) {
System.out.println("Problem while executing query");
System.out.println(e.getMessage());
}
} else if(user.equals("admin")){
try {
prep = conn.prepareStatement("SELECT adminname, pass from admin WHERE adminname=? AND pass=?");
prep.setString(1, name);
prep.setString(2,pass);
rs = prep.executeQuery();// only for select queries
System.out.println("Query executed successfully");
if(!rs.next()){
System.out.println("Username or password is incorrect.");
JOptionPane.showMessageDialog(window, "Username or password is incorrect");
} else{
JOptionPane.showMessageDialog(window, "Logged in as " + user);
AdminUI adminUI = new AdminUI(name, pass);
adminUI.pack();
adminUI.setVisible(true);
window.setVisible(false);
frame_login.setVisible(false);
}
} catch (SQLException e) {
System.out.println("Problem while executing query");
System.out.println(e.getMessage());
}
}
}
public void runDML(String query) {
try {
prep = conn.prepareStatement(query);
prep.executeUpdate();//for for insert update delete queries
System.out.println("Record Inserted successfully");
} catch (SQLException e) {
System.out.println("Problem while inserting Record");
System.out.println(e.getMessage());
}
}
public void runUpdate(String query,String data_current,String data_new,JPanel panel_name, String data){
try {
prep = conn.prepareStatement(query);
int res = JOptionPane.showConfirmDialog(panel_name, "Are you sure want to change your "+ data+ "?");
if(res == 0){
prep.setString(1,data_new);//for for insert update delete queries
prep.executeUpdate();
System.out.println("Record updated successfully");
} else {
System.out.println("No action taken.");
}
} catch (SQLException e) {
System.out.println("Problem while inserting Record");
System.out.println(e.getMessage());
}
}
public void runDelAcc(String query, JFrame window, JPanel panel_name){
int res = JOptionPane.showConfirmDialog(panel_name, "Are you sure want to delete your account?");
if(res == 0){
runDML(query);
System.out.println("Deleted successfully");
new MainMenu().setVisible(true);
window.setVisible(false);
} else {
System.out.println("No action taken.");
}
}
public void updateJTable(String query, JTable table,String ID,String name,String salary){
try{
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.setRowCount(0);
prep = conn.prepareStatement(query);
rs = prep.executeQuery();
while(rs.next()){
String[] tbData = {rs.getString(ID), rs.getString(name), rs.getString(salary)};
DefaultTableModel tblModel = (DefaultTableModel)table.getModel();
tblModel.addRow(tbData);
}
} catch (SQLException e) {
System.out.println("Problem while executing query");
System.out.println(e.getMessage());
}
}
}