-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionDB.java
More file actions
309 lines (252 loc) · 11.5 KB
/
ConnectionDB.java
File metadata and controls
309 lines (252 loc) · 11.5 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
package chat;
import java.sql.*;
public class ConnectionDB {
private static final String dbName = "DB55"; // Input your database name
private static final String dbUser = "G555"; // input user that has access to the database
private static final String dbPassword = "598f4_304"; // input user's password
public static void download() {
Connection dbcon = null;
Statement stmt = null;
ResultSet rs = null;
ResultSet rs1 = null;
String url = "jdbc:sqlserver://sqlserver.dmst.aueb.gr:1433;"
+ "databaseName=" + dbName + ";user=" + dbUser + ";password=" + dbPassword
+ ";encrypt=true;trustServerCertificate=true;";
/* Step 1 -> Dynamically load the driver's class file into memory */
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("connected");
} catch (java.lang.ClassNotFoundException e) {
System.out.println("ClassNotFoundException: " + e.getMessage());
System.exit(0);
}
/*
* Step 2 -> Establish a connection with the database and initializes the
* Connection object (dbcon)
*/
try {
dbcon = DriverManager.getConnection(url);
} catch (SQLException e) {
System.out.println("SQLException: " + e.getMessage());
System.exit(0);
}
/* Execute SQL statements */
try {
stmt = dbcon.createStatement();
rs = stmt.executeQuery("SELECT * FROM UserN");
while (rs.next()) {
String username = rs.getString("username");
String password = rs.getString("pass");
User tempUser = new User(username, password);
Statement stmt2 = dbcon.createStatement();
rs1 = stmt2.executeQuery("SELECT * FROM Answers WHERE username='" + username + "'");
int i = 0;
while (rs1.next()) {
int answerNumber = rs1.getInt("answer");
tempUser.answers[i] = Questions.fullQuestions[i][answerNumber];// eisagwgh apanthswn se pinaka
i++;
}
}
rs.close();
stmt.close();
dbcon.close();
} catch (SQLException e) {
System.out.println("SQLException: " + e.getMessage());
} finally {
try {
dbcon.close();
} catch (SQLException e) {
}
}
}
// BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
public static void uploadCred(User user) {
Connection connection = null;
PreparedStatement preparedStatement = null;
String url = "jdbc:sqlserver://sqlserver.dmst.aueb.gr:1433;"
+ "databaseName=" + dbName + ";user=" + dbUser + ";password=" + dbPassword
+ ";encrypt=true;trustServerCertificate=true;";
try {
// Load the SQLServerDriver class
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// Establish a connection
connection = DriverManager.getConnection(url);
// SQL insert statement
String insertQuery = "INSERT INTO UserN (username, pass) VALUES (?, ?)";
// Create a PreparedStatement with the insert query
preparedStatement = connection.prepareStatement(insertQuery);
// Check if the PreparedStatement is not null
if (preparedStatement != null) {
// Set parameters for the insert (replace with your actual column names and
// values)
preparedStatement.setString(1, user.username);// vazw to username apo tin klasi user
preparedStatement.setString(2, user.password);// password apo user
// Execute the insert
int rowsAffected = preparedStatement.executeUpdate();
// Check the number of rows affected
System.out.println("Rows affected: " + rowsAffected);
} else {
System.out.println("PreparedStatement is null");
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
try {
// Close the PreparedStatement and Connection
if (preparedStatement != null) {
preparedStatement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
public static void updateAns(User user) {
if (exists(user)) {
deleteANS(user);
}
Connection connection = null;
PreparedStatement preparedStatement = null;
String url = "jdbc:sqlserver://sqlserver.dmst.aueb.gr:1433;"
+ "databaseName=" + dbName + ";user=" + dbUser + ";password=" + dbPassword
+ ";encrypt=true;trustServerCertificate=true;";
try {
// Load the SQLServerDriver class
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// Establish a connection
connection = DriverManager.getConnection(url);
// SQL insert statement
String insertQuery = "INSERT INTO Answers (username, answer) VALUES (?, ?)";
// Create a PreparedStatement with the insert query
preparedStatement = connection.prepareStatement(insertQuery);
// Check if the PreparedStatement is not null
if (preparedStatement != null) {
// Set parameters for the insert (replace with your actual column names and
// values)
int rowsAffected;
int total = 0;
int x = User.answersLength;// plithos erwtisewn
int answerNumber = -1;
for (int i = 0; i < x; i++) {
preparedStatement.setString(1, user.username);// vazw to username apo tin klasi user
for (int j = 1; j <= Questions.choices; j++) {
if (user.answers[i].equals(Questions.fullQuestions[i][j])) {
answerNumber = j;
break;
}
}
preparedStatement.setInt(2, answerNumber);// vazw ton pinaka answer[i]
rowsAffected = preparedStatement.executeUpdate();
total += rowsAffected;
}
// Check the number of rows affected
System.out.println("Rows affected: " + total);
} else {
System.out.println("PreparedStatement is null");
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
try {
// Close the PreparedStatement and Connection
if (preparedStatement != null) {
preparedStatement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
public static void deleteANS(User user) {
Connection connection = null;
PreparedStatement preparedStatement = null;
String url = "jdbc:sqlserver://sqlserver.dmst.aueb.gr:1433;"
+ "databaseName=" + dbName + ";user=" + dbUser + ";password=" + dbPassword
+ ";encrypt=true;trustServerCertificate=true;";
try {
// Load the SQLServerDriver class
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// Establish a connection
connection = DriverManager.getConnection(url);
// SQL insert statement
String insertQuery = "DELETE FROM answers WHERE username = ?";
// Create a PreparedStatement with the insert query
preparedStatement = connection.prepareStatement(insertQuery);
// Check if the PreparedStatement is not null
if (preparedStatement != null) {
// Set parameters for the insert (replace with your actual column names and
// values)
int rowsAffected;
preparedStatement.setString(1, user.username);
rowsAffected = preparedStatement.executeUpdate();
System.out.println("Rows affected: " + rowsAffected);
} else {
System.out.println("PreparedStatement is null");
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
try {
// Close the PreparedStatement and Connection
if (preparedStatement != null) {
preparedStatement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static Boolean exists(User user) {
Connection connection = null;
ResultSet rs = null;
String url = "jdbc:sqlserver://sqlserver.dmst.aueb.gr:1433;"
+ "databaseName=" + dbName + ";user=" + dbUser + ";password=" + dbPassword
+ ";encrypt=true;trustServerCertificate=true;";
try {
// Load the SQLServerDriver class
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// Establish a connection
connection = DriverManager.getConnection(url);
// SQL insert statement
String insertQuery = "SELECT COUNT(*) FROM Answers WHERE username = ?";
try (PreparedStatement preparedStatement = connection.prepareStatement(insertQuery)) {
preparedStatement.setString(1, user.username);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet.next()) {
int count = resultSet.getInt(1);
return count > 0;
}
}
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
try {
// Close the PreparedStatement and Connection
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
public static void main(String[] args) {
User.UserList.clear();
User.createDefaultUsers();
User user = new User("eri", "nigga1");
// deleteANS(user);
}
}