Home  >  Article  >  Database  >  How to use Java+mysql to implement student status management system

How to use Java+mysql to implement student status management system

王林
王林forward
2023-05-29 12:28:111738browse

1. myswql database table

The project uses the mysql database and has 2 tables. A user table is used for login verification, and a student table is used for addition, deletion, modification and query.

creat table t_user(
id int primary key auto_increment,
login_name varchar(255),
login_pwd  varchar(255),
real_name varchar(255),
);
insert into t_user(id,login_name,login_pwd,real_name)
values('akm',"123",'萝卜蹲');
CREATE TABLE t_user(
 id char(12) PRIMARY KEY,
 name char(6),
 pwd varchar(255),
);

2. Function implementation

1. Actual demonstration

1.1 Login interface

How to use Java+mysql to implement student status management system

Input in the user's password field: akm
Input in the password field: 123
Click the login button to enter the system directly.

How to use Java+mysql to implement student status management system

#If you enter incorrectly, the status bar will show login failure and the login account and password will be cleared.

1.2 System main interface

How to use Java+mysql to implement student status management system

The system main interface consists of 5 buttons

How to use Java+mysql to implement student status management system

To add student information, please select the add button on the main interface and click to enter the adding interface as shown above. Add corresponding student information, ID, name, age, student status, etc. to the interface.

How to use Java+mysql to implement student status management system

1.3 Query information

Query student information by id.

How to use Java+mysql to implement student status management system

1.4 Traverse information

How to use Java+mysql to implement student status management system

1.5 Delete information

How to use Java+mysql to implement student status management system

Enter the id to delete directly.

1.6 Update information

How to use Java+mysql to implement student status management system

2.test.java file source code

The project has only one test file, no Encapsulation, friends who need it can encapsulate it themselves.

The code is as follows:

package com.company;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.sql.Statement;
import java.util.Scanner;
public class Test {
    static  Connection conn ;
    static Statement statement;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        login();
        }
    public static void control() {
        JFrame jf = new JFrame("学生学籍管理系统");
        jf.setLayout(new FlowLayout(FlowLayout.LEFT));
        jf.setBounds(400, 300, 300, 200);
        JButton button = new JButton("更新");
        JButton button1=new JButton("遍历");
        JButton button2=new JButton("删除");
        JButton button3=new JButton("添加");
        JButton button4=new JButton("查询");
        jf.add(button);
        jf.add(button1);
        jf.add(button2);
        jf.add(button3);
        jf.add(button4);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                update();
            }
        });
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                query();
            }
        });
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                delete();
            }
        });
        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                insert();
            }
        });
        button4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {onequery();}

        });
        jf.setVisible(true);
        jf.setResizable(false);
        button.setSize(40, 20);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void update() {
        conn = getConnection();
        JFrame jf = new JFrame("学生学籍管理系统");
        jf.setLayout(new FlowLayout(FlowLayout.LEFT));
        jf.setBounds(400, 300, 300, 200);
        JLabel label1 = new JLabel("年龄");
        JTextField agetext = new JTextField("", 10);
        JLabel label2 = new JLabel("id");
        JTextField idtext = new JTextField("", 10);
        JLabel label3 = new JLabel("学籍");
        JTextField addresstext = new JTextField("", 10);
        JLabel label4 = new JLabel("姓名");
        JTextField nametext = new JTextField("", 5);
        JTextField out = new JTextField("更新状态", 20);
        JButton button = new JButton("更新");
        jf.add(label1);
        jf.add(agetext);
        jf.add(label2);
        jf.add(idtext);
        jf.add(label3);
        jf.add(addresstext);
        jf.add(label4);
        jf.add(nametext);
        jf.add(out);
        jf.add(button);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                PreparedStatement ps=null;
                String age = agetext.getText();
                String id = idtext.getText();
                String address= addresstext.getText();
                String name=nametext.getText();
                try {
                    // 更新数据的sql语句
                    String sql = "update student set age =? , address =?, name=? where  id = ?";
                    ps=conn.prepareStatement(sql);
                    ps.setString(1,agetext.getText());
                    ps.setString(2,addresstext.getText());
                    ps.setString(3,nametext.getText());
                    ps.setString(4,idtext.getText());
                    int count = ps.executeUpdate();//记录操作次数
                    // 输出插入操作的处理结果
                    System.out.println("user表中更新 " + count + " 条数据");
                    ps.close();
                    //关闭数据库连接
                    conn.close();
                    out.setText("更新成功!!!!!!!!");
                    // 创建用于执行静态sql语句的Statement对象,st属局部变量
                } catch (SQLException a) {
                    System.out.println("更新数据失败");
                }

            }

        });
        jf.setVisible(true);
        jf.setResizable(false);
        button.setSize(40, 20);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    public static void query() {
        PreparedStatement ps=null;
        conn = getConnection();
        JFrame jf = new JFrame("学生学籍管理系统");
        jf.setLayout(null);
        jf.setBounds(400, 300, 350, 200);
        JButton button = new JButton("查询");
        JTextArea jm=new JTextArea("ID\t姓名\t年龄\t学籍");//显示界面
        jm.setBounds(10,50,350,100);//定义显示界面位置
        jf.add(button);
        jf.add(jm);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                PreparedStatement ps=null;
                try {
                    String sql = "select * from Student";
                    //创建用于执行静态sql语句的Statement对象,statement属局部变量
                    statement = conn.createStatement();//获取操作对象
                    ResultSet resultSet = statement.executeQuery(sql);// executeQuery执行单个SQL语句,返回单个ResultSet对象是
                    while (resultSet.next())//循环没有数据的时候返回flase退出循环
                    {
                        Integer Id = resultSet.getInt("id");//resultSet.next()是一个光标
                        String name = resultSet.getString("name");//getString返回的值一定是string
                        Integer age = resultSet.getInt("age");
                        String address=resultSet.getString("address");
                        //String adress = resultSet.getString("adress");
                        //输出查到的记录的各个字段的值
                        jm.append("\n"+Id + "\t" + name + "\t" + age+ "\t" + address );
                    }
                    statement.close();
                    conn.close();
                }catch (SQLException b){
                    System.out.println("查询失败!!!!!!!!");
                }
            }
        });
        jf.setVisible(true);
        jf.setResizable(false);
        button.setSize(40, 20);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void delete() {
        conn = getConnection();
        JFrame jf = new JFrame("学生学籍管理系统");
        jf.setLayout(new FlowLayout(FlowLayout.LEFT));
        jf.setBounds(400, 300, 300, 200);
        JLabel label2 = new JLabel("id");
        JTextField idtext = new JTextField("", 10);
        JTextField out = new JTextField("删除状态", 20);
        JButton button = new JButton("删除");
        jf.add(label2);
        jf.add(idtext);
        jf.add(out);
        jf.add(button);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String id = idtext.getText();
                PreparedStatement ps=null;
                try {
                    // 删除数据的sql语句
                    String sql = "delete from Student  where id = ?";
                    ps=conn.prepareStatement(sql);
                    ps.setString(1,idtext.getText());
                    int count = ps.executeUpdate();//记录操作次数
                    // 输出插入操作的处理结果
                    System.out.println("student表中删除 " + count + " 条数据");
                    ps.close();
                    out.setText("删除成功!!!!!!!!");
                    // 关闭数据库连接
                    conn.close();
                } catch (SQLException c) {
                    System.out.println("删除数据失败");
                }
            }
        });
        jf.setVisible(true);
        jf.setResizable(false);
        button.setSize(40, 20);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }
    public static void insert() {
        // 首先要获取连接,即连接到数据库
        conn = getConnection();
        JFrame jf = new JFrame("学生学籍管理系统");
        jf.setLayout(new FlowLayout(FlowLayout.LEFT));
        jf.setBounds(400, 300, 300, 200);
        JLabel label3 = new JLabel("id");
        JTextField idtext = new JTextField("", 10);
        JLabel label1 = new JLabel("年龄");
        JTextField agetext = new JTextField("", 10);
        JLabel label2 = new JLabel("姓名");
        JTextField nametext = new JTextField("", 10);
        JLabel label4 = new JLabel("学籍");
        JTextField addresstext = new JTextField("", 5);
        JTextField out = new JTextField("添加状态", 20);
        JButton button = new JButton("添加");
        jf.add(label3);
        jf.add(idtext);
        jf.add(label1);
        jf.add(agetext);
        jf.add(label2);
        jf.add(nametext);
        jf.add(label4);
        jf.add(addresstext);
        jf.add(out);
        jf.add(button);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String age = agetext.getText();
                String name = nametext.getText();
                String  id=idtext.getText();
                String address=addresstext.getText();
                try {
                    PreparedStatement ps=null;
                    // 插入数据的sql语句
                    String sql = "INSERT INTO Student( id,age,name,address) VALUES ( ?,?,?,?)";
                    ps=conn.prepareStatement(sql);
                    ps.setString(1,idtext.getText());
                    ps.setString(2,agetext.getText());
                    ps.setString(3,nametext.getText());
                    ps.setString(4,addresstext.getText());
                    int count = ps.executeUpdate();//记录操作次数
                    // 输出插入操作的处理结果
                    System.out.println("向user表中插入 " + count + " 条数据");
                    ps.close();
                    out.setText("添加成功!!!!!!!!");
                    // 关闭数据库连接
                    conn.close();
                } catch (SQLException d) {
                    System.out.println("插入数据失败" + d.getMessage());
                }

            }
        });
        jf.setVisible(true);
        jf.setResizable(false);
        button.setSize(40, 20);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static boolean login(){
        conn = getConnection();
        JFrame jf = new JFrame("学生学籍管理系统");
        jf.setLayout(new FlowLayout(FlowLayout.LEFT));
        jf.setBounds(400, 300, 300, 200);
        JLabel label1 = new JLabel("用户名");
        JTextField usernametext = new JTextField("", 20);
        JLabel label2 = new JLabel("密码");
        JPasswordField pwdtext = new JPasswordField("", 20);
        JTextField out = new JTextField("登录状态", 20);
        JButton button = new JButton("登录");
        jf.add(label1);
        jf.add(usernametext);
        jf.add(label2);
        jf.add(pwdtext);
        jf.add(out);
        jf.add(button);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 插入数据的sql语句
                PreparedStatement ps=null;
                try {
                    statement = conn.createStatement();//获取操作对象
                    String x = usernametext.getText();
                    String y = pwdtext.getText();
                    String sql ="select * from user";
                    ResultSet resultSet = statement.executeQuery(sql);
                    while(resultSet.next()){
                        String a=resultSet.getString("login_name");
                        String b=resultSet.getString("login_pwd");
                        if(a.equals(x)&&b.equals(y))
                        {
                            control();
                            out.setText("!!!!!登录成功!!!!!");
                        }
                        else if(x!=a&&y!=b){
                            out.setText("登录失败,请重新输入");
                        }
                    }
                    usernametext.setText("");
                    pwdtext.setText("");
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        });
        jf.setVisible(true);
        jf.setResizable(false);
        button.setSize(40, 20);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        boolean ok = true;
        return ok;
    }
    public static void onequery() {
        PreparedStatement ps=null;
        conn = getConnection();
        JFrame jf = new JFrame("学生学籍管理系统");
        jf.setLayout(new FlowLayout(FlowLayout.LEFT));
        jf.setBounds(400, 300, 350, 200);
        JLabel label3 = new JLabel("id");
        JTextField idtext = new JTextField("", 10);
        JLabel label1 = new JLabel("条件");
        JTextField atext = new JTextField("", 10);
        JButton button = new JButton("查询");
        //JTextArea jm=new JTextArea("ID\t姓名\t年龄\t学籍");//显示界面
        //jm.setBounds(10,50,350,100);//定义显示界面位置
        jf.add(label3);
        jf.add(idtext);
        jf.add(button);
        //jf.add(jm);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                PreparedStatement ps=null;
                String id=idtext.getText();
                conn = getConnection();
                try {
                    String sql = "select * from student where id = ?";
                    //创建用于执行静态sql语句的Statement对象,statement属局部变量
                    ps=conn.prepareStatement(sql);//获取操作对象
                    ps.setString(1,idtext.getText().toString());
                    ResultSet resultSet =  ps.executeQuery();// executeQuery执行单个SQL语句,返回单个ResultSet对象是
                    while (resultSet.next())//循环没有数据的时候返回flase退出循环
                    {
                        Integer Id = resultSet.getInt("id");//resultSet.next()是一个光标
                        String name = resultSet.getString("name");//getString返回的值一定是string
                        Integer age = resultSet.getInt("age");
                        String address=resultSet.getString("address");
                        System.out.println(Id + " " + name + " " + age + " "+ address + " " );
                        //输出查到的记录的各个字段的值
                        //jm.append("\n"+Id + "\t" + name + "\t" + age+ "\t" + address );
                    }
                    statement.close();
                    conn.close();
                }catch (SQLException m){
                    System.out.println(m.getMessage());
                    System.out.println("查询失败!!!!!!!!");
                }
            }
        });
        jf.setVisible(true);
        jf.setResizable(false);
        button.setSize(40, 20);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static Connection getConnection(){
        //创建用于连接数据库的Connection对象
        Connection connection = null;
        try {
            // 加载Mysql数据驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            System.out.println("数据库驱动加载成功");
            String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
            // 创建数据连接
            connection = DriverManager.getConnection(url, "root", "root");
            System.out.println("数据库连接成功");
        }catch (ClassNotFoundException | SQLException e){
            System.out.println("数据库连接失败" + e.getMessage());//处理查询结果
        }
        return connection;
    }

}

The above is the detailed content of How to use Java+mysql to implement student status management system. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete