-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccountInfo.java
More file actions
245 lines (218 loc) · 6.78 KB
/
AccountInfo.java
File metadata and controls
245 lines (218 loc) · 6.78 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
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.*;
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 javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.*;
public class AccountInfo {
private static JFrame frame;
private static JPanel panel;
private static JLabel homelabel;
private static JTable table;
private static DefaultTableModel tableModel;
//private JButton button_refresh = new JButton("刷新");
private JButton button_delete = new JButton("刪除");
private JButton button_add = new JButton("增加");
private static JButton button_back;
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";
public ArrayList getAccountInfo() {
Connection conn = null;
Statement stmt = null;
ArrayList alist = new ArrayList();
alist.clear();
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql = "";
sql = "SELECT account ,password,name,major FROM account";
ResultSet rs = stmt.executeQuery(sql);
int count = 0;
while (rs.next()) {
count++;
alist.add(count);
alist.add(rs.getString("account"));
alist.add(rs.getString("password"));
alist.add(rs.getString("name"));
alist.add(rs.getString("major"));
}
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();
}
}
System.out.println("Goodbye!");
return alist;
}
public void setAccountInfo(int a, String[] b) {
Connection conn = null;
Statement stmt = null;
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql = "";
if (a == 1) {
sql = "INSERT INTO account VALUES ('" + b[0] + "', '" + b[1] + "', '" + b[2] + "','" + b[3] + "')";
stmt.executeUpdate(sql);
sql = "CREATE TABLE " + b[0] + "(" + " ques VARCHAR(255), " + " quesver VARCHAR(255), " + " score INT, "
+ " PRIMARY KEY (ques ))";
stmt.executeUpdate(sql);
sql = "INSERT INTO `runoob`.`pretextscore` (`student`) VALUES ('" + b[0] + "')";
stmt.executeUpdate(sql);
} else {
sql = "DELETE FROM account " + "WHERE account ='" + b[0] + "'";
stmt.executeUpdate(sql);
sql = "DROP TABLE " + b[0];
stmt.executeUpdate(sql);
sql = "DELETE FROM pretextscore " + "WHERE student ='" + b[0] + "'";
stmt.executeUpdate(sql);
}
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();
}
}
System.out.println("Goodbye!");
}
public AccountInfo(int a) {
}
public AccountInfo() {
panel = new JPanel();
panel.setBackground(SystemColor.WHITE);
frame = new JFrame("AccountInfo!");
frame.setBounds(50, 50, 1080, 720);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
panel.setLayout(null);
homelabel = new JLabel("AccountInfo");
homelabel.setBounds(10, 20, 150, 25);
panel.add(homelabel);
flush();
// table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
// table.getColumnModel().getColumn(0).setPreferredWidth(80);
// table.setShowGrid(false);
// table.getTableHeader().setResizingAllowed(true);
button_add.setBounds(150, 20, 80, 25);
button_add.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
AddAccount ad = new AddAccount();
flush();
}
});
panel.add(button_add);
button_delete.setBounds(250, 20, 80, 25);
button_delete.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int i = table.getSelectedRow();
if (i != -1) {
int t = JOptionPane.showConfirmDialog(null, "你確認刪除嗎,刪除後不可恢復", null, JOptionPane.YES_NO_OPTION);
switch (t) {
case JOptionPane.YES_OPTION:
String b[] = new String[1];
b[0] = (String) tableModel.getValueAt(i, 1);
System.out.println(b[0]);
setAccountInfo(2, b);
}
}
flush();
}
});
panel.add(button_delete);
// button.setBounds(350, 20, 80, 25);
// button.addMouseListener(new MouseAdapter() {
// public void mouseClicked(MouseEvent e) {
// flush();
// }
// });
// panel.add(button);
button_back = new JButton("返回上一頁");
button_back.setBounds(890, 20, 120, 25);
button_back.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.dispose();
}
});
panel.add(button_back);
frame.setVisible(true);
}
public void flush() {
String[] columns = new String[] { "count", "account", "password", "name", "major" };
tableModel = new DefaultTableModel(columns, 0);
table = new JTable(tableModel);
tableModel.setRowCount(0);
ArrayList alist = new ArrayList();
alist = getAccountInfo();
int num = columns.length;
int count = 0;
for (int i = 0; i < alist.size(); i += num) {
Object[] objs = new Object[num];
for (int j = 0; j < objs.length; j++) {
objs[j] = alist.get(j + count * num);
}
tableModel.addRow(objs);
count++;
}
table.setModel(tableModel);
table = new JTable() {
public boolean isCellEditable(int row, int column) {
return false;
}
};
table.setModel(tableModel);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.getTableHeader().setReorderingAllowed(false);
table.getTableHeader().setResizingAllowed(false);
JScrollPane scrollable = new JScrollPane(table);
scrollable.setBounds(10, 80, 1000, 420);
panel.add(scrollable);
}
}