-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataBaseManager.java
More file actions
268 lines (242 loc) · 6.52 KB
/
DataBaseManager.java
File metadata and controls
268 lines (242 loc) · 6.52 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
package server;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/*
* Create object obj
* obj.setUsername (mysql ka, if different)
* obj.setPassword (mysql ka, if different)
* obj.setPortNumber (mysql ka, if different)
* obj.create()---- This doesn't change the existing tables ----
* obj.add/change/check()
* obj.destroy() ***(only if you want to remove the database completely)**
* obj.close()
*
*
*
*/
public class DataBaseManager {
private String driver ="com.mysql.cj.jdbc.Driver";
private String database ="jdbc:mysql://localhost:";
private String username="root";
private String password="password";
private Connection con;
private Statement st;
public void updateConnection()throws Exception {
Class.forName(driver);
con= DriverManager.getConnection(database,username,password);
st=con.createStatement();
}
public void close() {
try {
con.close();
st.close();
}
catch(Exception e) {
System.out.println(e);
}
}
private void createConnection()throws Exception {
Class.forName(driver);
con= DriverManager.getConnection("jdbc:mysql://localhost:3306",username,password);
st=con.createStatement();
}
private boolean createSchema() {
try {
createConnection();
}catch(Exception e) {
return false;
}
try {
st.executeUpdate("create schema intercode");
System.out.println("Schema created as intercode");
}
catch(Exception e) {
System.out.println(e);
}
return true;
}
public void setUsername(String username) {
this.username=username;
}
public void setPassword(String pass) {
password=pass;
}
public void setPortNumber(String port) {
if(port==null||port.equals("")) {
database="jdbc:mysql://localhost:"+"3306"+"/intercode";
}
else {
database="jdbc:mysql://localhost:"+port+"/intercode";
}
}
public Connection getConnection() {
return con;
}
public boolean checkuID(String str) {
ResultSet rs;
try {
rs=st.executeQuery("select uID from userInfo where uID = "+str);
while(rs.next()) {
if(str.equals(rs.getString(1))) {
return true;
}
}
} catch (Exception e) {
System.out.println("Error in checking userID present : "+e);
}
return false;
}
public boolean checkIntLogin(String uID, char pass[] ) {
System.out.println("Login request to Interviewer "+uID+" pass= "+pass.toString());
ResultSet rs;
try {
rs=st.executeQuery("select * from intInfo");
while(rs.next()) {
if(uID.equals(rs.getString(1))) {
if(pass.equals(rs.getString(2))){
System.out.println("Login successful to :"+rs.getString(1));
return true;
}
}
}
} catch (Exception e) {
System.out.println("Error in checking login : "+e);
}
System.out.println("Login unsuccessful to :"+uID+","+pass.toString());
return false;
}
public boolean checkLogin(String uID, char pass[] ) {
System.out.println("Login request to "+uID+" pass= "+pass.toString());
ResultSet rs;
try {
rs=st.executeQuery("select * from userInfo");
while(rs.next()) {
if(uID.equals(rs.getString(1))) {
if(pass.toString().equals(rs.getString(2))){
System.out.println("Login successful to :"+rs.getString(1));
return true;
}
}
}
} catch (Exception e) {
System.out.println("Error in checking login : "+e);
}
System.out.println("Login unsuccessful to :"+uID+","+pass.toString());
return false;
}
public boolean changeUserPassword(String uID, char pass[]) {
try {
st.executeUpdate("UPDATE userinfo SET password = '"+pass.toString()+"' WHERE uID = '"+uID+"'");
}
catch(Exception e) {
System.out.println("Password change unsuccessful\n"+e);
return false;
}
return true;
}
public boolean changeIntPassword(String uID, char oldP[],char newP[]) {
try {
if(checkLogin(uID, oldP)) {
st.executeUpdate("UPDATE intInfo SET password = '"+newP.toString()+"' WHERE uID = '"+uID+"'");
}
else { return false;
}
}
catch(Exception e) {
System.out.println("Int Password change unsuccessful\n"+e);
return false;
}
return true;
}
public void addInt(String I_UID, char pass[]) throws Exception {
PreparedStatement ps= con.prepareStatement("insert into intInfo "
+ "values (?,?) ");
ps.setString(1, I_UID);
ps.setString(2, pass.toString());
ps.executeUpdate();
System.out.println("Int added to database :"+I_UID);
}
public void addUser(String uID, String IntUID ,char pass[]) throws Exception {
PreparedStatement ps = con.prepareStatement("INSERT INTO userinfo "
+ "values(?,?,?,?,?) ");
ps.setString(1, uID);
ps.setString(3, IntUID);
ps.setInt(4, 0);
ps.setString(5, "");
ps.setString(2, pass.toString());
ps.executeUpdate();
System.out.println("user added to database :"+uID);
}
public void destroy() {
try {
updateConnection();
} catch (Exception e) {
System.out.println(e);
}
try {
st.executeUpdate("drop Database intercode");
} catch (Exception e) {
System.out.println(e);
}
}
public List<String> getInterviewee(String I_UID){
List<String> list =new ArrayList<>();
try {
ResultSet rs=st.executeQuery("Select UserID from UserInfo where I_UID= '"+I_UID+"'");
while(rs.next()) {
list.add(rs.getString(1));
}
}
catch(Exception e) {
System.out.println(e);
}
return list;
}
public void addScore(String userID, String score, String comments) {
try {
st.executeUpdate("UPDATE userInfo SET score = '"+score+"', comments = '"+comments+"' WHERE userID = '"+userID+"'");
System.out.println("");
}
catch(Exception e2) {
System.out.println(e2);
}
}
public void createIntInfo() throws Exception {
st.executeUpdate("CREATE TABLE intInfo (userID VARCHAR(30), "
+ " password VARCHAR(30), "
+ " UNIQUE INDEX `userID_UNIQUE` (`userID` ASC) VISIBLE)");
}
public void createUserInfo() throws Exception {
st.executeUpdate("CREATE TABLE userInfo (userID VARCHAR(30), "
+ " password VARCHAR(30), "
+ " I_UID VARCHAR(30), "
+ " score int(10), "
+ " notes VARCHAR(1000), "
+ " UNIQUE INDEX `userID_UNIQUE` (`userID` ASC) VISIBLE)");
}
public DataBaseManager(String username, String password, String port) {
this.username=username;
this.password=password;
setPortNumber(port);
}
public boolean create() {
destroy();
if(!createSchema()) {
return false;
}
try {
createUserInfo();
createIntInfo();
}
catch(Exception e) {
System.out.println(e.toString());
}
try {
updateConnection();
} catch (Exception e) {
System.out.println(e);
}
return true;
}
}