本文實例為大家分享了數據庫版的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
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
|
package Student_system; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import java.util.*; /*class Stu implements java.io.Serializable{ String number,name,specialty,grade,borth,sex; public Stu(){}; public void setNumber(String number){ this.number=number;} public String getNumber(){ return number;} public void setName(String name){ this.name=name;} public String getName(){ return name;} public void setSex(String sex){ this.sex=sex;} public String getSex(){ return sex;} public void setSpecialty(String specialty){ this.specialty=specialty;} public String getSpecialty(){ return specialty;} public void setGrade(String grade){ this.grade=grade;} public String getGrade(){ return grade;} public void setBorth(String borth){ this.borth=borth;} public String getBorth(){ return borth;} }*/ public class StudentSystem extends JFrame{ public static void main(String[] args){ JFrame frame = new JFrame(); frame.setTitle("信息管理系統"); frame.setSize(500, 500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container container = frame.getContentPane(); container.setLayout(new FlowLayout()); JLabel lb=new JLabel("錄入請先輸入記錄,查詢、刪除請先輸入學號,修改是對查詢" + "內容改后的保存!"); final JTextField 學號; final JTextField 姓名; final JTextField 專業; final JTextField 年級; final JTextField 出生; final JRadioButton 男; final JRadioButton 女; ButtonGroup group=null; JButton 錄入,查詢,刪除,修改,顯示; JPanel p1,p2,p3,p4,p5,p6,pv,ph; 學號=new JTextField(10); 姓名=new JTextField(10); 專業=new JTextField(10); 年級=new JTextField(10); 出生=new JTextField(10); group=new ButtonGroup(); 男=new JRadioButton("男"); 女=new JRadioButton("女"); group.add(男); group.add(女); 錄入=new JButton("錄入"); 查詢=new JButton("查詢"); 刪除=new JButton("刪除"); 修改=new JButton("修改"); 顯示=new JButton("顯示"); 修改.setEnabled(false); //添加輸入框及文本框 p1=new JPanel(); p1.add(new JLabel("學號:",JLabel.CENTER)); p1.add(學號); p2=new JPanel(); p2.add(new JLabel("姓名:",JLabel.CENTER)); p2.add(姓名); p3=new JPanel(); p3.add(new JLabel("性別:",JLabel.CENTER)); p3.add(男); p3.add(女); p4=new JPanel(); p4.add(new JLabel("專業:",JLabel.CENTER)); p4.add(專業); p5=new JPanel(); p5.add(new JLabel("年級:",JLabel.CENTER)); p5.add(年級); p6=new JPanel(); p6.add(new JLabel("出生:",JLabel.CENTER)); p6.add(出生); pv=new JPanel(); pv.setLayout(new GridLayout(6,1)); pv.add(p1); pv.add(p2); pv.add(p3); pv.add(p4); pv.add(p5); pv.add(p6); ph=new JPanel(); ph.add(錄入); ph.add(查詢); ph.add(修改); ph.add(刪除); ph.add(顯示); frame.add(lb); frame.add(ph); frame.add(p1); frame.add(p2); frame.add(p3); frame.add(p4); frame.add(p5); frame.add(p6); frame.add(pv); frame.add(ph); class MyListener implements ActionListener { public void actionPerformed (ActionEvent e) { //判斷選中是男是女 /*if(e.getSource().equals(男)){ if(男.isSelected()) { System.out.println("男被選中了"); } else { System.out.println("男被取消選中了"); } } if(e.getSource().equals(女)){ if(女.isSelected()) { System.out.println("女被選中了"); } else { System.out.println("女被取消選中了"); } }*/ //錄入 的功能 if(e.getActionCommand() == "錄入") { String text1 = 學號.getText().trim(); String text2 = 姓名.getText(); String text3 = 專業.getText(); String text4 = 年級.getText(); String text5 = 出生.getText(); String text6 = null; String text7 = "- - - - -我是分割線 - - - - -"; if(男.isSelected()) { text6 = "男"; } if(女.isSelected()) { text6 = "女"; } //用texts包含此次所有錄入信息 String texts = "\n\n學號:" + text1 + "\n\n" + "姓名:" + text2 + "\n\n" + "專業:" + text3 + "\n\n" + "年級:" + text4 + "\n\n" + "出生:" + text5 + "\n\n" + "性別:" + text6 + "\n\n" ; //顯示框 int m = JOptionPane.showConfirmDialog(null, "是否錄入該條記錄:" + texts , "錄入" , JOptionPane.YES_NO_OPTION);//n = 1/0; if(m==0) { insert_Student(text1,text2,text3,text4,text5); } else { JOptionPane.showMessageDialog(null, "已取消該次錄入!!"); } } //顯示 的功能 if(e.getActionCommand() == "顯示") { int n = JOptionPane.showConfirmDialog(null, "是否顯示所有記錄" , "顯示" , JOptionPane.YES_NO_OPTION);//n = 1/0; if(n ==0 ) { try { File file = new File("D:/file.txt"); InputStreamReader reader = new InputStreamReader( new FileInputStream(file));//創建一個輸入流對象 BufferedReader bufferReader = new BufferedReader(reader); String line = ""; String lines = null; line = bufferReader.readLine(); while(line != null) { System.out.println(line); if(lines != null) { lines = lines + "\n" + line + "\n"; } else { lines = line + "\n" ; } line = bufferReader.readLine(); } //JOptionPane.showMessageDialog(null, lines , "顯示",JOptionPane.INFORMATION_MESSAGE); } catch (Exception e1) { e1.printStackTrace(); } } else { JOptionPane.showMessageDialog(null, "已取消該次查詢"); } } //查詢 的功能 if(e.getActionCommand() == "查詢") { File file = new File("D:/file.txt"); BufferedReader reader = null; String text7 = "- - - - -我是分割線 - - - - -"; try { //InputStreamReader reader = new InputStreamReader( // new FileInputStream(file));//創建一個輸入流對象 reader = new BufferedReader(new FileReader(file)); //暫時僅允許查詢學號 String text1 = 學號.getText(); int n = JOptionPane.showConfirmDialog(null, "查詢的學號為:" + text1 , "查詢" , JOptionPane.YES_NO_OPTION);//n = 1/0; if(n == 0){ String line = null; String lines = null; while((line = reader.readLine())!= null) { if(line.equals(text1)) { System.out.println(line); while(!(line.equals(text7))){ line = reader.readLine(); if(lines == null) { lines = line + "\n"; } else { lines = lines + line + "\n"; } System.out.println(line); } } else { continue; } } JOptionPane.showMessageDialog(null, lines , "查詢",JOptionPane.INFORMATION_MESSAGE); reader.close(); } else { JOptionPane.showMessageDialog(null, "已取消查詢功能"); } } catch (Exception e1) { e1.printStackTrace(); } finally { if(reader != null) { try { reader.close(); }catch (IOException e1){ } } } } //刪除 的功能 if(e.getActionCommand() == "刪除") { String text1 = 學號.getText().trim(); delete_Student(text1); } } } //------------注冊監聽------------- MyListener listener = new MyListener(); MyListener listen = new MyListener(); 顯示.addActionListener(listener); 錄入.addActionListener(listener); 查詢.addActionListener(listener); 刪除.addActionListener(listener); 男.addActionListener(listen); 女.addActionListener(listen); frame.setVisible(true); } /* * ---------------------- 功能函數 --------------------- */ //獲取所有的數據 // public static void getAllMessage(){ try { //添加JDBC驅動 Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost/test"; String user = "root"; String password = "root"; Connection connect = DriverManager.getConnection(url,user,password); Statement stmt = connect.createStatement(); System.out.println("success to connect "); //讀取所有的數據 String sql1 = "select * from StudentSystem"; ResultSet rs = stmt.executeQuery(sql1); System.out.println("學號\t姓名\t專業\t年級\t出生"); String lines = null; while(rs.next()) { System.out.print(rs.getString(1)+ "\t"); System.out.print(rs.getString(2)+ "\t"); System.out.print(rs.getString(3)+ "\t"); System.out.print(rs.getString(4)+ "\t"); System.out.print(rs.getString(5)+ "\t"); System.out.println(); if(lines != null){ lines = lines + "學號__姓名__專業__年級__出生\n" + rs.getString(1)+ " "+ rs.getString(2)+ " "+ rs.getString(3) + " "+ rs.getString(4)+ " "+ rs.getString(5)+ " " + "\n\n"; } else { lines ="數據庫中所有數據:\n\n" + "學號__姓名__專業__年級__出生\n" + rs.getString(1)+ ""+ rs.getString(2)+ " "+ rs.getString(3) + " "+ rs.getString(4)+ " "+ rs.getString(5)+ " " + "\n"; } } JOptionPane.showMessageDialog(null, lines , "顯示",JOptionPane.INFORMATION_MESSAGE); } catch (Exception e) { e.printStackTrace(); } } /** * 插入功能 */ public static void insert_Student(String str1,String str2,String str3,String str4,String str5){ try { //添加JDBC驅動 Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost/test"; String user = "root"; String password = "root"; //連接數據庫 Connection connect = DriverManager.getConnection(url,user,password); Statement stmt = connect.createStatement(); System.out.println("success to connect "); /** * */ String lines = null; String sql = "select * from StudentSystem";//要執行的SQL String sql2 = "delete from StudentSystem where id =?"; String sql3 = "insert into StudentSystem(id,name,study,grade,birthplace)VALUES(?,?,?,?,?)";//SQL命令 PreparedStatement pst = (PreparedStatement)connect.prepareStatement(sql3); pst = connect.prepareStatement(sql3); pst.setNString(1,str1);//1,2,3,為對應上面的參數,切記!!!!!這里”12“傳給了第一個問號代表的 ID pst.setString(2,str2); pst.setString(3,str3); pst.setString(4,str4); pst.setString(5,str5); pst.executeUpdate(); JOptionPane.showMessageDialog(null, "該次錄入成功!!"); ResultSet rs = stmt.executeQuery(sql); System.out.println("學號\t姓名\t專業\t年級\t出生"); while(rs.next()) { System.out.print(rs.getString(1)+ "\t"); System.out.print(rs.getString(2)+ "\t"); System.out.print(rs.getString(3)+ "\t"); System.out.print(rs.getString(4)+ "\t"); System.out.print(rs.getString(5)+ "\t"); System.out.println(); if(lines != null){ lines = lines + "學號__姓名__專業__年級__出生\n" + rs.getString(1)+ " "+ rs.getString(2)+ " "+ rs.getString(3) + " "+ rs.getString(4)+ " "+ rs.getString(5)+ " " + "\n\n"; } else { lines ="數據庫中所有數據:\n\n" + "學號__姓名__專業__年級__出生\n" + rs.getString(1)+ ""+ rs.getString(2)+ " "+ rs.getString(3) + " "+ rs.getString(4)+ " "+ rs.getString(5)+ " " + "\n"; } } JOptionPane.showMessageDialog(null, lines , "顯示",JOptionPane.INFORMATION_MESSAGE); } catch (Exception e) { e.printStackTrace(); } } /** * ------------刪除功能------------ */ public static void delete_Student(String str){ try { //添加JDBC驅動 Class.forName( "com.mysql.jdbc.Driver" ); String url = "jdbc:mysql://localhost/test" ; String user = "root" ; String password = "root" ; //連接數據庫 Connection connect = DriverManager.getConnection(url,user,password); Statement stmt = connect.createStatement(); System.out.println( "success to connect " ); //** String lines = null ; String sql = "select * from StudentSystem" ; //要執行的SQL String sql2 = "delete from StudentSystem where id =?" ; PreparedStatement pst = (PreparedStatement)connect.prepareStatement(sql2); pst = connect.prepareStatement(sql2); pst.setString( 1 ,str); pst.executeUpdate(); JOptionPane.showMessageDialog( null , "已刪除該條記錄" , "刪除" ,JOptionPane.INFORMATION_MESSAGE); ResultSet rs = stmt.executeQuery(sql); System.out.println( "學號\t姓名\t專業\t年級\t出生" ); while (rs.next()) { System.out.print(rs.getString( 1 )+ "\t" ); System.out.print(rs.getString( 2 )+ "\t" ); System.out.print(rs.getString( 3 )+ "\t" ); System.out.print(rs.getString( 4 )+ "\t" ); System.out.print(rs.getString( 5 )+ "\t" ); System.out.println(); if (lines != null ){ lines = lines + "學號__姓名__專業__年級__出生\n" + rs.getString( 1 )+ " " + rs.getString( 2 )+ " " + rs.getString( 3 ) + " " + rs.getString( 4 )+ " " + rs.getString( 5 )+ " " + "\n\n" ; } else { lines = "數據庫中所有數據:\n\n" + "學號__姓名__專業__年級__出生\n" + rs.getString( 1 )+ "" + rs.getString( 2 )+ " " + rs.getString( 3 ) + " " + rs.getString( 4 )+ " " + rs.getString( 5 )+ " " + "\n" ; } } JOptionPane.showMessageDialog( null , lines , "顯示" ,JOptionPane.INFORMATION_MESSAGE); } catch (Exception e) { e.printStackTrace(); } } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/shengli_509/article/details/65655138