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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章