Java + MySQL 图形界面学生管理系统

项目分为六个模块(类),实现了对学生信息的增删改查功能,分别如下:

  1. Main.java:放main函数,是程序的入口。
  2. StuAdd.java:添加学生记录模块。
  3. StuDel.java:删除学生记录。
  4. StuModel.java:数据操作模块,决定显示在窗口的数据。
  5. StuUp.java:修改学生记录。
  6. WindowOfStudent.java:定义项目主窗口,以及对按钮的监听。

创建的数据表如下:

create table stu 
(
id char(20) primary key,
name char(20),
sex char(2),
age int,
qq char(20)
);

Main.java代码如下:

package com.dlnu.student;

public class Main {

	public static void main(String[] args) {
		WindowOfStudent win = new WindowOfStudent();
		win.setBounds(200,200,600,400);
		win.setTitle("学生管理系统");
	}

}

StuAdd.java代码如下:

package com.dlnu.student;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

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

import com.mysql.jdbc.Connection;

public class StuAdd extends JDialog implements ActionListener {
	
	private static final long serialVersionUID = 1L;
	
	JLabel jlId,jlName,jlSex,jlAge,jlQQ;
	JTextField jtfId,jtfName,jtfSex,jtfAge,jtfQQ;
	JPanel jp1,jp2,jp3;
	JButton submit,cancel;
	
	public StuAdd(Frame owner,String title,boolean modal) {
		super(owner, title, modal);
		
		jlId = new JLabel("学号");
		jlName = new JLabel("名字");
		jlSex = new JLabel("性别");
		jlAge = new JLabel("年龄");
		jlQQ = new JLabel("QQ");
		
		jtfId = new JTextField(10);
		jtfName = new JTextField(10);
		jtfSex = new JTextField(10);
		jtfAge = new JTextField(10);
		jtfQQ = new JTextField(10);
		
		submit = new JButton("添加");
		//监听submit按钮
		submit.addActionListener(this);
		cancel = new JButton("取消");
		cancel.addActionListener(this);
		
		jp1 = new JPanel();
		jp2 = new JPanel();
		jp3 = new JPanel();
		
		//设置布局
		jp1.setLayout(new GridLayout(6,2));
		jp2.setLayout(new GridLayout(6,2));
		
		jp3.add(submit);
		jp3.add(cancel);
		
		jp1.add(jlId);
		jp1.add(jlName);
		jp1.add(jlSex);
		jp1.add(jlAge);
		jp1.add(jlQQ);
		
		jp2.add(jtfId);
		jp2.add(jtfName);
		jp2.add(jtfSex);
		jp2.add(jtfAge);
		jp2.add(jtfQQ);
		
		add(jp1,BorderLayout.WEST);
		add(jp2,BorderLayout.CENTER);
		add(jp3,BorderLayout.SOUTH);
		
		setBounds(500,400,300,200);
		setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		//提交按钮被点击
		if (e.getSource() == submit) {
			Connection conn = null;
			PreparedStatement pstmt = null;
			ResultSet rs = null;
			
			try {
				//加载驱动
				Class.forName("com.mysql.jdbc.Driver");
				
				String url = "jdbc:mysql://localhost:3306/my?useSSL=false";
				String user = "root";
				String password = "123456";
				
				conn = (Connection) DriverManager.getConnection(url,user,password);
				
				String insert = "insert into stu values(?,?,?,?,?)";
				
				pstmt = conn.prepareStatement(insert);
				
				//获取输入框数据,并插入到数据库中
				pstmt.setString(1, jtfId.getText());
				pstmt.setString(2, jtfName.getText());
				pstmt.setString(3, jtfSex.getText());
				//int age = Integer.parseInt(jtfAge.getText());
				pstmt.setInt(4,Integer.parseInt(jtfAge.getText()));
				pstmt.setString(5, jtfQQ.getText());
				//存入数据库
				pstmt.executeUpdate();
				//关闭对话框
				dispose();
				
			} catch(Exception e1) {
				e1.printStackTrace();
			} finally {
				try {
					if (rs != null) {
						rs.close();
						rs = null;
					}
					
					if (pstmt != null) {
						pstmt.close();
						pstmt = null;
					}
					
					if (conn != null) {
						conn.close();
						conn = null;
					}
				} catch(Exception e2) {
					e2.printStackTrace();
				}
			}
		} else if (e.getSource() == cancel) {
			dispose();
		}
	}

}

StuDel.java代码如下:

package com.dlnu.student;

import java.sql.DriverManager;
import java.sql.PreparedStatement;

import com.mysql.jdbc.Connection;

public class StuDel {
	Connection conn = null;
	PreparedStatement pstmt = null;
	
	int rowNum;
	String id;
	
	public StuDel(int rowNum, StuModel sm) {
		this.rowNum = rowNum;
		id = sm.getValueAt(rowNum, 0).toString();
	}
	
	public void deleteStudent() {
		try {
			// 加载驱动
			Class.forName("com.mysql.jdbc.Driver");

			String url = "jdbc:mysql://localhost:3306/my?useSSL=false";
			String user = "root";
			String password = "123456";

			conn = (Connection) DriverManager.getConnection(url, user, password);
			
			String del = "delete from stu where id = ?";
			pstmt = conn.prepareStatement(del);
			pstmt.setString(1, id);
			pstmt.execute();

		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (pstmt != null) {
					pstmt.close();
					pstmt = null;
				}

				if (conn != null) {
					conn.close();
					conn = null;
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}
} 

StuModel.java代码如下:

package com.dlnu.student;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class StuModel extends AbstractTableModel {

	private static final long serialVersionUID = 1L;
	
	Vector rowData;	//存放数据行
	Vector columnNames;	//存放列名
	
	//数据库连接变量
	Statement stmt = null;
	Connection conn = null;
	ResultSet rs = null;
	
	String sql;
	
	
	public StuModel(String sql) {
		this.sql = sql;
		init();
	}
	
	//用于初始化表
	public StuModel() {
		this.sql = "select * from stu";
		init();
	}

	private void init() {
		columnNames = new Vector();
		//设置列名
		columnNames.add("学号");
		columnNames.add("姓名");
		columnNames.add("性别");
		columnNames.add("年龄");
		columnNames.add("QQ");
		
		//获取数据行
		rowData = new Vector();
		
		try {
			//加载驱动
			Class.forName("com.mysql.jdbc.Driver");
			
			String url = "jdbc:mysql://localhost:3306/my?useSSL=false";
			String user = "root";
			String password = "123456";
			
			conn = (Connection) DriverManager.getConnection(url, user, password);
			stmt = (Statement) conn.createStatement();
			rs = stmt.executeQuery(sql);
			
			//获取查询数据,并放入vector
			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.getString(4));
				hang.add(rs.getString(5));
				//放入rowData中,相当于二维数组
				rowData.add(hang);
			}
			
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				
				if (stmt != null) {
					stmt.close();
					stmt = null;
				}
				
				if (conn != null) {
					conn.close();
					conn = null;
				}
				
			} catch(Exception e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public int getRowCount() {
		return rowData.size();
	}

	@Override
	public int getColumnCount() {
		return columnNames.size();
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		return ((Vector)(rowData.get(rowIndex))).get(columnIndex);
	}
	
	@Override
	public String getColumnName(int columnIndex) {
		return (String)columnNames.get(columnIndex);
	}
}

StuUp.java代码入下:

package com.dlnu.student;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

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

import com.mysql.jdbc.Connection;

public class StuUp extends JDialog implements ActionListener {

	private static final long serialVersionUID = 1L;

	JLabel jlId, jlName, jlSex, jlAge, jlQQ;
	JTextField jtfId, jtfName, jtfSex, jtfAge, jtfQQ;
	JPanel jp1, jp2, jp3;
	JButton submit, cancel;
	
	String id;

	public StuUp(Frame owner, String title, boolean modal, StuModel sm, int rowNum) {
		super(owner, title, modal);

		jlId = new JLabel("学号");
		jlName = new JLabel("名字");
		jlSex = new JLabel("性别");
		jlAge = new JLabel("年龄");
		jlQQ = new JLabel("QQ");

		jtfId = new JTextField(10);
		jtfName = new JTextField(10);
		jtfSex = new JTextField(10);
		jtfAge = new JTextField(10);
		jtfQQ = new JTextField(10);

		// 显示选择的行数据
		jtfId.setText(sm.getValueAt(rowNum, 0).toString());
		
		id = sm.getValueAt(rowNum, 0).toString();
		
		jtfName.setText(sm.getValueAt(rowNum, 1).toString());
		jtfSex.setText(sm.getValueAt(rowNum, 2).toString());
		jtfAge.setText(sm.getValueAt(rowNum, 3).toString());
		jtfQQ.setText(sm.getValueAt(rowNum, 4).toString());

		submit = new JButton("修改");
		// 监听submit按钮
		submit.addActionListener(this);
		cancel = new JButton("取消");
		cancel.addActionListener(this);

		jp1 = new JPanel();
		jp2 = new JPanel();
		jp3 = new JPanel();

		// 设置布局
		jp1.setLayout(new GridLayout(6, 2));
		jp2.setLayout(new GridLayout(6, 2));

		jp3.add(submit);
		jp3.add(cancel);

		jp1.add(jlId);
		jp1.add(jlName);
		jp1.add(jlSex);
		jp1.add(jlAge);
		jp1.add(jlQQ);

		jp2.add(jtfId);
		jp2.add(jtfName);
		jp2.add(jtfSex);
		jp2.add(jtfAge);
		jp2.add(jtfQQ);

		add(jp1, BorderLayout.WEST);
		add(jp2, BorderLayout.CENTER);
		add(jp3, BorderLayout.SOUTH);

		setBounds(500,400,300,200);
		setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// 提交按钮被点击
		if (e.getSource() == submit) {
			Connection conn = null;
			PreparedStatement pstmt = null;
			ResultSet rs = null;

			try {
				// 加载驱动
				Class.forName("com.mysql.jdbc.Driver");

				String url = "jdbc:mysql://localhost:3306/my?useSSL=false";
				String user = "root";
				String password = "123456";

				conn = (Connection) DriverManager.getConnection(url, user, password);
				
				//String id = sm.getValueAt(rowNum, 0).toString();
				String insert = "update stu set name=?,sex=?,age=?,qq=? where id='" + id + "'";

				pstmt = conn.prepareStatement(insert);

				// 获取输入框数据,并插入到数据库中
				//pstmt.setString(1, jtfId.getText());
				pstmt.setString(1, jtfName.getText());
				pstmt.setString(2, jtfSex.getText());
				// int age = Integer.parseInt(jtfAge.getText());
				pstmt.setInt(3, Integer.parseInt(jtfAge.getText()));
				pstmt.setString(4, jtfQQ.getText());
				// 存入数据库
				pstmt.executeUpdate();
				// 关闭对话框
				dispose();

			} catch (Exception e1) {
				e1.printStackTrace();
			} finally {
				try {
					if (rs != null) {
						rs.close();
						rs = null;
					}

					if (pstmt != null) {
						pstmt.close();
						pstmt = null;
					}

					if (conn != null) {
						conn.close();
						conn = null;
					}
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
		} else if (e.getSource() == cancel) {
			dispose();
		}
	}
}

WindowOfStudent.java代码如下:

package com.dlnu.student;

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

public class WindowOfStudent extends JFrame implements ActionListener{ 
	
	private static final long serialVersionUID = 1L;
	
	//定义控件
	JPanel jp1,jp2;
	JLabel jl1,jl2;
	JButton jb1,jb2,jb3,jb4;
	JTable jt;	//表格
	JScrollPane jsp;
	JTextField jtf;
	
	StuModel sm;
	//StuListen listener;
	
	public WindowOfStudent() {
		init();
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	private void init() {
		//listener = new StuListen();
		
		jp1 = new JPanel();
		jtf = new JTextField(15);
		jb1 = new JButton("查询");
		//监听jb1按钮
		jb1.addActionListener(this);
		
		jl1 = new JLabel("请输入学号:");	//按学号查询
		
		jp1.add(jl1);
		jp1.add(jtf);
		jp1.add(jb1);
		
		jb2 = new JButton("添加");
		//插入监听jb2按钮代码
		jb2.addActionListener(this);
		jb3 = new JButton("修改");
		//插入监听jb3按钮代码
		jb3.addActionListener(this);
		jb4 = new JButton("删除");
		//插入监听jb4按钮代码
		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);
		
		this.add(jsp);
		this.add(jp1,"North");
		this.add(jp2,"South");
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		//查询
		if (e.getSource() == jb1) {
			String id = jtf.getText().trim();
			String sql;
			
			if (id.equals("")) {
				sql = "select * from stu";
			} else {
				sql = "select * from stu where id='" + id + "'";
			}
			
			//查询
			sm = new StuModel(sql);
			//刷新jtable
			jt.setModel(sm);
		} 
		//添加
		else if (e.getSource() == jb2) {
			StuAdd sa = new StuAdd(this,"添加学生",true);
			sm = new StuModel();
			jt.setModel(sm);
		} 
		//修改
		else if (e.getSource() == jb3) {
			int rowNum = jt.getSelectedRow();
			if (rowNum == -1) {
				JOptionPane.showMessageDialog(this, "请选择一行");
				return;
			}
			
			StuUp su = new StuUp(this,"修改学生信息",true,sm,rowNum);
			sm = new StuModel();
			jt.setModel(sm);
		}
		//删除
		else if (e.getSource() == jb4) {
			int rowNum = jt.getSelectedRow();
			if (rowNum == -1) {
				JOptionPane.showMessageDialog(this, "请选择一行");
				return;
			}
			StuDel sd = new StuDel(rowNum,sm);
			sd.deleteStudent();
			
			sm = new StuModel();
			jt.setModel(sm);
		}
	}
}











这个项目也不能算完全独立完成的,还是参考了别人的代码,下面附上我参考的代码的网址:

https://www.cnblogs.com/lfdy/p/5619450.html

你可能感兴趣的:(Java + MySQL 图形界面学生管理系统)