服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - JAVA教程 - Java+Mysql学生管理系统源码

Java+Mysql学生管理系统源码

2020-05-21 11:48HelloRoot JAVA教程

这篇文章主要为大家详细介绍了Java+Mysql学生管理系统源码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

最近正在学java和数据库,想起以前写的学生管理系统,都是从网上下载,敷衍了事。闲来无事,也就自己写了一个,不过功能实现的不是很多。

开发语言:java; 开发环境:Mysql, java; 开发工具:eclipse
开发此案例,首先得在电脑上有java开发环境和Mysql, java开发环境与Mysql的搭建,就不再叙述了,如果需要,请联系我最下面的联系方式:dingyelf@aliyun.com

此次系统比较简易:数据库中只有一个表:stu;功能:能够对学生增加、删除、修改。

开发步骤:

1.在数据库中建表:

?
1
2
3
4
5
6
7
8
create table stu(
stuId String,
stuName String,
stuSex String,
stuAge int,
stuJG String,
stuDept Sring
);

2.java 代码主要由四个类组成:
Test3包含主函数;StuModel用来刷新、呈现数据库;StuAddDiag用来实现增添读者功能;StuUpDiag是修改学生信息。具体代码如下:
 Test3.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
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
 
public class Test3 extends JFrame implements ActionListener {
 //定义一些控件
 JPanel jp1,jp2;
 JLabel jl1,jl2;
 JButton jb1,jb2,jb3,jb4;
 JTable jt;
 JScrollPane jsp;
 JTextField jtf;
 StuModel sm;
 //定义连接数据库的变量
 Statement stat = null;
 PreparedStatement ps;
 Connection ct = null;
 ResultSet rs = null;
 
 public static void main(String[] args){
 Test3 test3 = new Test3();
 }
 //构造函数
 public Test3(){
 jp1 = new JPanel();
 jtf = new JTextField(10);
 jb1 = new JButton("查询");
 jb1.addActionListener(this);
 jl1 = new JLabel("请输入名字:");
 
 jp1.add(jl1);
 jp1.add(jtf);
 jp1.add(jb1);
 
 jb2 = new JButton("添加");
 jb2.addActionListener(this);
 jb3 = new JButton("修改");
 jb3.addActionListener(this);
 jb4 = new JButton("删除");
 jb4.addActionListener(this);
 
 jp2 = new JPanel();
 jp2.add(jb2);
 jp2.add(jb3);
 jp2.add(jb4);
 
 //创建模型对象
 sm = new StuModel();
 
 
 //初始化
 jt = new JTable(sm);
 
 jsp = new JScrollPane(jt);
 
 //将jsp放入到jframe中
 this.add(jsp);
 this.add(jp1,"North");
 this.add(jp2,"South");
 this.setSize(600, 400);
 //this.setLocation(300, 200);
 this.setDefaultCloseOperation(EXIT_ON_CLOSE);
 this.setVisible(true);
 
 }
 public void actionPerformed(ActionEvent arg0) {
 //判断是哪个按钮被点击
 if(arg0.getSource() == jb1){
 System.out.println("用户希望被查询...");
 //因为把对表的数据封装到StuModel中,可以比较简单的完成查询
 String name = this.jtf.getText().trim();
 //写一个sql语句
 String sql = "select * from stu where stuName = '"+name+"' ";
 //构建一个数据模型类,并更新
 sm = new StuModel(sql);
 //更新jtable
 jt.setModel(sm);
 
 }
 
 //一、弹出添加界面
 else if(arg0.getSource() == jb2){
 System.out.println("添加...");
 StuAddDiag sa = new StuAddDiag(this,"添加学生",true);
 
 //重新再获得新的数据模型,
 sm = new StuModel();
 jt.setModel(sm);
 }else if(arg0.getSource() == jb4){
 //二、删除记录
 //1.得到学生的ID
 int rowNum = this.jt.getSelectedRow();//getSelectedRow会返回给用户点中的行
 //如果该用户一行都没有选,就返回-1
 if(rowNum == -1){
 //提示
 JOptionPane.showMessageDialog(this, "请选中一行");
 return ;
 }
 //得到学术ID
 String stuId = (String)sm.getValueAt(rowNum, 0);
 System.out.println("Id: "+stuId);
 
 //连接数据库,完成删除任务
 try{
 //1.加载驱动
 Class.forName("com.mysql.jdbc.Driver");
 //2.连接数据库
 String url = "jdbc:mysql://localhost:3306/spdb1";
 String user = "root";
 String passwd = "lfdy";
 
 ct = DriverManager.getConnection(url, user, passwd);
 System.out.println("连接成功");
 ps = ct.prepareStatement("delete from stu where stuId = ?");
 ps.setString(1,stuId);
 ps.executeUpdate();
 
 }catch(Exception e){
 e.printStackTrace();
 }finally{
 try{
 if(rs!= null){
 rs.close();
 rs = null;
 
 }
 if(ps!= null){
 ps.close();
 ps = null;
 }
 if(ct != null){
 ct.close();
 ct = null;
 }
 } catch(Exception e){
 e.printStackTrace();
 }
 }
 sm = new StuModel();
 //更新jtable
 jt.setModel(sm);
 }else if(arg0.getSource() == jb3){
 System.out.println("11111");
 //三、用户希望修改
 int rowNum = this.jt.getSelectedRow();
 if(rowNum == -1){
 //提示
 JOptionPane.showMessageDialog(this, "请选择一行");
 return ;
 }
 //显示对话框
 System.out.println( "12435");
 StuUpDiag su = new StuUpDiag(this, "修改学术", true, sm, rowNum);
 sm = new StuModel();
 jt.setModel(sm);
 }
 }
}

StuModel.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
/*
 * 这是我的一个stu表的模型
 * 可以把对学生表的操作全都封装到这个类
 */
package com.test2;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.table.*;
 
public class StuModel extends AbstractTableModel{
 
 //rowData存放行数据,columnNames存放列名
 Vector rowData,columnNames;
 
 //定义连接数据库的变量
 Statement stat = null;
 Connection ct = null;
 ResultSet rs = null;
 
 //初始化
 public void init(String sql){
 if(sql.equals("")){
 sql = "select * from stu";
 }
 //中间
 //设置列名
 columnNames = new Vector();
 columnNames.add("学号");
 columnNames.add("名字");
 columnNames.add("性别");
 columnNames.add("年龄");
 columnNames.add("籍贯");
 columnNames.add("门派");
 
 //rowData存放多行
 rowData = new Vector();
 
 try{
 //1.加载驱动
 Class.forName("com.mysql.jdbc.Driver");
 System.out.println("加载成功");
 //2.连接数据库
 //定义几个常量
 String url = "jdbc:mysql://localhost:3306/spdb1";
 String user = "root";
 String passwd = "lfdy";
 
 ct = DriverManager.getConnection(url,user,passwd);
 stat = ct.createStatement();//创建stat对象
 rs = stat.executeQuery(sql);//查询结果
 
 while(rs.next()){
 Vector hang = new Vector();
 hang.add(rs.getString(1));
 hang.add(rs.getString(2));
 hang.add(rs.getString(3));
 hang.add(rs.getInt(4));
 hang.add(rs.getString(5));
 hang.add(rs.getString(6));
 //加入到rowData中
 rowData.add(hang);
 
 }
 
 }catch(Exception e){
 e.printStackTrace();
 }finally{
 try{
 if(rs!=null){
 rs.close();
 rs = null;
 }
 if(stat != null){
 stat.close();
 stat = null;
 }
 if(ct != null){
 ct.close();
 ct = null;
 }
 }catch(Exception e){
 e.printStackTrace();
 }
 }
 }
 
 //增加学生函数
 public void addStu(String sql){
 //根据用户输入的sql语句,完成添加任务
 
 
 
 }
 
 //第二个构造函数,通过传递的sql语句来获得数据模型
 public StuModel(String sql){
 this.init(sql);
 }
 
 //构造函数,用于初始化我的数据模型(表)
 public StuModel(){
 this.init("");
 }
 
 //得到共有多少行
 public int getRowCount() {
 // TODO Auto-generated method stub
 return this.rowData.size();
 }
 
 //得到共有多少列
 public int getColumnCount() {
 // TODO Auto-generated method stub
 return this.columnNames.size();
 }
 
 //得到某行某列的数据
 public Object getValueAt(int row, int column) {
 // TODO Auto-generated method stub
 return ((Vector)(this.rowData.get(row))).get(column);
 }
 
 //得到属性名字
 public String getColumnName(int column) {
 // TODO Auto-generated method stub
 return (String)this.columnNames.get(column);
 }
}

StuAddDiag.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
package com.test2;
 
import javax.swing.JDialog;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.*;
 
public class StuAddDiag extends JDialog implements ActionListener {
 //定义我需要的swing组件
 JLabel jl1,jl2,jl3,jl4,jl5,jl6;
 JTextField jf1,jf2,jf3,jf4,jf5,jf6;
 JPanel jp1,jp2,jp3;
 JButton jb1,jb2;
 //owner代笔父窗口,title是窗口的名字,modal指定是模式窗口()或者非模式窗口
 public StuAddDiag(Frame owner,String title, boolean modal){
 //调用父类方法
 super(owner,title,modal);
 
 jl1 = new JLabel("学号");
 jl2 = new JLabel("名字");
 jl3 = new JLabel("性别");
 jl4 = new JLabel("年龄");
 jl5 = new JLabel("籍贯");
 jl6 = new JLabel("门派");
 
 jf1 = new JTextField(10);
 jf2 = new JTextField(10);
 jf3 = new JTextField(10);
 jf4 = new JTextField(10);
 jf5 = new JTextField(10);
 jf6 = new JTextField(10);
 
 jb1 = new JButton("添加");
 jb1.addActionListener(this);
 jb2 = new JButton("取消");
 
 jp1 = new JPanel();
 jp2 = new JPanel();
 jp3 = new JPanel();
 
 //设置布局
 jp1.setLayout(new GridLayout(6,1));
 jp2.setLayout(new GridLayout(6,1));
 
 jp3.add(jb1);
 jp3.add(jb2);
 
 jp1.add(jl1);
 jp1.add(jl2);
 jp1.add(jl3);
 jp1.add(jl4);
 jp1.add(jl5);
 jp1.add(jl6);
 
 jp2.add(jf1);
 jp2.add(jf2);
 jp2.add(jf3);
 jp2.add(jf4);
 jp2.add(jf5);
 jp2.add(jf6);
 
 this.add(jp1, BorderLayout.WEST);
 this.add(jp2, BorderLayout.CENTER);
 this.add(jp3, BorderLayout.SOUTH);
 
 this.setSize(300,200);
 this.setVisible(true);
 }
 @Override
 public void actionPerformed(ActionEvent e) {
 // TODO Auto-generated method stub
 if(e.getSource() == jb1){
 Connection ct = null;
 PreparedStatement pstmt = null;
 ResultSet rs = null;
 
 try{
 //1.加载驱动
 Class.forName("com.mysql.jdbc.Driver");
 System.out.println("加载成功");
 //2.连接数据库
 //定义几个常量
 String url = "jdbc:mysql://localhost:3306/spdb1";
 String user = "root";
 String passwd = "lfdy";
 ct = DriverManager.getConnection(url,user,passwd);
 
 //与编译语句对象
 
 String strsql = "insert into stu values(?,?,?,?,?,?)";
 pstmt = ct.prepareStatement(strsql);
 
 //给对象赋值
 pstmt.setString(1,jf1.getText());
 pstmt.setString(2,jf2.getText());
 pstmt.setString(3,jf3.getText());
 pstmt.setString(4,jf4.getText());
 pstmt.setString(5,jf5.getText());
 pstmt.setString(6,jf6.getText());
 
 pstmt.executeUpdate();
 
 this.dispose();//关闭学生对话框
 
 }catch(Exception arg1){
 arg1.printStackTrace();
 }finally{
 try{
 if(rs!=null){
 rs.close();
 rs = null;
 }
 if(pstmt != null){
 pstmt.close();
 pstmt = null;
 }
 if(ct != null){
 ct.close();
 ct = null;
 }
 }catch(Exception arg2){
 arg2.printStackTrace();
 }
 }
 
 }
 
 }
 
 
}

StuUpDiag.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
package com.test2;
/*
 * 修改学生
 */
import javax.swing.JDialog;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.*;
 
public class StuUpDiag extends JDialog implements ActionListener {
 //定义我需要的swing组件
 JLabel jl1,jl2,jl3,jl4,jl5,jl6;
 JTextField jf1,jf2,jf3,jf4,jf5,jf6;
 JPanel jp1,jp2,jp3;
 JButton jb1,jb2;
 //owner代笔父窗口,title是窗口的名字,modal指定是模式窗口()或者非模式窗口
 public StuUpDiag(Frame owner,String title, boolean modal,StuModel sm,int rowNum){
 //调用父类方法
 super(owner,title,modal);
 
 jl1 = new JLabel("学号");
 
 jl2 = new JLabel("名字");
 
 jl3 = new JLabel("性别");
 jl4 = new JLabel("年龄");
 jl5 = new JLabel("籍贯");
 
 
 
 jl6 = new JLabel("门派");
 
 
 jf1 = new JTextField(10);jf1.setText((sm.getValueAt(rowNum, 0)).toString());
 jf2 = new JTextField(10);jf2.setText((String)sm.getValueAt(rowNum, 1));
 jf3 = new JTextField(10);jf3.setText(sm.getValueAt(rowNum, 2).toString());
 jf4 = new JTextField(10);jf4.setText((sm.getValueAt(rowNum, 3)).toString());
 jf5 = new JTextField(10);jf5.setText((String)sm.getValueAt(rowNum, 4));
 jf6 = new JTextField(10);jf6.setText((String)sm.getValueAt(rowNum, 5));
 
 jb1 = new JButton("修改");
 jb1.addActionListener(this);
 jb2 = new JButton("取消");
 
 jp1 = new JPanel();
 jp2 = new JPanel();
 jp3 = new JPanel();
 
 //设置布局
 jp1.setLayout(new GridLayout(6,1));
 jp2.setLayout(new GridLayout(6,1));
 
 jp3.add(jb1);
 jp3.add(jb2);
 
 jp1.add(jl1);
 jp1.add(jl2);
 jp1.add(jl3);
 jp1.add(jl4);
 jp1.add(jl5);
 jp1.add(jl6);
 
 jp2.add(jf1);
 jp2.add(jf2);
 jp2.add(jf3);
 jp2.add(jf4);
 jp2.add(jf5);
 jp2.add(jf6);
 
 this.add(jp1, BorderLayout.WEST);
 this.add(jp2, BorderLayout.CENTER);
 this.add(jp3, BorderLayout.SOUTH);
 
 this.setSize(300,200);
 this.setVisible(true);
 }
 @Override
 public void actionPerformed(ActionEvent e) {
 // TODO Auto-generated method stub
 if(e.getSource() == jb1){
 Connection ct = null;
 PreparedStatement pstmt = null;
 ResultSet rs = null;
 
 try{
 //1.加载驱动
 Class.forName("com.mysql.jdbc.Driver");
 System.out.println("加载成功");
 //2.连接数据库
 //定义几个常量
 String url = "jdbc:mysql://localhost:3306/spdb1";
 String user = "root";
 String passwd = "lfdy";
 ct = DriverManager.getConnection(url,user,passwd);
 
 //与编译语句对象
 
 String strsql = "insert into stu values(?,?,?,?,?,?)";
 pstmt = ct.prepareStatement(strsql);
 
 //给对象赋值
 pstmt.setString(1,jf1.getText());
 pstmt.setString(2,jf2.getText());
 pstmt.setString(3,jf3.getText());
 pstmt.setString(4,jf4.getText());
 pstmt.setString(5,jf5.getText());
 pstmt.setString(6,jf6.getText());
 
 pstmt.executeUpdate();
 
 this.dispose();//关闭学生对话框
 
 }catch(Exception arg1){
 arg1.printStackTrace();
 }finally{
 try{
 if(rs!=null){
 rs.close();
 rs = null;
 }
 if(pstmt != null){
 pstmt.close();
 pstmt = null;
 }
 if(ct != null){
 ct.close();
 ct = null;
 }
 }catch(Exception arg2){
 arg2.printStackTrace();
 }
 }
 
 }
 
 }
 
 
}

开发与测试结果:

1.系统主界面:

Java+Mysql学生管理系统源码

2.按名字查询:

Java+Mysql学生管理系统源码

3.选中一行,删除:

Java+Mysql学生管理系统源码

4.选中一行修改:

Java+Mysql学生管理系统源码

5.点击添加按钮,进行添加:

Java+Mysql学生管理系统源码

后续此系统将继续完善,有疑问和技术交流的,可联系本人:dingyelf@aliyun.com

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

延伸 · 阅读

精彩推荐