-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCloudFile.java
More file actions
162 lines (162 loc) · 6.07 KB
/
Copy pathCloudFile.java
File metadata and controls
162 lines (162 loc) · 6.07 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
package myapp;
import java.sql.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.json.simple.parser.*;
import org.json.simple.*;
import javax.sql.DataSource;
class Quiz {
Connection conn;
String url;
public Quiz() {
try {
HikariConfig config = new HikariConfig();
config.setJdbcUrl(String.format("jdbc:mysql:///%s", "courseSchema"));
config.setUsername("root");
config.setPassword("Adithya");
config.addDataSourceProperty("socketFactory", "com.google.cloud.sql.mysql.SocketFactory");
config.addDataSourceProperty("cloudSqlInstance", "psyched-bonfire-317319:us-central1:curso");
config.addDataSourceProperty("ipTypes", "PUBLIC");
DataSource pool = new HikariDataSource(config);
conn = pool.getConnection();
/*Class.forName("com.mysql.jdbc.GoogleDriver");
url = "jdbc:mysql:///courseSchema?cloudSqlInstance=psyched-bonfire-317319:us-central1:curso&user=root&password=Adithya";
conn = DriverManager.getConnection(url);*/
} catch (Exception e) {
e.printStackTrace();
return;
}
}
public String addQuiz(String quizName) {
try {
PreparedStatement statement = conn.prepareStatement("insert into quizzes (quizName) values (?)");
statement.setString(1,quizName);
statement.executeUpdate();
return "success";
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
}
public String addQuestion(int quizId, String questionText, String option1, String option2,String option3, String option4, int correctOption) {
try {
PreparedStatement statement = conn.prepareStatement("insert into questions(qText,option1,option2,option3,option4,correctOption) values (?,?,?,?,?,?)");
statement.setString(1,questionText);
statement.setString(2,option1);
statement.setString(3,option2);
statement.setString(4,option3);
statement.setString(5,option4);
statement.executeUpdate();
return "success";
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
}
public int getQuizId(String quizName) {
try {
PreparedStatement statement = conn.prepareStatement("select * from quizzes where quizName = ?");
statement.setString(1,quizName);
ResultSet rs = statement.executeQuery();
rs.next();
return rs.getInt("quizId");
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
public int[] getQuestions(int quizId) {
try {
PreparedStatement statement = conn.prepareStatement("select * from quizzesToQuestions where quizId = ? limit 5");
statement.setInt(1,quizId);
ResultSet rs = statement.executeQuery();
ArrayList<Integer> arr = new ArrayList<>();
while(!rs.isLast()) {
rs.next();
arr.add(rs.getInt("qId"));
}
int[] intArr = new int[arr.size()];
arr.toArray(intArr);
return intArr;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public int[] getAllQuestions(int quizId) {
try {
PreparedStatement statement = conn.prepareStatement("select * from quizzesToQuestions where quizId = ?");
statement.setInt(1,quizId);
ResultSet rs = statement.executeQuery();
ArrayList<Integer> arr = new ArrayList<>();
while(!rs.isLast()) {
rs.next();
arr.add(rs.getInt("qId"));
}
int[] intArr = new int[arr.size()];
arr.toArray(intArr);
return intArr;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public JSONObject getQuestionDetails(int qId) {
try {
PreparedStatement statement = conn.prepareStatement("select * from questions where qId = ?");
statement.setInt(1,qId);
JSONObject jObj = new JSONObject();
ResultSet rs = statement.executeQuery();
rs.next();
jObj.put("qId",qId);
jObj.put("option1",rs.getString("option1"));
jObj.put("option2",rs.getString("option2"));
jObj.put("option3",rs.getString("option3"));
jObj.put("option4",rs.getString("option4"));
jObj.put("correctOption",rs.getString("correctOption"));
int[] intArr = new int[arr.size()];
return jObj;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public String removeQuiz(int quizId) {
try {
int[] qIdArr = this.getAllQuestions(quizId);
for(int i = 0; i < qIdArr.length; i++) {
this.removeQuestion(qIdArr[i]);
}
PreparedStatement statement = conn.prepareStatement("delete from questions where qId = ?");
statement.setInt(1,qId);
statement.executeUpdate();
return "success";
} catch(Exception e) {
e.printStackTrace();
return e.getMessage();
}
}
public String removeQuestion(int qId) {
try {
PreparedStatement statement = conn.prepareStatement("delete from questions where qId = ?");
statement.setInt(1,qId);
statement.executeUpdate();
return "success";
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
}
public String close() {
try {
conn.close();
return "success";
} catch (Exception e) {
return "-1" + e.getMessage();
}
}
}