Java学生信息管理系统设计(数据库版)

时间:2022-09-23 15:46:32

本文实例为大家分享了数据库版的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