-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestionBank.java
More file actions
244 lines (192 loc) · 6.56 KB
/
QuestionBank.java
File metadata and controls
244 lines (192 loc) · 6.56 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
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
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 java.util.HashSet;
import java.util.Set;
import javax.swing.*;
public class QuestionBank {
private static JFrame frame;
private static JPanel panel;
private static JScrollPane scrollable;
private static JComboBox jComboBox_subject;
private static JComboBox jComboBox_degree;
private static JButton button_confirm;
private static JLabel showlabel;
private static JLabel homelabel;
private static JLabel label;
private Object selection;
private Object getsubject;
private static Set<QuestionBank> questionRepository = new HashSet<>();// 所有題庫
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/RUNOOB?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
static final String USER = "root";
static final String PASS = "Nknu@123456";
private Integer orderNum;// 題號
private String degree;
private String title;// 題幹
private String[] option;// 選項
private String answer;// 答案
public QuestionBank(String degree, String title, String[] option, String answer) {
this.degree = degree;
this.title = title;
this.option = option;
this.answer = answer;
}
@Override
public int hashCode() {
// 返回题干字符串的 hashcode
return this.title.hashCode();
}
public void setOption(String[] str) {
this.option = str;
}
public String[] getOption() {
return this.option;
}
public void setAnswer(String str) {
this.answer = str;
}
public String getAnswer() {
return this.answer;
}
public void setOrderNum(int num) {
this.orderNum = num;
}
public int getOrderNum() {
return this.orderNum;
}
public String getDegree() {
return this.degree;
}
public String getTitle() {
return this.title;
}
public String setQuestionBank(Object getsubject, Object selection) {
System.out.print(getsubject);
System.out.print(selection);
Connection conn = null;
Statement stmt = null;
questionRepository.removeAll(questionRepository);
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql = "";
if (getsubject.equals("國文")) {
sql = "SELECT Degree, Question, A ,B ,C ,D ,Answer FROM Chinese";
} else if (getsubject.equals("數學")) {
sql = "SELECT Degree, Question, A ,B ,C ,D ,Answer FROM Maths";
} else if (getsubject.equals("英文")) {
sql = "SELECT Degree, Question, A ,B ,C ,D ,Answer FROM english";
}
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
String Degree = rs.getString("Degree");
String Question = rs.getString("Question");
String A = rs.getString("A");
String B = rs.getString("B");
String C = rs.getString("C");
String D = rs.getString("D");
String Answer = rs.getString("Answer");
if (selection.equals("全")) {
questionRepository.add(new QuestionBank(Degree, Question, new String[] { A, B, C, D }, Answer));
} else if (selection.equals("難")) {
if (Degree.equals("3"))
questionRepository.add(new QuestionBank(Degree, Question, new String[] { A, B, C, D }, Answer));
} else if (selection.equals("中")) {
if (Degree.equals("2"))
questionRepository.add(new QuestionBank(Degree, Question, new String[] { A, B, C, D }, Answer));
} else if (selection.equals("易")) {
if (Degree.equals("1"))
questionRepository.add(new QuestionBank(Degree, Question, new String[] { A, B, C, D }, Answer));
}
System.out.print("ok");
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null)
stmt.close();
} catch (SQLException se2) {
}
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
String str = "<html><body>Degree_Ques______A______B______C______D<br>";
ArrayList<QuestionBank> paper = new ArrayList<>(this.questionRepository);
for (int i = 0; i < paper.size(); i++) {
QuestionBank question = paper.get(i);
str = str + question.getDegree() + "_" + question.getTitle();
String[] options = question.getOption();
for (int j = 0; j < options.length; j++) {
str = str + "______" + options[j];
}
str = str + "<br>";
}
str = str + "</body></html>";
System.out.println("Goodbye!");
return str;
}
public QuestionBank() {
panel = new JPanel();
panel.setBackground(SystemColor.WHITE);
frame = new JFrame("QuestionBank!");
frame.setBounds(50, 50, 1080, 720);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
panel.setLayout(null);
homelabel = new JLabel("QuestionBank");
homelabel.setBounds(10, 20, 150, 25);
panel.add(homelabel);
String subject[] = { "國文", "數學", "英文" };
jComboBox_subject = new JComboBox(subject);
jComboBox_subject.setBounds(200, 20, 80, 25);
panel.add(jComboBox_subject);
String degree[] = { "全", "中", "難", "易" };
jComboBox_degree = new JComboBox(degree);
jComboBox_degree.setBounds(300, 20, 80, 25);
panel.add(jComboBox_degree);
button_confirm = new JButton("確定");
button_confirm.setBounds(400, 20, 80, 25);
button_confirm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selection = jComboBox_degree.getItemAt(jComboBox_degree.getSelectedIndex());
getsubject = jComboBox_subject.getItemAt(jComboBox_subject.getSelectedIndex());
String data = "科目: " + getsubject + ", 難易度: " + selection;
showlabel.setText(data);
String str = setQuestionBank(getsubject, selection);
label.setText(str);
}
});
panel.add(button_confirm);
showlabel = new JLabel();
showlabel.setBounds(10, 50, 300, 25);
panel.add(showlabel);
label = new JLabel();
label.setBounds(10, 80, 1000, 540);
panel.add(label);
scrollable = new JScrollPane(label);
scrollable.setBounds(10, 80, 1000, 540);
panel.add(scrollable);
frame.setVisible(true);
}
}