java+SQL做学生信息管理系统(增删改查)

java+SQL做学生信息管理系统(增删改查)

[过程中需要用到的所有工具数据库以及数据库管理器等等]
https://pan.baidu.com/s/1cLKJPKXauLCl-Vwah6wFIQ
提取码:he5x
在这里插入图片描述
大学学习java后做的第一个小项目忍不住分享一下,也是我自己的面向对象编程的实践作业啦,有点水,不是很优。废话不多数,下面进入正题

界面的编写是非常简单的,直接贴代码了,首先看添加功能

Add.java

import javax.swing.;
import java.awt.
;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Add extends JFrame implements ActionListener{
private static final Statement PraparedStatement = null;
Connection con;

JLabel id = new JLabel("Id");
JLabel name = new JLabel("Name");
JLabel sex = new JLabel("sex");
JLabel tel = new JLabel("Tel");
JLabel grade = new JLabel("Grade");
JLabel address = new JLabel("Address");
JLabel birthday = new JLabel("Birthday");

JTextField id1 = new JTextField(10);
JTextField name1 = new JTextField(10);
JTextField sex1 = new JTextField(10);
JTextField tel1 = new JTextField(10);
JTextField grade1 = new JTextField(10);
JTextField address1 = new JTextField(10);
JTextField birthday1 = new JTextField(8);

JButton yes = new JButton("确认");
JButton no  = new JButton("取消");	

	public Add() {
		setTitle("添加学生信息");
		setLayout(null);
		
		id.setBounds(100,90,100,50);
		id1.setBounds(300,100,200,30);
		name.setBounds(100,140,100,50);
		name1.setBounds(300,150,200,30);
		sex.setBounds(100,190,100,50);
		sex1.setBounds(300,200,200,30);
		tel.setBounds(100,240,100,50);
		tel1.setBounds(300,250,200,30);
		grade.setBounds(100, 290, 100, 50);
		grade1.setBounds(300, 300, 200, 30);
		address.setBounds(100, 340, 100, 50);
		address1.setBounds(300,350,200,30);
		birthday.setBounds(100, 390, 100, 50);
		birthday1.setBounds(300, 400, 200, 30);
		
		yes.setBounds(100,450, 100, 60);
		no.setBounds(400, 450, 100,60);
		
		add(id);
		add(id1);
		add(name);
		add(name1);
		add(sex);
		add(sex1);
		add(tel);
		add(tel1);
		add(grade);
		add(grade1);
		add(address);
		add(address1);
		add(birthday);
		add(birthday1);
		
		add(yes);
		add(no);
		
		yes.addActionListener(this);
		no.addActionListener(this);
		
		setVisible(true);
		setBounds(600,300,600,600);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 
	}
	public static void main(String args[]){
	new Add();
	}
	}
	

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

执行以后的界面
这就是执行以后的界面
下面就是按钮功能的实现,同时包含了数据库的操作

将下面的代码直接加在上面的代码的主函数上面就可以,这是上面的按钮的监听器方法

	public void actionPerformed(ActionEvent e) {
				 
	 if(e.getSource()==yes) {								//如果你点击的是确认按钮
		 String s1=id1.getText();   					//获取文本框中的信息
    	 String s2=name1.getText(); 
    	 String s3=sex1.getText();
    	 String s4=tel1.getText();
    	 String s5=grade1.getText();
    	 String s6=address1.getText();
    	 String s7=birthday1.getText();	 
    	 
    	 int  num  = Integer.parseInt(s7);					//将获得的日期转换为整数类型
    	
    	 //加载数据库驱动
	  	 try {					
	    		  Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDrive");
	    	  }
	    	  catch(Exception e1) {
	    		  
	    	  }	    	
	    	 //连接数据库
	    		  String uri ="jdbc:sqlserver://172.16.8.54:1433;DatebaseName=Stu";			//ip改为自己的ip,“Stu”数据库中自己新建的数据库名
	    		  String user = "sa";															//sa为自己的数据库用户名
	    		  String password = "962464";											//自己的数据库密码
	    		 
				try {
					con = DriverManager.getConnection(uri,user,password);
				} catch (SQLException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}
	  
	    	 
	    	  JOptionPane.showMessageDialog(null, "数据库连接成功", "连接数据库",
	    			   JOptionPane.INFORMATION_MESSAGE);
	    	  			
		try {
			if(init()==true) {							//假如添加的id数据库中已经存在,则不添加
					JOptionPane.showMessageDialog(null, "数据库中此 Id 已存在,不可添加", "提示",
					JOptionPane.INFORMATION_MESSAGE);
					dispose();
				}
			else if(init()==false){					//如果没有,则执行添加
					String sql="insert into Students(Id,Name,Sex,Tel,Grade,Address,Birth)"+"values('"+s1+"','"+s2+"','"+s3+"','"+s4+"','"+s5+"','"+s6+"','"+num+"')";											//创建sql语句
					
					 Statement statement = null;
		
				try {
					statement = con.createStatement();
				} catch (SQLException e2) {
					e2.printStackTrace();
				}
					try {
							int result = statement.executeUpdate(sql);				//执行sql语句
						} catch (SQLException e1) {
							e1.printStackTrace();
						}
				    	 
						JOptionPane.showMessageDialog(null, "添加学生信息成功", "完成",
								   JOptionPane.INFORMATION_MESSAGE);
					dispose();
											
				}
		} catch (HeadlessException | SQLException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
		}
	 else if(e.getSource()==no) {
			dispose();
	 }
		}

//判断添加的Id在数据库中是否存在的方法
public boolean init() throws SQLException {

		String sql1="select Id FROM Students where Id='"+s1+"'";	
		
		PreparedStatement pstm = con.prepareStatement(sql1);// 执行查询
		
		ResultSet rs = pstm.executeQuery();			
		
		boolean result = false;
		
		while(rs.next()) {
			
			result = true;				
		}			
		rs = pstm.executeQuery();
		return result;
	}
}

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

添加功能就完成了,大功告成

删除功能,界面百年不变,所以我就只写按钮监听器里面的方法了,直接写个界面加到里面就可以了。

删除功能是按照id删除,需要输入要删除的id
下面是执行删除功能的核心代码,数据库连接等代码都是恒定不变的,就不贴了,学编程绝对不能一味的Ctrl+c;Ctrl+v;一定要自己的思考,亲手敲一遍

Delete.java

String sql=“delete Students where Id =’”+s1+"’";

    	  Statement statement = null;
			
    	  try {
				statement = con.createStatement();
			} catch (SQLException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
	    try {
		     
			 int resultset = statement.executeUpdate(sql);
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}	    	  	    	   
	    
	    JOptionPane.showMessageDialog(null, "删除学生信息成功", "完成",
    			   JOptionPane.INFORMATION_MESSAGE);
    	  
				}	    
    	  }catch (HeadlessException | SQLException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}

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

更新功能

更新功能的实现,我用到了sql中if语句的拼接,从而实现,更新指定标签的内容

Update.java

String sql = “update Students set Id = '”+s1+"’";
String sql1=“where Id=’”+s1+"’";

		if(s2!= null&&!s2.equals("")) sql+=",Name ='"+s2+"'";
				
		if(s3!= null&&!s3.equals("")) sql +=",Sex ='"+s3+"'";	
		
		if(s4!= null&&!s4.equals("")) sql +=",Tel ='"+s4+"'"; 
					 
		if(s5!= null&&!s5.equals("")) sql += ",Grade ='"+s5+"'";
						
		if(s6!= null&&!s6.equals("")) sql +=",Address ='"+s6+"'";
			 
		if(s7!= null&&!s7.equals("")) sql +=",Birth ='"+s7+"'";
		
   		String sql3= sql+sql1;
		Statement statement=null;
		try {
			statement = con.createStatement();
		} catch (SQLException e2) {
			e2.printStackTrace();
		}
		try {
			int result = statement.executeUpdate(sql3);
		} catch (SQLException e1) {				
			e1.printStackTrace();
		}
		
		JOptionPane.showMessageDialog(null, "更新学生信息成功", "完成",
				   JOptionPane.INFORMATION_MESSAGE);

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

查询功能

Select.java

String sql=“select Id,Name,Sex,Tel,Grade,Address,Birth from Stu.Students where ID =’”+Select.id1.getText()+"’";
PreparedStatement pstm = con.prepareStatement(sql);// 执行查询
ResultSet rs = pstm.executeQuery();
// 计算有多少条记录
int count = 0;
while(rs.next()){
count++;
}
rs=pstm.executeQuery();

1
2
3
4
5
6
7
8
9

查询还涉及到最后是用文本区显示还是使用表格显示,这个就需要自己设计了

一定要注意,代码中凡是涉及数据库名,和数据表名字的一定要改成自己的

好了,就分享到这里,欢迎留言,大家探讨。

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值