-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPersonalInfo.java
More file actions
293 lines (266 loc) · 8.1 KB
/
PersonalInfo.java
File metadata and controls
293 lines (266 loc) · 8.1 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class PersonalInfo {
private static JFrame frame;
private static JPanel panel;
private static JLabel passlabel;
private static JLabel namelabel;
private static JLabel majorlabel;
private static JLabel homelabel;
private static JLabel label, label1, label2, label3;
private static JFormattedTextField jformattedtextfield, mjformattedtextfield, njformattedtextfield;
private static JButton button_click;
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 void setMajor(String str) {
Connection conn = null;
Statement stmt = null;
Login login = new Login(2);
String user = login.getUser();
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql = "";
sql = "UPDATE account " + "SET major = '" + str + "' WHERE account = '" + user + "' ";
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 void setName(String str) {
Connection conn = null;
Statement stmt = null;
Login login = new Login(2);
String user = login.getUser();
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql = "";
sql = "UPDATE account " + "SET name = '" + str + "' WHERE account = '" + user + "' ";
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 void setPassword(String str) {
Connection conn = null;
Statement stmt = null;
Login login = new Login(2);
String user = login.getUser();
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql = "";
sql = "UPDATE account " + "SET password = '" + str + "' WHERE account = '" + user + "' ";
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 ArrayList getPersonalInfo() {
Connection conn = null;
Statement stmt = null;
Login login = new Login(2);
String user = login.getUser();
ArrayList alist = new ArrayList();
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 WHERE account = '" + user + "' ";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
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 PersonalInfo() {
panel = new JPanel();
panel.setBackground(SystemColor.WHITE);
frame = new JFrame("AccountInfo!");
frame.setBounds(50, 50, 720, 480);
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);
ArrayList alist = new ArrayList();
alist = getPersonalInfo();
label = new JLabel("帳號: " + alist.get(0).toString());
label.setBounds(10, 50, 150, 25);
panel.add(label);
passlabel = new JLabel("密碼: " + alist.get(1).toString());
passlabel.setBounds(10, 80, 150, 25);
panel.add(passlabel);
label = new JLabel("修改密碼");
label.setBounds(200, 80, 70, 25);
panel.add(label);
jformattedtextfield = new JFormattedTextField();
jformattedtextfield.setBounds(270, 80, 150, 25);
panel.add(jformattedtextfield);
button_click = new JButton("確定");
button_click.setBounds(450, 80, 80, 25);
button_click.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String password = jformattedtextfield.getText();
setPassword(password);
label1.setText("Update!");
passlabel.setText("密碼: " + password);
}
});
panel.add(button_click);
label1 = new JLabel();
label1.setBounds(540, 80, 80, 25);
panel.add(label1);
namelabel = new JLabel("姓名: " + (String) alist.get(2));
namelabel.setBounds(10, 110, 150, 25);
panel.add(namelabel);
label = new JLabel("修改姓名");
label.setBounds(200, 110, 70, 25);
panel.add(label);
njformattedtextfield = new JFormattedTextField();
njformattedtextfield.setBounds(270, 110, 150, 25);
panel.add(njformattedtextfield);
button_click = new JButton("確定");
button_click.setBounds(450, 110, 80, 25);
button_click.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = njformattedtextfield.getText();
setName(name);
label2.setText("Update!");
namelabel.setText("密碼: " + name);
}
});
panel.add(button_click);
label2 = new JLabel();
label2.setBounds(540, 110, 80, 25);
panel.add(label2);
majorlabel = new JLabel("主修: " + (String) alist.get(3));
majorlabel.setBounds(10, 140, 150, 25);
panel.add(majorlabel);
label = new JLabel("修改主修");
label.setBounds(200, 140, 70, 25);
panel.add(label);
mjformattedtextfield = new JFormattedTextField();
mjformattedtextfield.setBounds(270, 140, 150, 25);
panel.add(mjformattedtextfield);
button_click = new JButton("確定");
button_click.setBounds(450, 140, 80, 25);
button_click.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String major = mjformattedtextfield.getText();
setMajor(major);
label3.setText("Update!");
majorlabel.setText("密碼: " + major);
}
});
panel.add(button_click);
label3 = new JLabel();
label3.setBounds(540, 140, 80, 25);
panel.add(label3);
button_back = new JButton("返回上一頁");
button_back.setBounds(570, 20, 120, 25);
button_back.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.dispose();
}
});
panel.add(button_back);
frame.setVisible(true);
}
}