Java 学生信息管理系统

关注博主不迷路,博主带你码代码!

1. 项目概述

这是一个学生信息管理系统,学生信息主要包括:学号,姓名,性别,出生日期,班级;暂时就只有这些信息,若后续需要,可以相应的进行添加。

该项目主要采用的技术是 Java GUI 设计,这个地方主要用 Java 工具 windowsbuilder 进行布局,(windowsbuilder没有安装或者不会安装的,可以看看我的这篇博客 Eclipse 安装 windowbuilder插件 ),MySql 数据库(MySql 数据库没有安装的 或者数据库使用软件 Navicat 没有安装的可以看看我的这篇博客 2021 MySql 8.0.26 安装教程(最详细的傻瓜教程) ),以及使用 Java 中的 JDBC 将数据库链接起来。

2. 数据库的建立

由于在项目中没有创建学生数据库的功能,所以学生数据库必须要先建立好,下面给出如何建立好MySQL学生数据库的相关代码 + 图片

-- 创建数据库CREATE DATABASE niitDEFAULT CHARACTER SET utf8mb4;SHOW DATABASES;-- 使用数据库niitUSE niit;-- 创建表StudentCREATE TABLE Student(ID INT PRIMARY KEY,NAME VARCHAR(4),GENDER CHAR(1),DOBDATE,BATCH INT);-- 插入记录INSERT INTO Student VALUES(1,独孤求败,,1988-1-2,1),(2,周芷若,,1988-12-3,1),(3,金毛狮王,,1978-11-1,1),(4,蒙挚,,1989-2-5,2),(5,夏春,,1998-4-3,2),(6,梅长苏,,1999-12-6,2);-- 查看表中所有记录 语法:select * from 表名SELECT * FROM Student;

3. 项目页面

主页面

添加学生

修改信息

删除学生

查询信息

4. 项目代码 + 分析

主页面

package studentsmanager;import java.awt.Color;import java.awt.EventQueue;import java.awt.Font;import javax.swing.JButton;import javax.swing.JFrame;importjavax.swing.JLabel;import javax.swing.JPanel;import javax.swing.border.EmptyBorder;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;public class MainFrame extends JFrame {private JPanel contentPane;/*** Launch the application.*/// 有了main函数才可以单独运行public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {MainFrame frame = new MainFrame();frame.setVisible(true);} catch(Exception e) {e.printStackTrace();}}});}/*** Create the frame.*/public MainFrame() {// 设置大小不可改变setResizable(false);// 设置标题setTitle("学生管理系统");// 由于这是主页面,所有当主页面关闭的时候,程序就直接退出setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// setBounds(int x,int y,int width,int height);// x 和 y 是窗口打开时的位置// width 和 height 是窗口打宽度和高度setBounds(100, 100, 450, 350);// setLocationRelativeTo(c)// 设置窗口相对于 c 的位置,当 c 为空或者 null 时,默认为是相对于屏幕中央setLocationRelativeTo(null);// 实例化一个 panecontentPane = new JPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));setContentPane(contentPane);contentPane.setLayout(null);JLabel lblNewLabel = new JLabel("学生管理系统");lblNewLabel.setForeground(Color.RED);lblNewLabel.setFont(newFont("宋体", Font.BOLD, 25));lblNewLabel.setBounds(140, 10, 163, 44);contentPane.add(lblNewLabel);JButton addButton = new JButton("添加学生");// addActionListener 注册监听器addButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {// 设置新窗口可见new AddFrame().setVisible(true);}});addButton.setFont(new Font("宋体", Font.PLAIN, 18));addButton.setBounds(167, 64, 114, 37);contentPane.add(addButton);JButton changeButton =new JButton("修改信息");changeButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {// 同上newChangeFrame().setVisible(true);}});changeButton.setFont(new Font("宋体", Font.PLAIN, 18));changeButton.setBounds(167, 121, 114, 37);contentPane.add(changeButton);JButton deleteButton =new JButton("删除学生");deleteButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {// 同上new DeleteFrame().setVisible(true);}});deleteButton.setFont(new Font("宋体", Font.PLAIN, 18));deleteButton.setBounds(167, 180, 114, 37);contentPane.add(deleteButton);JButton queryButton = new JButton("查询信息");queryButton.addActionListener(newActionListener() {public void actionPerformed(ActionEvent e) {// 同上new QueryFrame().setVisible(true);}});queryButton.setFont(new Font("宋体", Font.PLAIN,18));queryButton.setBounds(167, 240, 114, 37);contentPane.add(queryButton);}}

添加学生

package studentsmanager;import java.awt.EventQueue;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.sql.SQLException;import javax.swing.BoxLayout;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.border.EmptyBorder;public class AddFrame extends JFrame {private JPanel contentPane;private JTextField idField;private JTextField nameField;private JTextField genderField;private JTextField dobField;private JTextField batchField;/*** Launch the application.*/public static void main(String[] args) {EventQueue.invokeLater(newRunnable() {public void run() {try {AddFrame frame = new AddFrame();frame.setVisible(true);} catch(Exception e) {e.printStackTrace();}}});}/*** Create the frame.*/public AddFrame() {// 和前面类似setResizable(false);setTitle("添加学生");// 这个地方,添加学生只是这个程序的一部分,所以当关闭这部分的时候,程序不直接退出,只是关闭该部分程序setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);setBounds(100, 100, 450, 470);setLocationRelativeTo(null);contentPane =new JPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));setContentPane(contentPane);contentPane.setLayout(newBoxLayout(contentPane, BoxLayout.Y_AXIS));JPanel panel =new JPanel();contentPane.add(panel);panel.setLayout(null);JLabel TitleLabel = new JLabel("请输入新学生的信息:");TitleLabel.setFont(new Font("宋体", Font.BOLD, 20));TitleLabel.setBounds(71, 34, 208, 45);panel.add(TitleLabel);JLabel idLabel =new JLabel("学号:");idLabel.setFont(new Font("宋体", Font.PLAIN, 16));idLabel.setBounds(71, 105, 50, 30);panel.add(idLabel);idField = new JTextField();idField.setBounds(143, 99, 240, 45);panel.add(idField);idField.setColumns(10);JLabel nameLabel = new JLabel("姓名:");nameLabel.setFont(new Font("宋体", Font.PLAIN, 16));nameLabel.setBounds(71, 160, 50, 30);panel.add(nameLabel);nameField = newJTextField();nameField.setColumns(10);nameField.setBounds(143, 154, 240, 45);panel.add(nameField);JLabel genderLabel = new JLabel("性别:");genderLabel.setFont(new Font("宋体", Font.PLAIN, 16));genderLabel.setBounds(71, 215, 50, 30);panel.add(genderLabel);genderField = newJTextField();genderField.setColumns(10);genderField.setBounds(143, 209, 240, 45);panel.add(genderField);JLabel dobLabel = new JLabel("出生日期:");dobLabel.setFont(new Font("宋体", Font.PLAIN, 16));dobLabel.setBounds(41, 270, 80, 30);panel.add(dobLabel);dobField = newJTextField();dobField.setColumns(10);dobField.setBounds(143, 264, 240, 45);panel.add(dobField);JLabel batchLabel = new JLabel("班级:");batchLabel.setFont(newFont("宋体", Font.PLAIN, 16));batchLabel.setBounds(71, 325, 50, 30);panel.add(batchLabel);batchField = newJTextField();batchField.setColumns(10);batchField.setBounds(143, 319, 240, 45);panel.add(batchField);JButton addButton = new JButton("添加");// 添加鼠标监听事件addButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {// 先取出数据// getText() 返回字符串类型的// Integer.parseInt 数据类型转换intid = Integer.parseInt(idField.getText());String nameString = nameField.getText();String genderString = genderField.getText();String dobfieldString = dobField.getText();int batch = Integer.parseInt(batchField.getText());// 输出一下学生的信息,方便修改System.out.println(id + "\t" + nameString + "\t" + genderString + "\t"+ dobfieldString +"\t" + batch);// SQLHelp 是自己写的一个工具类SQLHelp sqlHelp = new SQLHelp();try {// 调用添加学生信息的方法sqlHelp.addStudent(id, nameString, genderString, dobfieldString, batch);// 弹出对话框,提示用户添加成功JOptionPane.showMessageDialog(AddFrame.this, "添加成功!");} catch (SQLException e1) {// 这里捕获一下异常,因为学生的学号是唯一的,所以当学号已经存在的时候,提示用户,该学生信息已存在// 异常代码为 23000if(e1.getSQLState().equals("23000")) {JOptionPane.showMessageDialog(AddFrame.this, "添加失败!该学生已存在");}e1.printStackTrace();}}});addButton.setFont(new Font("宋体", Font.PLAIN, 18));addButton.setBounds(182, 389, 97, 33);panel.add(addButton);}}

添加成功:

添加失败:

修改信息

package studentsmanager;import java.awt.EventQueue;import java.awt.Font;import javax.swing.BoxLayout;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.border.EmptyBorder;import java.awt.event.ActionListener;import java.sql.SQLException;import java.awt.event.ActionEvent;public class ChangeFrame extends JFrame {private JPanel contentPane;private JTextField idField;private JTextField nameField;private JTextField genderField;private JTextField dobField;private JTextField batchField;private JTextField searchField;/*** Launch the application.*/public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try{ChangeFrame frame =new ChangeFrame();frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/*** Create the frame.*/public ChangeFrame() {setResizable(false);setTitle("修改信息");setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);setBounds(100, 100, 450, 470);setLocationRelativeTo(null);contentPane = new JPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));setContentPane(contentPane);contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));JPanel panel = new JPanel();contentPane.add(panel);panel.setLayout(null);JLabel lblNewLabel = new JLabel("请输入要修改的学生的学号:");lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 16));lblNewLabel.setBounds(71, 0, 208, 29);panel.add(lblNewLabel);JLabel lblNewLabel_1 = new JLabel("学号:");lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN,16));lblNewLabel_1.setBounds(71, 105, 50, 30);panel.add(lblNewLabel_1);idField = new JTextField();idField.setEditable(false);idField.setBounds(143, 99, 240, 45);panel.add(idField);idField.setColumns(10);JLabel lblNewLabel_1_1 = new JLabel("姓名:");lblNewLabel_1_1.setFont(new Font("宋体", Font.PLAIN, 16));lblNewLabel_1_1.setBounds(71, 160, 50, 30);panel.add(lblNewLabel_1_1);nameField = newJTextField();nameField.setColumns(10);nameField.setBounds(143, 154, 240, 45);panel.add(nameField);JLabel lblNewLabel_1_1_1 = new JLabel("性别:");lblNewLabel_1_1_1.setFont(new Font("宋体", Font.PLAIN, 16));lblNewLabel_1_1_1.setBounds(71, 215, 50, 30);panel.add(lblNewLabel_1_1_1);genderField =new JTextField();genderField.setColumns(10);genderField.setBounds(143, 209, 240, 45);panel.add(genderField);JLabel lblNewLabel_1_1_1_1 =new JLabel("出生日期:");lblNewLabel_1_1_1_1.setFont(new Font("宋体", Font.PLAIN, 16));lblNewLabel_1_1_1_1.setBounds(41, 270, 80, 30);panel.add(lblNewLabel_1_1_1_1);dobField = new JTextField();dobField.setColumns(10);dobField.setBounds(143, 264, 240, 45);panel.add(dobField);JLabel lblNewLabel_1_1_1_2 =new JLabel("班级:");lblNewLabel_1_1_1_2.setFont(new Font("宋体", Font.PLAIN, 16));lblNewLabel_1_1_1_2.setBounds(71, 325, 50, 30);panel.add(lblNewLabel_1_1_1_2);batchField = new JTextField();batchField.setColumns(10);batchField.setBounds(143, 319, 240, 45);panel.add(batchField);JButton changeButton = new JButton("修改");changeButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {intid = Integer.parseInt(searchField.getText());String nameString = nameField.getText();String gendeString = genderField.getText();String dobString = dobField.getText();int batch = Integer.parseInt(batchField.getText());SQLHelp sqlHelp = new SQLHelp();try {sqlHelp.changeStudent(id, nameString, gendeString, dobString, batch);JOptionPane.showMessageDialog(ChangeFrame.this, "修改成功!");} catch (SQLException e) {JOptionPane.showMessageDialog(ChangeFrame.this, "修改失败!");e.printStackTrace();}}});changeButton.setFont(new Font("宋体", Font.PLAIN, 18));changeButton.setBounds(182, 389, 97, 33);panel.add(changeButton);searchField = newJTextField();searchField.setBounds(71, 39, 208, 45);panel.add(searchField);searchField.setColumns(10);JButton searchButton = new JButton("查找");searchButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e){int id = Integer.parseInt(searchField.getText());try{SQLHelp sqlHelp =new SQLHelp();Student student = sqlHelp.queryStudent(id);if(student != null) {// id 是直接读入的,所以就不需要从数据库中读出了idField.setText(String.valueOf(id));nameField.setText(student.getName());genderField.setText(student.getGender());dobField.setText(student.getDob());batchField.setText(String.valueOf(student.getBatch()));}else {JOptionPane.showMessageDialog(ChangeFrame.this, "无此用户");}} catch(SQLException e1) {e1.printStackTrace();}}});searchButton.setFont(new Font("宋体", Font.PLAIN, 18));searchButton.setBounds(289, 42, 97, 39);panel.add(searchButton);}}

修改前查询到学生信息

修改成功

修改失败

失败的情况是数据链接出现了问题

未查询到学生信息

删除学生

package studentsmanager;import java.awt.EventQueue;import java.awt.Font;import javax.swing.BoxLayout;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.border.EmptyBorder;import java.awt.event.ActionListener;import java.sql.SQLException;import java.awt.event.ActionEvent;public class DeleteFrame extends JFrame {private JPanel contentPane;private JTextField idField;private JTextField nameField;private JTextField genderField;private JTextField dobField;private JTextField batchField;private JTextField searchField;/*** Launch the application.*/public static void main(String[] args){EventQueue.invokeLater(new Runnable() {public void run() {try {DeleteFrame frame = new DeleteFrame();frame.setVisible(true);} catch(Exception e) {e.printStackTrace();}}});}/*** Create the frame.*/public DeleteFrame() {setResizable(false);setTitle("删除学生");setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);setBounds(100, 100, 450, 470);setLocationRelativeTo(null);contentPane = newJPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));setContentPane(contentPane);contentPane.setLayout(newBoxLayout(contentPane, BoxLayout.Y_AXIS));JPanel panel =new JPanel();contentPane.add(panel);panel.setLayout(null);JLabel lblNewLabel = new JLabel("请输入要删除的学生的学号:");lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 16));lblNewLabel.setBounds(71, 0, 208, 29);panel.add(lblNewLabel);JLabel lblNewLabel_1 =new JLabel("学号:");lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 16));lblNewLabel_1.setBounds(71, 105, 50, 30);panel.add(lblNewLabel_1);idField =new JTextField();idField.setEditable(false);idField.setBounds(143, 99, 240, 45);panel.add(idField);idField.setColumns(10);JLabel lblNewLabel_1_1 = new JLabel("姓名:");lblNewLabel_1_1.setFont(new Font("宋体", Font.PLAIN, 16));lblNewLabel_1_1.setBounds(71, 160,50, 30);panel.add(lblNewLabel_1_1);nameField = new JTextField();nameField.setColumns(10);nameField.setBounds(143, 154, 240, 45);panel.add(nameField);JLabel lblNewLabel_1_1_1 = new JLabel("性别:");lblNewLabel_1_1_1.setFont(new Font("宋体", Font.PLAIN, 16));lblNewLabel_1_1_1.setBounds(71, 215, 50, 30);panel.add(lblNewLabel_1_1_1);genderField = new JTextField();genderField.setColumns(10);genderField.setBounds(143, 209, 240, 45);panel.add(genderField);JLabel lblNewLabel_1_1_1_1 = new JLabel("出生日期:");lblNewLabel_1_1_1_1.setFont(new Font("宋体", Font.PLAIN,16));lblNewLabel_1_1_1_1.setBounds(41, 270, 80, 30);panel.add(lblNewLabel_1_1_1_1);dobField = newJTextField();dobField.setColumns(10);dobField.setBounds(143, 264, 240, 45);panel.add(dobField);JLabel lblNewLabel_1_1_1_2 = new JLabel("班级:");lblNewLabel_1_1_1_2.setFont(new Font("宋体", Font.PLAIN, 16));lblNewLabel_1_1_1_2.setBounds(71, 325, 50, 30);panel.add(lblNewLabel_1_1_1_2);batchField = newJTextField();batchField.setColumns(10);batchField.setBounds(143, 319, 240, 45);panel.add(batchField);JButton deleteButton = new JButton("删除");deleteButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {int id = Integer.parseInt(searchField.getText());try {SQLHelp sqlHelp = new SQLHelp();sqlHelp.deletestudent(id);JOptionPane.showMessageDialog(DeleteFrame.this, "删除成功!");} catch(SQLException e1) {JOptionPane.showMessageDialog(DeleteFrame.this, "删除失败");e1.printStackTrace();}}});deleteButton.setFont(new Font("宋体", Font.PLAIN, 18));deleteButton.setBounds(182, 389, 97, 33);panel.add(deleteButton);searchField = newJTextField();searchField.setBounds(71, 39, 208, 45);panel.add(searchField);searchField.setColumns(10);JButton searchButton = new JButton("查找");searchButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {int id = Integer.parseInt(searchField.getText());try{SQLHelp sqlHelp =new SQLHelp();Student student = sqlHelp.queryStudent(id);if(student != null) {idField.setText(String.valueOf(id));nameField.setText(student.getName());genderField.setText(student.getGender());dobField.setText(student.getDob());batchField.setText(String.valueOf(student.getBatch()));}else {JOptionPane.showMessageDialog(DeleteFrame.this, "无此学生");}} catch(SQLException e) {e.printStackTrace();}}});searchButton.setFont(new Font("宋体", Font.PLAIN, 18));searchButton.setBounds(289, 42, 97, 39);panel.add(searchButton);}}

删除成功

删除失败

删除失败的时候是数据库链接出现了问题

查询到学生信息

未查询到学生

查询信息

package studentsmanager;import java.awt.EventQueue;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.sql.SQLException;import javax.swing.BoxLayout;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.border.EmptyBorder;public class QueryFrame extends JFrame {private JPanel contentPane;privateJTextField idField;private JTextField nameField;private JTextField genderField;private JTextField dobField;private JTextField batchField;privateJTextField searchField;/*** Launch the application.*/public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {QueryFrame frame = new QueryFrame();frame.setVisible(true);} catch(Exception e) {e.printStackTrace();}}});}/*** Create the frame.*/public QueryFrame() {setResizable(false);setTitle("查询信息");setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);setBounds(100, 100, 450, 470);setLocationRelativeTo(null);contentPane = new JPanel();contentPane.setBorder(newEmptyBorder(5, 5, 5, 5));setContentPane(contentPane);contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));JPanel panel = newJPanel();contentPane.add(panel);panel.setLayout(null);JLabel lblNewLabel = new JLabel("请输入要查找的学生的学号:");lblNewLabel.setFont(new Font("宋体", Font.PLAIN,16));lblNewLabel.setBounds(71, 0, 208, 29);panel.add(lblNewLabel);JLabel lblNewLabel_1 = new JLabel("学号:");lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 16));lblNewLabel_1.setBounds(71, 105, 50, 30);panel.add(lblNewLabel_1);idField = newJTextField();idField.setEditable(false);idField.setBounds(143, 99, 240, 45);panel.add(idField);idField.setColumns(10);JLabel lblNewLabel_1_1 = newJLabel("姓名:");lblNewLabel_1_1.setFont(new Font("宋体", Font.PLAIN, 16));lblNewLabel_1_1.setBounds(71, 160, 50, 30);panel.add(lblNewLabel_1_1);nameField =new JTextField();nameField.setColumns(10);nameField.setBounds(143, 154, 240, 45);panel.add(nameField);JLabel lblNewLabel_1_1_1 =new JLabel("性别:");lblNewLabel_1_1_1.setFont(new Font("宋体", Font.PLAIN, 16));lblNewLabel_1_1_1.setBounds(71, 215, 50, 30);panel.add(lblNewLabel_1_1_1);genderField =new JTextField();genderField.setColumns(10);genderField.setBounds(143, 209, 240, 45);panel.add(genderField);JLabel lblNewLabel_1_1_1_1 =new JLabel("出生日期:");lblNewLabel_1_1_1_1.setFont(new Font("宋体", Font.PLAIN, 16));lblNewLabel_1_1_1_1.setBounds(41, 270, 80, 30);panel.add(lblNewLabel_1_1_1_1);dobField = new JTextField();dobField.setColumns(10);dobField.setBounds(143, 264, 240, 45);panel.add(dobField);JLabel lblNewLabel_1_1_1_2 = new JLabel("班级:");lblNewLabel_1_1_1_2.setFont(new Font("宋体", Font.PLAIN, 16));lblNewLabel_1_1_1_2.setBounds(71, 325, 50, 30);panel.add(lblNewLabel_1_1_1_2);batchField = new JTextField();batchField.setColumns(10);batchField.setBounds(143, 319, 240, 45);panel.add(batchField);searchField = new JTextField();searchField.setBounds(71, 39, 208, 45);panel.add(searchField);searchField.setColumns(10);JButton searchButton = new JButton("查找");searchButton.addActionListener(newActionListener() {public void actionPerformed(ActionEvent arg0) {// 1.读出要查询的学生输入的id// 因为学生的学号是唯一的,所以我们根据学号查询后,只会有一条数据// 转化一下数据类型intid = Integer.parseInt(searchField.getText());// 2.执行JDBC语句try {SQLHelp sqlHelp = new SQLHelp();Student student = sqlHelp.queryStudent(id);// 3.将查询结果填到文本框中// 前提是学生存在if(student != null) {idField.setText(String.valueOf(id));nameField.setText(student.getName());genderField.setText(student.getGender());dobField.setText(student.getDob());batchField.setText(String.valueOf(student.getBatch()));}else {// 此时学生不存在,提示用户,不存在该学生JOptionPane.showMessageDialog(QueryFrame.this, "无此学生");}} catch (SQLException e) {e.printStackTrace();}}});searchButton.setFont(new Font("宋体", Font.PLAIN,18));searchButton.setBounds(289, 42, 97, 39);panel.add(searchButton);}}

SQLHelp 工具类

package studentsmanager;importjava.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class SQLHelp {public static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";public static final String DB_URL = "jdbc:mysql://localhost:3306/niit?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";public static final String username = "root";public static final String password = "011216";private Connection connection = null;private PreparedStatement pStatement = null;private ResultSet rSet = null;// 加载驱动// 静态初始化块(只执行一次)static {try {Class.forName(JDBC_DRIVER);} catch(ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}// 链接数据库public void connectDB() {try{connection = DriverManager.getConnection(DB_URL,username,password);System.out.println("数据库链接成功");} catch (SQLException e) {System.out.println("数据库链接失败");e.printStackTrace();}}// 关闭资源public void close() {if(rSet != null) {try {rSet.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(pStatement != null) {try {pStatement.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(connection != null) {try {connection.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public void addStudent(int id, String nameString, String genderString, String dobString, int batch) throwsSQLException{// try finally 无论是否抛出异常都将执行 finally 中的语句try {// 先链接到数据库connectDB();// sql 语句// 静态 sql 语句需要进行字符串拼接// 动态 sql 语句String addsql = "insert into student values(?, ?, ?, ?, ?)";pStatement = connection.prepareStatement(addsql);pStatement.setInt(1, id);pStatement.setString(2, nameString);pStatement.setString(3, genderString);pStatement.setString(4, dobString);pStatement.setInt(5, batch);pStatement.executeUpdate();}finally {close();}}public void changeStudent(int id, String nameString, String genderString, String dobString, int batch) throwsSQLException{try {connectDB();String changesql = "update student set name = ?, gender = ?, dob = ?, batch = ? where id = ?";pStatement = connection.prepareStatement(changesql);pStatement.setString(1, nameString);pStatement.setString(2, genderString);pStatement.setString(3, dobString);pStatement.setInt(4, batch);pStatement.setInt(5, id);pStatement.executeUpdate();} finally{close();}}public void deletestudent(int id) throws SQLException {try {connectDB();String deleteString = "select * from student where id = ?";pStatement = connection.prepareStatement(deleteString);pStatement.setInt(1, id);pStatement.execute();} finally {close();}}public Student queryStudent(int id) throws SQLException {try {connectDB();String querysql = "select * from student where id = ?";pStatement = connection.prepareStatement(querysql);pStatement.setInt(1, id);// 三种方法执行// execute() 都可以使用,返回true或false// executeQuery() 专门用于查询,返回结果集// executeUpdate() 专门用于删除、更新rSet = pStatement.executeQuery();if(rSet.next()) {String nameString = rSet.getString(2);String genderString = rSet.getString(3);String dobString = rSet.getString(4);int batchString = rSet.getInt(5);// 查询到学生信息返回结果集return newStudent(nameString, genderString, dobString, batchString);}else {// 没有查询到学生信息,返回nullreturn null;}} finally {close();}}}

Student 工具类

package studentsmanager;public class Student {private String name;private String gender;private String dob;private int batch;public Student(String name, String gender, String dob, int batch) {this.name = name;this.gender = gender;this.dob = dob;this.batch = batch;}public String getName() {return name;}public String getGender() {returngender;}public String getDob() {return dob;}public int getBatch() {return batch;}}

5. 项目总结

这算一个比较完整的项目了,既有 GUI 又有 MySQL 数据库的已经 Java 的 JDBC ,虽然已经有了 GUI 界面,但很明显,没有进行任何美化,就只是简单的一个界面,勉勉强强可以看的过去,使用 JDBC 链接数据库的时候,没有实现可以直接在程序创建数据库,只能从数据库方创建数据库,这是一个缺点,同时使用 JDBC 链接数据库的时候,所使用的代码依旧是 MySQL 数据库的代码,(我 MySQL 学的不好,增删改查啥的数据库代码都还记不清楚 哭了…)还有就是当程序在进行修改和删除的时候,修改失败和删除失败的时候,都是因为数据链接失败了才进行弹窗,不知道怎么改成无法修改或者无法删除的时候提示,(又哭了…)

欢迎个位大佬指出错误,同时也欢迎各位小白借鉴,有问题的话,评论或者私聊都是可以的,看了就会回复!

最后一句:

关注博主不迷路,博主带你码代码!

原文地址:

https://blog.csdn.net/qq_52354698/article/details/121582956
分类: 百科知识 标签: 暂无标签

评论

暂无评论数据

暂无评论数据

目录