This repository has been archived by the owner on May 11, 2021. It is now read-only.
forked from JonathanYK/TriviaGameServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mySQLdb.java
113 lines (96 loc) · 4.62 KB
/
mySQLdb.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
package GeneralPackage;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import GeneralPackage.apiImport.question;
public class mySQLdb implements DBAccessInterface {
private static Connection Connection;
@Override
//Basic connection:
public String connect(String IP, String Port, String DBname, String User, String Pass) {
String MySQLConnDetails = "jdbc:mysql://" + IP + ":" + Port + "/" + DBname + "?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"; // mySQL connection method
try {
Connection = DriverManager.getConnection(MySQLConnDetails,User,Pass); //try to connect to the DB using the provided user and pass.
} catch (SQLException e) {
e.printStackTrace();
}
return "The connection was successfully established with mySQL database";
}
@Override
public void query(String query, String action) throws Exception {
String output = "SQL Output error :(";
try {
Statement NewStatement = Connection.createStatement();
if(action == "print") {
ResultSet MyResult = NewStatement.executeQuery(query);
while (MyResult.next()) {
System.out.println("The Question Text Is: " + MyResult.getString("question") + "\nThe Category Of the Question: " + MyResult.getString("category")
+ "\nThe Difficulty Of The Question Is:" + MyResult.getString("difficulty") + "\nThe Correct Answer Is: " + MyResult.getString("correct_answer") + "/The First Incorrect Answer is:" + MyResult.getString("incorrect_answer1")
+ "\nThe Second Incorrect Answer Is: " + MyResult.getString("incorrect_answer2") + "\nThe Third Incorrect Answer Is: " + MyResult.getString("incorrect_answer3") + "\n------------------------");
output = "Results are above";
}
}
if(action == "add") { // add new questions to the db
int myResult = NewStatement.executeUpdate(query);
output = Integer.toString(myResult);
System.out.println("Row added successfuly!");
}
} catch (SQLException err) {
System.out.println("SQL Output error :(");
err.printStackTrace();
throw new Exception();
}
}
public void queryImportlb(String user, String diff, int score) throws Exception {
try {
Statement NewStatement = Connection.createStatement();
int myResult = NewStatement.executeUpdate("INSERT INTO triviagame.leaderboard (username, difficulty, score) VALUES ('"+ user +"' ,'"+ diff +"' ,'"+ score +"')");
System.out.println("Row added successfuly!");
} catch (Exception e) {
e.printStackTrace();
}
}
public ArrayList queryExportlb(String diff) {
ArrayList<leaderboard> arrListlb = new ArrayList<leaderboard>();
try {
Statement NewStatement = Connection.createStatement();
ResultSet MyResult = NewStatement.executeQuery("SELECT * FROM triviagame.leaderboard where difficulty=" + "'" + diff + "'" + " order by score desc limit 10;"); // full query including the difficulty from the user ordered by descending score.
while(MyResult.next()) {
arrListlb.add(new leaderboard(MyResult.getString("username"),MyResult.getString("difficulty"),MyResult.getInt("score"))); // adding the questions to the arrListApi one-by-one.
}
}
catch (Exception e) {
System.out.println("The export of the leaderboard failed! The reason is: \n" + e);
}
return arrListlb;
}
public ArrayList <exportQues> exportQueryApi (String diff) {
//Get question in case 1 to user
ArrayList<exportQues> arrListApi = new ArrayList<exportQues>();
try {
Statement NewStatement = Connection.createStatement();
ResultSet MyResult = NewStatement.executeQuery("SELECT * FROM triviagame.realquestions where difficulty=" + "'" + diff + "'" + "limit 10"); // full query including the difficulty from the user.
while(MyResult.next()) { //add to arraylist
arrListApi.add(new exportQues(MyResult.getString("question"),MyResult.getString("category"),MyResult.getString("difficulty"),MyResult.getString("correct_answer"),MyResult.getString("incorrect_answer1"),MyResult.getString("incorrect_answer2"),MyResult.getString("incorrect_answer3"))); // adding the questions to the arrListApi one-by-one.
}
}
catch (Exception e) {
System.out.println("The export to api failed! The reason: \n" + e);
}
return arrListApi;
}
@Override
public String disconnect() {
String Output = "Disconnection Failed :(";
try {
Connection.close();
Output = "Disconnected";
} catch (SQLException e) {
e.printStackTrace();
}
return Output;
}
}