JAVA+MYSQL 实现学生信息管理系统

文章目录

  • 系统要求
  • 实现流程
  • 代码

系统要求

设计一个学生管理系统,能够实现简单的学生管理功能。

数据库stu包含两个表:
user:用户表,字段为(id,password),用于实现登录功能。
student:学生表,字段为(snum,sname,ssex,sbirth,sethnic,syear,shome,smajor,scollege)

此系统功能包括:
登录:根据用户表的 id 和 password 登录。
先登录成功后才能对学生信息进行操作。(即登录成功后才可弹出学生管理系统界面)
查询:根据学号查询信息。
修改:根据学号对学生信息进行修改。
插入:加入新生的信息。
删除:删除指定学号的学生信息。
JAVA+MYSQL 实现学生信息管理系统_第1张图片

JAVA+MYSQL 实现学生信息管理系统_第2张图片
JAVA+MYSQL 实现学生信息管理系统_第3张图片
JAVA+MYSQL 实现学生信息管理系统_第4张图片
JAVA+MYSQL 实现学生信息管理系统_第5张图片
JAVA+MYSQL 实现学生信息管理系统_第6张图片


实现流程

① 在 mysql 建 stu 库,完成后,在 stu 库内建 user 和 student 表。 并在 user 和 student 添加几条记录(填几行信息)。
② 在eclipse创建相应的文件、包、类,并导入 jar 包。(不会导的话百度下)
导入成功后应该会出现这个:
在这里插入图片描述

③ 把几个类的代码写好。


代码

JAVA+MYSQL 实现学生信息管理系统_第7张图片

主类test11:

package 学生信息管理系统;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class J10 extends JFrame{
	J10(){
		setTitle("学生登陆界面");
		setSize(400,150);
		setVisible(true);
	}
}

class J11 extends J10{
	JTextField t1;
	JPasswordField pass;
	J11(){
		t1 = new JTextField(12);//文本框
		pass = new JPasswordField(12);//密码框
		JLabel l1 = new JLabel("账号:"); JLabel l2 = new JLabel("密码:"); //标签对象
		setLayout(new GridLayout(2,1)); //网格布局管理器
		JPanel p1 = new JPanel(); JPanel p2 = new JPanel();
		p1.add(l1); p2.add(l2);
		p1.add(t1); p2.add(pass);
		add(p1); add(p2);
	}
}

class J12 extends J11 implements ActionListener{
	JButton b1,b2;
	Object obj;
	J12(){
		setLayout(new GridLayout(3,1));
		JPanel p3 = new JPanel();
		b1 = new JButton("登录"); b2 = new JButton("注册");
		b1.addActionListener(this);
		b2.addActionListener(this);
		p3.add(b1);p3.add(b2);add(p3);
		}
	
	public void actionPerformed(ActionEvent e) {
		Object obj = e.getSource();
		if(obj == b1){
			String name = this.t1.getText();
			String passw = this.pass.getText();
			boolean t = false;
			try {
				JDB1 x = new JDB1();
				x.OpenConn();
				t = x.login(name, passw);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			if(t == true) 
				{
					System.out.println("登陆成功!");
					new J31().setVisible(true);
				}
			else {
				System.out.println("登陆失败!");
				new J10().setTitle("登录失败!!!");
			}	
		}
	}
}

class J31 extends J10 implements ActionListener{
	JMenuBar mainMenu = new JMenuBar(); //创建菜单栏
	JMenu menuSystem = new JMenu("系统管理");
	JMenuItem itemOpen = new JMenuItem("打开");
	JMenuItem itemExit = new JMenuItem("退出");
	
	JMenu menuStu = new JMenu("学生管理");
	JMenuItem itemAdds = new JMenuItem("增加");
	JMenuItem itemEdits = new JMenuItem("修改");
	JMenuItem itemDeletes = new JMenuItem("删除");
	JMenuItem itemSearch = new JMenuItem("查询");
	
	Font t = new Font("Dialog",0,12);
	
	J31(){
		itemAdds.addActionListener(this);
		itemEdits.addActionListener(this);
		itemDeletes.addActionListener(this);
		itemSearch.addActionListener(this);
		itemExit.addActionListener(this);
		this.pack();
		setTitle("学生管理信息系统主界面"); setSize(400,300);
		menuSystem.setFont(t);
		menuSystem.add(itemOpen); menuSystem.add(itemExit);
		
		menuStu.add(itemAdds); menuStu.add(itemEdits); menuStu.add(itemDeletes); menuStu.add(itemSearch);
		
		mainMenu.add(menuSystem); mainMenu.add(menuStu); 
		this.setJMenuBar(mainMenu);
	}
	public void actionPerformed(ActionEvent e) {
		Object obj = e.getSource();
		if(obj == itemAdds) {
			new add().setVisible(true);
		}
		else if(obj == itemEdits) {
			new alter().setVisible(true);
		}
		else if(obj == itemDeletes) {
			new delete().setVisible(true);
		}
		else if(obj == itemSearch) {
			new select().setVisible(true);
		}
		else if(obj == itemExit) {
			this.dispose();
		}
	}
}

public class test11{
	public static void main(String []args) {
		J12 x = new J12();
		x.setVisible(true);
		x.setResizable(false); 
		x.setLocationRelativeTo(null); //居中
	}
}

JDB1:

package 学生信息管理系统;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JDB1 {
	static Connection conn = null;
	static java.sql.Statement stmt;
	static ResultSet rs;
	static String sql;
	//数据库连接操作方法
	public static void OpenConn() throws Exception{ 
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://localhost:3306/stu?useSSL=false";
			String username = "root"; String password = "123456";//密码是你安装mysql时的密码
			conn = DriverManager.getConnection(url,username,password);
			if(conn != null) System.out.println("数据库连接成功");
		}catch(Exception e) {
			System.err.println("数据库连接:"+e.getMessage()+"\n");
		}
	}
	//信息比对 登录时使用
	 public static boolean login(String lname,String lpassword) throws SQLException {
	        	stmt = conn.createStatement();
	        	sql = "select password from user where id="+"'"+lname+"'";
	        	rs = stmt.executeQuery(sql);
	        	while(rs.next()) {
	        		if(rs.getString("password").equals(lpassword)) {
	        			System.out.println("密码正确!");
	        			return true;
	        		}else {
	        			System.out.println("密码错误!");
	        			return false;
	        		}	
	        	}
	        return false;
	 }
	 //执行数据查询的方法
	 public ResultSet executeQuery(String sql) {
		 stmt = null; rs = null;
		 try {
			 stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
			 rs = stmt.executeQuery(sql);
		 }catch(SQLException e) {
			 System.err.println("查询数据:"+e.getMessage());
		 }
		 return rs;
	 }
	 //执行创建数据表、插入数据等操作
	 public void execute(String sql) {
		 stmt = null; rs = null;
		 try {
			 stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
			 stmt.execute(sql);
		 }catch(SQLException e) {
			 System.err.println(e.getMessage());
		 }
	 }
	 //更新数据库操作方法
	 public void executeUpdate(String sql) {
		 stmt = null; rs = null;
		 try {
			 stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
			 stmt.execute(sql);
			 conn.setAutoCommit(false);
			 conn.commit();//提交
		 }catch(SQLException e) {
			 System.err.println("更新数据"+e.getMessage());
		 }
	 }
	 //关闭statement对象的方法
	 public void closeStmt() {
		 try {
			 stmt.close();
		 }catch(SQLException e){
			 System.err.println("释放对象"+e.getMessage());
		 }
	 }
	 //关闭数据库的方法
	 public void closeConn() {
		 try {
			 conn.close();
		 }catch(SQLException e) {
			 System.err.println("释放对象"+e.getMessage());
		 }
	 }
}

add:

package 学生信息管理系统;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

class add extends J10 implements ActionListener{ //增加
	JButton b3;
	JTextField t1,t2,t3,t4,t5,t6,t7,t8,t9;
	add(){
		setSize(600,300);
		setTitle("增加学生信息");
		t1 = new JTextField(10);
		t2 = new JTextField(10);
		t3 = new JTextField(10);
		t4 = new JTextField(10);
		t5 = new JTextField(10);
		t6 = new JTextField(10);
		t7 = new JTextField(10);
		t8 = new JTextField(10);
		t9 = new JTextField(10);
		
		JLabel l1 = new JLabel("姓名:"); 
		JLabel l2 = new JLabel("学号:"); 
		JLabel l3 = new JLabel("性别:"); 
		JLabel l4 = new JLabel("民族:"); 
		JLabel l5 = new JLabel("学院:"); 
		JLabel l6 = new JLabel("专业:"); 
		JLabel l7 = new JLabel("籍贯:"); 
		JLabel l8 = new JLabel("出生日期:"); 
		JLabel l9 = new JLabel("入学日期:"); 
		
		setLayout(new GridLayout(5,2));
		JPanel p1 = new JPanel();
		JPanel p2 = new JPanel();
		JPanel p3 = new JPanel();
		JPanel p4 = new JPanel();
		JPanel p5 = new JPanel();
		JPanel p6 = new JPanel();
		JPanel p7 = new JPanel();
		JPanel p8 = new JPanel();
		JPanel p9 = new JPanel();
		
		p1.add(l1);p2.add(l2);p3.add(l3);p4.add(l4);p5.add(l5);p6.add(l6);p7.add(l7);p8.add(l8);p9.add(l9);
		p1.add(t1);p2.add(t2);p3.add(t3);p4.add(t4);p5.add(t5);p6.add(t6);p7.add(t7);p8.add(t8);p9.add(t9);
		add(p1);add(p2);add(p3);add(p4);add(p5);add(p6);add(p7);add(p8);add(p9);
		
		JPanel p10 = new JPanel();
		b3 = new JButton("增加"); 
		b3.addActionListener(this);
		p10.add(b3); add(p10);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		Object obj = e.getSource();
		if(obj == b3) {
			String name = this.t1.getText();
			String num = this.t2.getText();
			String sex = this.t3.getText();
			String ethnic = this.t4.getText();
			String college = this.t5.getText();
			String major = this.t6.getText();
			String home = this.t7.getText();
			String birth = this.t8.getText();
			String year = this.t9.getText();
			JDB1 x = new JDB1();
			try {
				x.OpenConn();
				String sql = "select * from student where snum="+num;
				x.rs = x.executeQuery(sql);
				if(x.rs.next()) new J10().setTitle("添加失败!!!");
				else new J10().setTitle("添加成功!!!");
				
				sql = "insert into student(snum,sname,ssex,sbirth,shome,sethnic,syear,smajor,scollege) values("+num+",'"+name+"','"+sex+"','"+birth+"','"+home+"','"+ethnic+"','"+year+"','"+major+"','"+college+"')";
				System.out.println(sql);
				x.execute(sql);
			}catch(Exception e1) {
				System.err.println(e1.getMessage());
			}
			x.closeStmt();
			x.closeConn();
		}	
	}
	public static void main(String []args) {
		//new add().setVisible(true);
	}
}	

delete:

package 学生信息管理系统;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

class delete extends J10 implements ActionListener{ //删除
	JButton b3;
	JTextField t2;
	delete(){
		setSize(600,300);
		setTitle("删除学生信息");
		JTextField t1 = new JTextField(10);
		t2 = new JTextField(10);
		JTextField t3 = new JTextField(10);
		JTextField t4 = new JTextField(10);
		JTextField t5 = new JTextField(10);
		JTextField t6 = new JTextField(10);
		JTextField t7 = new JTextField(10);
		JTextField t8 = new JTextField(10);
		JTextField t9 = new JTextField(10);
		
		JLabel l1 = new JLabel("姓名:"); 
		JLabel l2 = new JLabel("学号:"); 
		JLabel l3 = new JLabel("性别:"); 
		JLabel l4 = new JLabel("民族:"); 
		JLabel l5 = new JLabel("学院:"); 
		JLabel l6 = new JLabel("专业:"); 
		JLabel l7 = new JLabel("籍贯:"); 
		JLabel l8 = new JLabel("出生日期:"); 
		JLabel l9 = new JLabel("入学日期:"); 
		
		setLayout(new GridLayout(5,2));
		JPanel p1 = new JPanel();
		JPanel p2 = new JPanel();
		JPanel p3 = new JPanel();
		JPanel p4 = new JPanel();
		JPanel p5 = new JPanel();
		JPanel p6 = new JPanel();
		JPanel p7 = new JPanel();
		JPanel p8 = new JPanel();
		JPanel p9 = new JPanel();
		
		p1.add(l1);p2.add(l2);p3.add(l3);p4.add(l4);p5.add(l5);p6.add(l6);p7.add(l7);p8.add(l8);p9.add(l9);
		p1.add(t1);p2.add(t2);p3.add(t3);p4.add(t4);p5.add(t5);p6.add(t6);p7.add(t7);p8.add(t8);p9.add(t9);
		add(p1);add(p2);add(p3);add(p4);add(p5);add(p6);add(p7);add(p8);add(p9);
		
		JPanel p10 = new JPanel();
		b3 = new JButton("删除"); 
		b3.addActionListener(this);
		p10.add(b3); add(p10);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		Object obj = e.getSource();
		if(obj == b3) {
			String num = this.t2.getText();
			
			JDB1 x = new JDB1();
			try {
				x.OpenConn();
				String sql = "select * from student where snum="+num;
				x.rs = x.executeQuery(sql);
	            if(!x.rs.next()) setTitle("删除失败!!!");
	            else setTitle("删除成功!!!");
	            
				sql = "delete from student where snum="+num;
				System.out.println(sql);
				x.executeUpdate(sql);
			}catch(Exception e1) {
				System.err.println(e1.getMessage());
			}
			x.closeStmt();
			x.closeConn();
		}
}
	public static void main(String []args) {
		//new delete().setVisible(true);
	}
}	

select:

package 学生信息管理系统;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

class select extends J10 implements ActionListener{ //查询
	JButton b3;
	JTextField t1,t2,t3,t4,t5,t6,t7,t8,t9;
	select(){
		setSize(600,300);
		setTitle("查看学生信息");
		t1 = new JTextField(10);
		t2 = new JTextField(10);
		t3 = new JTextField(10);
		t4 = new JTextField(10);
		t5 = new JTextField(10);
		t6 = new JTextField(10);
		t7 = new JTextField(10);
		t8 = new JTextField(10);
		t9 = new JTextField(10);
		
		JLabel l1 = new JLabel("姓名:"); 
		JLabel l2 = new JLabel("学号:"); 
		JLabel l3 = new JLabel("性别:"); 
		JLabel l4 = new JLabel("民族:"); 
		JLabel l5 = new JLabel("学院:"); 
		JLabel l6 = new JLabel("专业:"); 
		JLabel l7 = new JLabel("籍贯:"); 
		JLabel l8 = new JLabel("出生日期:"); 
		JLabel l9 = new JLabel("入学日期:"); 
		
		setLayout(new GridLayout(5,2));
		JPanel p1 = new JPanel();
		JPanel p2 = new JPanel();
		JPanel p3 = new JPanel();
		JPanel p4 = new JPanel();
		JPanel p5 = new JPanel();
		JPanel p6 = new JPanel();
		JPanel p7 = new JPanel();
		JPanel p8 = new JPanel();
		JPanel p9 = new JPanel();
		
		p1.add(l1);p2.add(l2);p3.add(l3);p4.add(l4);p5.add(l5);p6.add(l6);p7.add(l7);p8.add(l8);p9.add(l9);
		p1.add(t1);p2.add(t2);p3.add(t3);p4.add(t4);p5.add(t5);p6.add(t6);p7.add(t7);p8.add(t8);p9.add(t9);
		add(p1);add(p2);add(p3);add(p4);add(p5);add(p6);add(p7);add(p8);add(p9);
		
		JPanel p10 = new JPanel();
		b3 = new JButton("查找"); 
		b3.addActionListener(this);
		p10.add(b3); add(p10);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		Object obj = e.getSource();
		if(obj == b3) {
			String num = this.t2.getText();
			JDB1 x = new JDB1();
			try {
				x.OpenConn();
				String sql = "select * from student where snum="+num;
				x.rs = x.executeQuery(sql);
				while(x.rs.next()) {
					t1.setText(x.rs.getString("sname"));
					t2.setText(x.rs.getString("snum"));
					t3.setText(x.rs.getString("ssex"));
					t4.setText(x.rs.getString("sethnic"));
					t5.setText(x.rs.getString("scollege"));
					t6.setText(x.rs.getString("smajor"));
					t7.setText(x.rs.getString("shome"));
					t8.setText(x.rs.getString("sbirth"));
					t9.setText(x.rs.getString("syear"));
				}
				
			}catch(Exception e1) {
				System.err.println(e1.getMessage());
			}
			x.closeStmt();
			x.closeConn();
		}	
	}
	public static void main(String []args) {
		//new select().setVisible(true);
	}
}

alter:

package 学生信息管理系统;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

class alter extends J10 implements ActionListener{ //修改
	JButton b3;
	JTextField t1,t2,t3,t4,t5,t6,t7,t8,t9;
	alter(){
		setSize(600,300);
		setTitle("修改学生信息");
		t1 = new JTextField(10);
		t2 = new JTextField(10);
		t3 = new JTextField(10);
		t4 = new JTextField(10);
		t5 = new JTextField(10);
		t6 = new JTextField(10);
		t7 = new JTextField(10);
		t8 = new JTextField(10);
		t9 = new JTextField(10);
		
		JLabel l1 = new JLabel("姓名:"); 
		JLabel l2 = new JLabel("学号:"); 
		JLabel l3 = new JLabel("性别:"); 
		JLabel l4 = new JLabel("民族:"); 
		JLabel l5 = new JLabel("学院:"); 
		JLabel l6 = new JLabel("专业:"); 
		JLabel l7 = new JLabel("籍贯:"); 
		JLabel l8 = new JLabel("出生日期:"); 
		JLabel l9 = new JLabel("入学日期:"); 
		
		setLayout(new GridLayout(5,2));
		JPanel p1 = new JPanel();
		JPanel p2 = new JPanel();
		JPanel p3 = new JPanel();
		JPanel p4 = new JPanel();
		JPanel p5 = new JPanel();
		JPanel p6 = new JPanel();
		JPanel p7 = new JPanel();
		JPanel p8 = new JPanel();
		JPanel p9 = new JPanel();
		
		p1.add(l1);p2.add(l2);p3.add(l3);p4.add(l4);p5.add(l5);p6.add(l6);p7.add(l7);p8.add(l8);p9.add(l9);
		p1.add(t1);p2.add(t2);p3.add(t3);p4.add(t4);p5.add(t5);p6.add(t6);p7.add(t7);p8.add(t8);p9.add(t9);
		add(p1);add(p2);add(p3);add(p4);add(p5);add(p6);add(p7);add(p8);add(p9);
		
		JPanel p10 = new JPanel();
		b3 = new JButton("修改"); 
		b3.addActionListener(this);
		p10.add(b3); add(p10);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		Object obj = e.getSource();
		if(obj == b3) {
			String name = this.t1.getText();
			String num = this.t2.getText();
			String sex = this.t3.getText();
			String ethnic = this.t4.getText();
			String college = this.t5.getText();
			String major = this.t6.getText();
			String home = this.t7.getText();
			String birth = this.t8.getText();
			String year = this.t9.getText();
			JDB1 x = new JDB1();
			try {
				x.OpenConn();
				String sql = "select * from student where snum="+num;
				x.rs = x.executeQuery(sql);
	            if(x.rs.next()) new J10().setTitle("修改成功!!!");
	            else new J10().setTitle("修改失败!!!");
	            
				sql = "update student set sname = '" + name + "', ssex = '" + sex + "', sbirth = '" + birth + "', shome = '" + home + "', sethnic = '" +ethnic + "', syear = '" + year + "', smajor = '" + major + "', scollege = '" + college + "' where snum = " + num;
				System.out.println(sql);
				x.executeUpdate(sql);
			}catch(Exception e1) {
				System.err.println(e1.getMessage());
			}
			x.closeStmt();
			x.closeConn();
		}
	}
	public static void main(String []args) {
		//new alter().setVisible(true);
	}
}	

你可能感兴趣的:(Java,jdbc,java,mysql,数据库,sql)