-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuizDataManager.java
136 lines (119 loc) · 3.63 KB
/
QuizDataManager.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class QuizDataManager {
private Connection con;
public QuizDataManager() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception e) {
System.err.println(e.toString());
}
String url = "jdbc:mysql://isel.cs.unb.ca:3306/cs2043team17aDB";
try {
con = DriverManager.getConnection(url, "cs2043team17a",
"cs2043team17a");
} catch (SQLException e) {
System.err.println("Database connection error.");
}
}
public ArrayList<QuizListObj> getAllQuizzes() {
ArrayList<QuizListObj> quizlist = new ArrayList<QuizListObj>();
try {
Statement st = con.createStatement();
String sqlQuery = "select * from QuizListTable;";
ResultSet rs = st.executeQuery(sqlQuery);
while (rs.next()) {
QuizListObj quiz = new QuizListObj();
quiz.QuizDescription = rs.getString(2);
quiz.QuizType = rs.getString(3);
quizlist.add(quiz);
}
} catch (SQLException e) {
System.err.println("SQL error: getAllQuizzes");
}
return quizlist;
}
public ArrayList<QuestionObj> getAllQuestions() {
ArrayList<QuestionObj> questionlist = new ArrayList<QuestionObj>();
try {
Statement st = con.createStatement();
String sqlQuery = "select * from QuizQuestionTable;";
ResultSet rs = st.executeQuery(sqlQuery);
while (rs.next()) {
QuestionObj question = new QuestionObj();
question.QuestionId = rs.getInt(1);
question.QuizId = rs.getInt(2);
question.Answer1 = rs.getString(3);
question.Answer2 = rs.getString(4);
question.Answer3 = rs.getString(5);
question.Answer4 = rs.getString(6);
question.CorrectAnswer = rs.getString(7);
question.Question = rs.getString(8);
questionlist.add(question);
}
} catch (SQLException e) {
System.err.println("SQL error: getAllQuestions");
}
return questionlist;
}
public void createQuiz(QuestionObj question) {
try {
Statement st = con.createStatement();
String sqlQuery = "insert into QuizQuestionTable(Answer1, Answer2, Answer3, Answer4, CorrectAnswer, Question) values "
+ "('"
+ question.Answer1
+ "', '"
+ question.Answer2
+ "', '"
+ question.Answer3
+ "', '"
+ question.Answer4
+ "', '"
+ question.CorrectAnswer
+ "', '"
+ question.Question + "');";
System.out.println(sqlQuery);
st.executeUpdate(sqlQuery);
} catch (SQLException e) {
System.err.println("SQL error: addQuizInfo");
}
}
public boolean checkCorrectAnswer(int questionid, String answer) {
boolean result = false;
String correctAnswer;
try {
Statement st = con.createStatement();
String sqlQuery = "select * from QuizQuestionTable where QuestionId = "
+ questionid + ";";
ResultSet rs = st.executeQuery(sqlQuery);
correctAnswer = rs.getString(8);
if (correctAnswer.equals(answer)) {
result = true;
} else {
result = false;
}
} catch (SQLException e) {
System.err.println("SQL error: getAllQuizzes");
}
return result;
}
public void addQuizInfo(QuizListObj quiz) {
try {
Statement st = con.createStatement();
String sqlQuery = "insert into QuizListTable(QuizDescription, QuizType) values "
+ "('"
+ quiz.QuizDescription
+ "','"
+ quiz.QuizType
+ "');";
System.out.println(sqlQuery);
st.executeUpdate(sqlQuery);
} catch (SQLException e) {
System.err.println("SQL error: addQuizInfo");
}
}
}