本文實(shí)例為大家分享了java實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)源碼,供大家參考,具體內(nèi)容如下
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Font; import java.awt.GridLayout; 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 java.util.List; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextArea; import javax.swing.table.AbstractTableModel; import javax.swing.text.BadLocationException; /* DROP DATABASE IF EXISTS `myproject`; CREATE DATABASE myproject DEFAULT CHARSET utf8 COLLATE utf8_general_ci; USE ABC; SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; DROP TABLE IF EXISTS `student`; CREATE TABLE `student` ( `id` varchar(36) NOT NULL, `name` varchar(36) NOT NULL, `age` varchar(36) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; SET FOREIGN_KEY_CHECKS = 1; * * */ public class Test extends JFrame { private static final long serialVersionUID = 1L; private JTable table; private JPanel panel; private JScrollPane scrollpane; private JButton button1, button2, button3; private JTextArea text1, text2, text3; private List<Student> stu; public Test() throws BadLocationException, SQLException { super ( "學(xué)生信息" ); this .setSize( 500 , 340 ); this .add(getJScrollPane(stu), BorderLayout.CENTER); this .add(getJPanel(), BorderLayout.SOUTH); this .setResizable( true ); this .setLocation( 300 , 300 ); this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } // 設(shè)置JScrollPane方法 private JScrollPane getJScrollPane(List<Student> stu) throws SQLException { if (scrollpane == null ) { scrollpane = new JScrollPane(); scrollpane.setViewportView(getJTable(stu)); } return scrollpane; } // 設(shè)置JPanel方法 private JPanel getJPanel() { if (panel == null ) { panel = new JPanel(); panel.setLayout( new GridLayout( 2 , 3 )); text1 = new JTextArea(); text2 = new JTextArea(); text3 = new JTextArea(); button1 = new JButton( "添加" ); button2 = new JButton( "刪除" ); button3 = new JButton( "更新" ); button1.addActionListener( new insert()); button2.addActionListener( new delete()); button3.addActionListener( new update()); text1.setBorder(BorderFactory.createLineBorder(Color.gray, 2 )); text2.setBorder(BorderFactory.createLineBorder(Color.gray, 2 )); text3.setBorder(BorderFactory.createLineBorder(Color.gray, 2 )); text1.setFont( new Font( "宋體" , Font.BOLD, 16 )); text2.setFont( new Font( "宋體" , Font.BOLD, 16 )); text3.setFont( new Font( "宋體" , Font.BOLD, 16 )); text1.setText( "id" ); text2.setText( "name" ); text3.setText( "age" ); panel.add(text1); panel.add(text2); panel.add(text3); panel.add(button1); panel.add(button2); panel.add(button3); } return panel; } // 設(shè)置Jtable方法 private void setJTable(JTable table) { table.setFont( new Font( "宋體" , Font.BOLD, 18 )); table.setRowHeight( 30 ); } // 獲取Jtable對(duì)象方法(該方法具體就是獲得jtable對(duì)象的時(shí)候 一并從數(shù)據(jù)取出學(xué)生信息并放入Jtable表格中) private JTable getJTable(List<Student> stu) throws SQLException { if (table == null ) { JDBCDaoImpl jdbc = new JDBCDaoImpl(); ResultSet rs = jdbc.search(); stu = select(rs); jdbc.closeConnection(); table = new JTable( new Table(stu)); setJTable(table); } return table; } // 設(shè)置學(xué)生信息方法(該方法是用戶增加 刪除 更新用戶操作的具體實(shí)現(xiàn)方法 包含了完整性檢查) private Student setStu() { if (text1.getText().equals( "" ) || text2.getText().equals( "" ) || text3.getText().equals( "" )) { return null ; } else { Student sd = new Student(); sd.setId(text1.getText()); sd.setName(text2.getText()); sd.setAge(text3.getText()); return sd; } } // 重置輸入框?yàn)榭?/code> private void resetText() { text1.setText( "" ); text2.setText( "" ); text3.setText( "" ); } // 刷新學(xué)生信息方法(該方法是重新讀取數(shù)據(jù)庫(kù)學(xué)生的信息 然后返回一個(gè)學(xué)生的集合 用于刷新Jtable表格對(duì)象中的數(shù)據(jù)) private List<Student> select(ResultSet rs) throws SQLException { List<Student> st = new ArrayList<Student>(); while (rs.next()) { Student s = new Student(); s.setId(rs.getString( 1 )); s.setName(rs.getString( 2 )); s.setAge(rs.getString( 3 )); st.add(s); } return st; } // 添加按鈕-監(jiān)聽(tīng)器(該方法是對(duì)添加按鈕實(shí)現(xiàn)的具體方法 ) class insert implements ActionListener { @Override public void actionPerformed(ActionEvent e) { stu = new ArrayList<Student>(); Student sd = new Student(); JDBCDaoImpl jdbc = new JDBCDaoImpl(); sd = setStu(); if (sd != null ) { jdbc.insert(sd); ResultSet rs = jdbc.search(); try { stu = select(rs); } catch (SQLException e1) { e1.printStackTrace(); } jdbc.closeConnection(); JTable table = new JTable( new Table(stu)); //新建一個(gè)Jtable 對(duì)象 用來(lái)盛放增加后的學(xué)生信息 setJTable(table); //設(shè)置Jtable信息 Test. this .scrollpane.setViewportView(table); //把Jtable設(shè)置到Panel resetText(); } else { JOptionPane.showMessageDialog(Test. this , "輸入數(shù)據(jù)不完整" ); } } } // 刪除按鈕-監(jiān)聽(tīng)器(該方法是對(duì)刪除按鈕實(shí)現(xiàn)的具體方法) class delete implements ActionListener { @Override public void actionPerformed(ActionEvent e) { stu = new ArrayList<Student>(); Student sd = new Student(); JDBCDaoImpl jdbc = new JDBCDaoImpl(); sd = setStu(); if (sd != null ) { jdbc.delete(sd); ResultSet rs = jdbc.search(); try { stu = select(rs); } catch (SQLException e1) { e1.printStackTrace(); } jdbc.closeConnection(); JTable table = new JTable( new Table(stu)); //新建一個(gè)Jtable 對(duì)象 用來(lái)盛放增加后的學(xué)生信息 setJTable(table); //設(shè)置Jtable信息 Test. this .scrollpane.setViewportView(table); //把Jtable設(shè)置到Panel resetText(); } else { JOptionPane.showMessageDialog(Test. this , "輸入數(shù)據(jù)不完整" ); } } } // 更新按鈕-監(jiān)聽(tīng)器(該方法是對(duì)更新按鈕實(shí)現(xiàn)的具體方法) class update implements ActionListener { @Override public void actionPerformed(ActionEvent e) { stu = new ArrayList<Student>(); Student sd = new Student(); JDBCDaoImpl jdbc = new JDBCDaoImpl(); sd = setStu(); if (sd != null ) { jdbc.update(sd); ResultSet rs = jdbc.search(); try { stu = select(rs); } catch (SQLException e1) { e1.printStackTrace(); } jdbc.closeConnection(); JTable table = new JTable( new Table(stu)); //新建一個(gè)Jtable 對(duì)象 用來(lái)盛放增加后的學(xué)生信息 setJTable(table); //設(shè)置Jtable信息 Test. this .scrollpane.setViewportView(table); //把Jtable設(shè)置到Panel resetText(); } else { JOptionPane.showMessageDialog(Test. this , "輸入數(shù)據(jù)不完整" ); } } } // Student類 (用于封裝數(shù)據(jù)信息和數(shù)據(jù)庫(kù)表進(jìn)行映射) public class Student { // 學(xué)生的id name age信息 private String id; private String name; private String age; // get&set方法 public String getId() { return id; } public void setId(String id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public String getAge() { return age; } public void setAge(String age) { this .age = age; } } // JTable 表模式類 (JTable對(duì)象 初始化的時(shí)候通過(guò) 這個(gè)Table獲取表格的行數(shù)、列數(shù)、列標(biāo)題、以及每個(gè)單元格存放的數(shù)據(jù) 具體使用原因放在開(kāi)頭的備注了) public class Table extends AbstractTableModel { List<Student> stu = new ArrayList<Student>(); public Table(List s) { this .stu = s; } public List<Student> getStu() { return stu; } public void setStu(List<Student> stu) { this .stu = stu; } @Override // 獲取行數(shù) public int getRowCount() { return stu.size(); } @Override // 獲取列數(shù) public int getColumnCount() { // TODO Auto-generated method stub return 3 ; } @Override public boolean isCellEditable( int rowIndex, int columnIndex) { return true ; } @Override // 獲取列名字 public String getColumnName( int col) { String res = "" ; switch (col) { case 0 : res = "ID" ; break ; case 1 : res = "Name" ; break ; case 2 : res = "Age" ; break ; default : break ; } return res; } @Override // 獲取具體值 public Object getValueAt( int rowIndex, int columnIndex) { // TODO Auto-generated method stub Object res = "" ; Student temp = stu.get(rowIndex); switch (columnIndex) { case 0 : res = temp.getId(); break ; case 1 : res = temp.getName(); break ; case 2 : res = temp.getAge(); break ; default : break ; } return res; } } // JDBCDAO類 配置連接數(shù)據(jù)的信息,鏈接釋放操作和基本增刪改查操作 public class JDBCDaoImpl { String url = "jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8&useSSL=false" ; String user = "root" ; String passwd = "123456" ; Connection conn = null ; Statement stmt = null ; ResultSet rs = null ; // 數(shù)據(jù)庫(kù)連接開(kāi)始 public Connection getConnection() { try { Class.forName( "com.mysql.jdbc.Driver" ); conn = DriverManager.getConnection(url, user, passwd); stmt = conn.createStatement(); } catch (Exception e) { e.printStackTrace(); } return conn; } // 數(shù)據(jù)庫(kù)連接釋放 public void closeConnection() { if (rs != null ) { try { rs.close(); stmt.close(); conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (rs == null ) { try { stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } // 查找操作 public ResultSet search() { getConnection(); try { String sql = "SELECT * FROM student" ; rs = stmt.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } return rs; } // 添加操作 public void insert(Student sd) { // TODO Auto-generated method stub getConnection(); try { String sql = "INSERT INTO student(id,name,age)" + "VALUES('" + sd.getId() + "','" + sd.getName() + "','" + sd.getAge() + "')" ; int count = stmt.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } } // 刪除操作 public void delete(Student sd) { // TODO Auto-generated method stub getConnection(); try { String sql = "DELETE FROM student WHERE id = '" + sd.getId() + "'" ; int count = stmt.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } } // 更新操作 public void update(Student sd) { // TODO Auto-generated method stub getConnection(); try { String sql = "UPDATE student SET name='" + sd.getName() + "',age= '" + sd.getAge() + "'WHERE id = '" + sd.getId() + "'" ; int count = stmt.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } } } // main 方法 public static void main(String[] args) throws BadLocationException, SQLException { new Test().setVisible( true ); } } |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/broccoli2/article/details/73661957