-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathDatabaseManager.java
More file actions
292 lines (255 loc) · 7.28 KB
/
Copy pathDatabaseManager.java
File metadata and controls
292 lines (255 loc) · 7.28 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
package org.payroll;
import java.io.*;
import java.sql.*;
import java.util.*;
public class DatabaseManager {
String ConnectionString;
Connection conn;
Statement curs;
PreparedStatement ps;
public DatabaseManager(String db) {
ConnectionString = "jdbc:sqlite:" + db;
if (!(new File(db)).exists()) {
connectToDatabase();
initNewDatabase();
} else {
connectToDatabase();
}
}
void connectToDatabase() {
try {
conn = DriverManager.getConnection(ConnectionString);
curs = conn.createStatement();
curs.setQueryTimeout(30);
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
void initNewDatabase() {
try {
curs.executeUpdate(
"CREATE TABLE login_ids(id INTEGER NOT NULL PRIMARY KEY, username STRING NOT NULL, password STRING NOT NULL)"
);
curs.executeUpdate(
"INSERT INTO login_ids VALUES(null, \"admin\", \"password\")"
);
curs.executeUpdate(
"CREATE TABLE departments(" +
"id INTEGER NOT NULL PRIMARY KEY," +
"dep_name STRING NOT NULL," +
"basic_salary INTEGER NOT NULL," +
"da INTEGER NOT NULL," +
"hra INTEGER NOT NULL," +
"pf INTEGER NOT NULL," +
"gross_salary INTEGER NOT NULL," +
"epf INTEGER NOT NULL," +
"lic INTEGER NOT NULL," +
"deductions INTEGER NOT NULL," +
"net_salary INTEGER NOT NULL" +
")"
);
curs.executeUpdate(
"CREATE TABLE employees(" +
"id INTEGER NOT NULL PRIMARY KEY," +
"first_name STRING NOT NULL," +
"last_name STRING NOT NULL," +
"email STRING NOT NULL," +
"department STRING NOT NULL" +
")"
);
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
public Boolean verifyLoginId(String username) {
try {
ps = conn.prepareStatement("SELECT * FROM login_ids WHERE lower(username) = ? "); // secure from sql injection
ps.setString(1, username);
return ps.executeQuery().next();
} catch (SQLException e) {
System.err.println(e.getMessage());
}
return false;
}
public Boolean verifyLoginId(String username, String password) {
try {
return curs.executeQuery(
"SELECT * FROM login_ids WHERE username=\"" + username + "\" AND password=\"" + password + "\""
).next();
} catch (SQLException e) {
System.err.println(e.getMessage());
}
return false;
}
public void createLoginId(String username, String password) {
try {
curs.executeUpdate("INSERT INTO login_ids VALUES(null, \"" + username + "\", \"" + password + "\")");
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
public void deleteLoginId(String username) {
try {
curs.executeUpdate(
"DELETE FROM login_ids WHERE username=\"" + username + "\""
);
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
public void changePassword(String username, String newPassword) {
try {
curs.executeUpdate(
"UPDATE login_ids SET password=\"" + newPassword + "\" WHERE username=\"" + username + "\""
);
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
public Boolean existsDepartment(String dep_name) {
try {
return curs.executeQuery(
"SELECT * FROM departments WHERE dep_name=\"" + dep_name + "\""
).next();
} catch (SQLException e) {
System.err.println(e.getMessage());
}
return false;
}
public void newDepartment(String dep_name, int basic_salary, int da_percent, int hra_percent, int pf_percent) {
int da = (da_percent / 100) * basic_salary;
int hra = (hra_percent / 100) * basic_salary;
int pf = (pf_percent / 100) * basic_salary;
int gross_salary = basic_salary + da + hra + pf;
int epf = pf / 2;
int lic = epf / 2;
int deductions = epf + lic;
int net_salary = gross_salary - deductions;
try {
curs.executeUpdate(
"INSERT INTO departments VALUES(" +
"null," +
"\"" + dep_name + "\" ," +
Integer.toString(basic_salary) + "," +
Integer.toString(da) + "," +
Integer.toString(hra) + "," +
Integer.toString(pf) + "," +
Integer.toString(gross_salary) + "," +
Integer.toString(epf) + "," +
Integer.toString(lic) + "," +
Integer.toString(deductions) + "," +
Integer.toString(net_salary) +
")"
);
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
public void deleteDepartment(String dep_name) {
try {
curs.executeUpdate(
"DELETE FROM departments WHERE dep_name=\"" + dep_name + "\""
);
curs.executeUpdate(
"DELETE FROM employees WHERE department=\"" + dep_name + "\""
);
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
public void updateDepartment(String dep_name, int basic_salary, int da, int hra, int pf) {
deleteDepartment(dep_name);
newDepartment(dep_name, basic_salary, da, hra, pf);
}
public ArrayList<String> getListOfDepartments() {
ArrayList<String> lst = new ArrayList<String>();
try {
ResultSet rs = curs.executeQuery("SELECT dep_name FROM departments");
while (rs.next()) {
lst.add(rs.getString("dep_name"));
}
} catch (SQLException e) {
System.err.println(e.getMessage());
}
return lst;
}
public int getSalary(String dep_name) {
try {
ResultSet rs = curs.executeQuery("SELECT net_salary FROM departments WHERE dep_name=\"" + dep_name + "\"");
if (rs.next())
return rs.getInt("net_salary");
} catch (SQLException e) {
System.err.println(e.getMessage());
}
return 0;
}
public Boolean existsEmployeeID(int id) {
try {
return curs.executeQuery(
"SELECT * FROM employees WHERE id=" + Integer.toString(id)
).next();
} catch (SQLException e) {
System.err.println(e.getMessage());
}
return false;
}
public void createEmployee(String fn, String ln, String email, String department) {
try {
curs.executeUpdate("INSERT INTO employees VALUES(" +
"null," +
"\"" + fn + "\"," +
"\"" + ln + "\"," +
"\"" + email + "\"," +
"\"" + department + "\"" +
")");
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
public void deleteEmployee(int id) {
try {
curs.executeUpdate(
"DELETE FROM employees WHERE id=" + Integer.toString(id)
);
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
public void updateEmployee(int id, String fn, String ln, String email, String department) {
try {
curs.executeUpdate(
"UPDATE employees SET " +
"first_name=\"" + fn + "\"," +
"last_name=\"" + ln + "\"," +
"email=\"" + email + "\"," +
"department=\"" + department + "\" " +
"WHERE id=" + Integer.toString(id)
);
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
public Object[][] getEmployees() {
ArrayList<Object[]> employees = new ArrayList<Object[]>();
ResultSet rs;
try {
rs = curs.executeQuery(
"SELECT * FROM employees"
);
while (rs.next()) {
Object[] temp = {
rs.getInt("id"),
rs.getString("first_name"),
rs.getString("last_name"),
rs.getString("email"),
rs.getString("department"),
getSalary(rs.getString("department"))
};
employees.add(temp);
}
} catch (SQLException e) {
System.err.println(e.getMessage());
}
return employees.toArray(new Object[employees.size()][]);
}
}