A java student management system based on MySQL8

Main.java

package StudentManagerSystem;

import java.sql.*;
import java.util.Scanner;

public class Main {

    static Connection conn = null;
    static Statement stmt = null;

    static {
        try {
            conn = DriverManager.getConnection("jdbc:mysql:///xin?useSSL=false&serverTimezone=UTC", "root", "123456");
            stmt = conn.createStatement();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        if (conn == null || stmt == null) {
            System.out.println("连接失败,错误原因mysql服务未开启等...");
            return;
        }
        Boolean status = false;
        Scanner sc = new Scanner(System.in);
        while (true) {
            status = clear(status);    //用于清屏
            System.out.println("------------------" + "欢迎使用学生管理系统" + "-----------------");
            System.out.println("\t\t\t\t\t" + "1.添加学生信息");
            System.out.println("\t\t\t\t\t" + "2.删除学生信息");
            System.out.println("\t\t\t\t\t" + "3.学生信息排序");
            System.out.println("\t\t\t\t\t" + "4.修改学生信息");
            System.out.println("\t\t\t\t\t" + "5.查看学生信息");
            System.out.println("\t\t\t\t\t" + "6.退出管理系统");
            System.out.println("\t\t" + "------------------------------------");
            System.out.print("\t\t\t\t\t" + "请输入功能序号:");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    addMessage();
                    break;
                case 2:
                    delMessage();
                    break;
                case 3:
                    sort();
                    break;
                case 4:
                    modification();
                    break;
                case 5:
                    look();
                    break;
                case 6:
                    return;
            }
        }
    }

    static Boolean clear(Boolean status) {
        if (status) {
            for (int i = 0; i < 20; i++) {
                System.out.println();
            }
        }
        return true;
    }

    static void addMessage() throws SQLException {
        while (true) {
            Student student = new Student();
            System.out.print("请输入姓名:");
            student.setName(new Scanner(System.in).next());

            System.out.print("请输入性别:");
            student.setGender(new Scanner(System.in).next());

            System.out.print("请输入语文分数:");
            student.setChinese(new Scanner(System.in).nextInt());

            System.out.print("请输入数学分数:");
            student.setMath(new Scanner(System.in).nextInt());

            System.out.print("请输入英语分数:");
            student.setEnglish(new Scanner(System.in).nextInt());

            student.count();

            String sql = "INSERT INTO student VALUES(" + student.getWriteId() + ","
                    + "'" + student.getName() + "'" + ","
                    + "'" + student.getGender() + "'" + ","
                    + student.getChinese() + ","
                    + student.getMath() + ","
                    + student.getEnglish() + ","
                    + student.getCount()
                    + ")";
            stmt.executeUpdate(sql);
            while (true) {
                System.out.print("添加成功,是否继续添加(Y/N):");
                char c = new Scanner(System.in).next().charAt(0);
                if (c == 'y' || c == 'Y') {
                    break;
                } else if (c == 'N' || c == 'n') {
                    return;
                }
            }
        }
    }

    static void delMessage() throws SQLException {
        String delName;
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入要删除的学生姓名:");
        delName = sc.next();
        ResultSet results = stmt.executeQuery("select * from student where name='" + delName + "';");
        if (!results.next()) {     //如果找不到学生信息
            System.out.print("未找到学生姓名为:" + delName + " 的学生信息,三秒后自动返回主界面 ");
            for (int i = 0; i < 3; i++) {
                System.out.print("\b" + (3 - i));
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return;
        }

        ResultSet results2 = stmt.executeQuery("select * from student where name='" + delName + "';");
        int count = 0;    //判断有信息名与输入的 名子 一样
        while (results2.next()) {
            count++;
        }

        if (count > 1) {
            ResultSet results3 = stmt.executeQuery("select * from student where name='" + delName + "';");
            System.out.println("姓名重复的学生信息如下:");

            while (results3.next()) {
                System.out.format("id:%-6d  name:%-6s  gender:%-2s  chinese:%-5d  math:%-5d  english:%-5d  count:%d\n",
                        results3.getInt(1), results3.getString("name"),
                        results3.getString("gender"), results3.getInt("chinese"),
                        results3.getInt("math"), results3.getInt("english")
                        , results3.getInt("count"));
            }

            System.out.print("请输入需要删除的学生id:");
            int delId = sc.nextInt();
            ResultSet results4 = stmt.executeQuery("select * from student where id=" + delId + ";");
            if (!results4.next()) {     //如果找不到学生信息
                System.out.print("未找到id为:" + delId + " 的学生信息,三秒后自动返回主界面 ");
                for (int i = 0; i < 3; i++) {
                    System.out.print("\b" + (3 - i));
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                return;
            } else {
                if (stmt.executeUpdate("delete from student where id=" + delId + ";") > 0) {
                    while (true) {
                        System.out.print("删除成功,是否继续删除信息(Y/N):");
                        char c = new Scanner(System.in).next().charAt(0);
                        if (c == 'y' || c == 'Y') {
                            break;
                        } else if (c == 'N' || c == 'n') {
                            return;
                        }
                    }
                } else {
                    System.out.print("删除失败,错误原因未知,三秒后自动返回主界面 ");
                    for (int i = 0; i < 3; i++) {
                        System.out.print("\b" + (3 - i));
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    return;
                }
            }
        } else {
            if (stmt.executeUpdate("delete from student where name='" + delName + "'") > 0) {
                System.out.println("学生:" + delName + " 的信息删除成功!");
            } else {
                System.out.println("学生:" + delName + " 的信息删除失败!");
            }
            while (true) {
                System.out.print("是否继续删除信息(Y/N):");
                char c = new Scanner(System.in).next().charAt(0);
                if (c == 'y' || c == 'Y') {
                    break;
                } else if (c == 'N' || c == 'n') {
                    return;
                }
            }
        }
    }

    static void sort() throws SQLException {
        System.out.println("学生成绩排序后如下:");
        int count = 0;
        ResultSet resultSet = stmt.executeQuery("SELECT * FROM student ORDER BY count DESC;");
        while (resultSet.next()) {
            count++;
            System.out.format("id:%-6d  name:%-6s  gender:%-2s  chinese:%-5d  math:%-5d  english:%-5d  count:%d\n",
                    resultSet.getInt(1), resultSet.getString("name"),
                    resultSet.getString("gender"), resultSet.getInt("chinese"),
                    resultSet.getInt("math"), resultSet.getInt("english")
                    , resultSet.getInt("count"));
        }

        if (count == 0) {
            System.out.print("数据库中共有:0 条学生信息,三秒后自动返回主界面 ");
            for (int i = 0; i < 3; i++) {
                System.out.print("\b" + (3 - i));
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return;
        }

        System.out.println("共有:" + count + " 条学生信息,输出完毕,按任意字符后回车返回主界面");
        String s = new Scanner(System.in).next();
    }

    static void modification() throws SQLException {
        String modificationName;
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入要修改信息的学生姓名:");
        modificationName = sc.next();
        ResultSet results = stmt.executeQuery("select * from student where name='" + modificationName + "';");
        if (!results.next()) {     //如果找不到学生信息
            System.out.print("未找到学生姓名为:" + modificationName + " 的学生信息,三秒后自动返回主界面 ");
            for (int i = 0; i < 3; i++) {
                System.out.print("\b" + (3 - i));
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return;
        }

        ResultSet results2 = stmt.executeQuery("select * from student where name='" + modificationName + "';");
        int count = 0;    //判断有信息名与输入的 名子 一样
        while (results2.next()) {
            count++;
        }

        if (count > 1) {
            ResultSet results3 = stmt.executeQuery("select * from student where name='" + modificationName + "';");
            System.out.println("姓名重复的学生信息如下:");

            while (results3.next()) {
                System.out.format("id:%-6d  name:%-6s  gender:%-2s  chinese:%-5d  math:%-5d  english:%-5d  count:%d\n",
                        results3.getInt(1), results3.getString("name"),
                        results3.getString("gender"), results3.getInt("chinese"),
                        results3.getInt("math"), results3.getInt("english")
                        , results3.getInt("count"));
            }

            System.out.print("请输入需要修改的学生id:");
            int studentId = sc.nextInt();
            ResultSet results4 = stmt.executeQuery("select * from student where id=" + studentId + ";");

            if (!results4.next()) {     //如果找不到学生信息
                System.out.print("未找到id为:" + studentId + " 的学生信息,三秒后自动返回主界面 ");
                for (int i = 0; i < 3; i++) {
                    System.out.print("\b" + (3 - i));
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                return;
            } else {
                Student student = new Student();
                System.out.print("请重新输入姓名:");
                student.setName(new Scanner(System.in).next());

                System.out.print("请重新输入性别:");
                student.setGender(new Scanner(System.in).next());

                System.out.print("请重新输入语文分数:");
                student.setChinese(new Scanner(System.in).nextInt());

                System.out.print("请重新输入数学分数:");
                student.setMath(new Scanner(System.in).nextInt());

                System.out.print("请重新输入英语分数:");
                student.setEnglish(new Scanner(System.in).nextInt());

                student.count();

                String sql = "UPDATE student SET "
                        + "name='" + student.getName() + "'" + ","
                        + "gender='" + student.getGender() + "'" + ","
                        + "chinese=" + student.getChinese() + ","
                        + "math=" + student.getMath() + ","
                        + "english=" + student.getEnglish() + ","
                        + "count=" + student.getCount()
                        + " where id=" + studentId + ";";
                stmt.executeUpdate(sql);

                System.out.print("\n修改成功,三秒后自动返回主界面");
                for (int i = 0; i < 3; i++) {
                    System.out.print("\b" + (3 - i));
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                return;
            }
        } else {
            Student student = new Student();
            System.out.print("请重新输入姓名:");
            student.setName(new Scanner(System.in).next());

            System.out.print("请重新输入性别:");
            student.setGender(new Scanner(System.in).next());

            System.out.print("请重新输入语文分数:");
            student.setChinese(new Scanner(System.in).nextInt());

            System.out.print("请重新输入数学分数:");
            student.setMath(new Scanner(System.in).nextInt());

            System.out.print("请重新输入英语分数:");
            student.setEnglish(new Scanner(System.in).nextInt());

            student.count();

            String sql = "UPDATE student SET("
                    + "name='" + student.getName() + "'" + ","
                    + "gender='" + student.getGender() + "'" + ","
                    + "chinese=" + student.getChinese() + ","
                    + "math=" + student.getMath() + ","
                    + "english=" + student.getEnglish() + ","
                    + "count=" + student.getCount()
                    + ") where name=" + modificationName + ";";
            stmt.executeUpdate(sql);

            System.out.print("\n修改成功,三秒后自动返回主界面");
            for (int i = 0; i < 3; i++) {
                System.out.print("\b" + (3 - i));
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return;
        }
    }

    static void look() throws SQLException {
        System.out.println("学生信息如下:");
        int count = 0;
        ResultSet resultSet = stmt.executeQuery("SELECT * FROM student;");
        while (resultSet.next()) {
            count++;
            System.out.format("id:%-6d  name:%-6s  gender:%-2s  chinese:%-5d  math:%-5d  english:%-5d  count:%d\n",
                    resultSet.getInt(1), resultSet.getString("name"),
                    resultSet.getString("gender"), resultSet.getInt("chinese"),
                    resultSet.getInt("math"), resultSet.getInt("english")
                    , resultSet.getInt("count"));
        }

        if (count == 0) {
            System.out.print("数据库中共有:0 条学生信息,三秒后自动返回主界面 ");
            for (int i = 0; i < 3; i++) {
                System.out.print("\b" + (3 - i));
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return;
        }

        System.out.println("共有:" + count + " 条学生信息,输出完毕,按任意字符后回车返回主界面");
        String s = new Scanner(System.in).next();
    }
}


Student.java

package StudentManagerSystem;

public class Student {
    int readId;
    String writeId = "null";
    String name;
    String gender;
    int chinese;
    int math;
    int english;
    int count = 0;

    public Student() {
    }

    public Student(String id, String name, String gender, int chinese, int nath, int english) {
        this.writeId = id;
        this.name = name;
        this.gender = gender;
        this.chinese = chinese;
        this.math = nath;
        this.english = english;
    }

    public Student(int id, String name, String gender, int chinese, int nath, int english) {
        this.readId = id;
        this.name = name;
        this.gender = gender;
        this.chinese = chinese;
        this.math = nath;
        this.english = english;
    }

    public void count() {
        this.count = chinese + math + english;
    }

    public int getReadId() {
        return readId;
    }

    public void setWriteId(String id) {
        this.writeId = id;
    }

    public String getWriteId() {
        return writeId;
    }

    public void setReadId(int id) {
        this.readId = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getChinese() {
        return chinese;
    }

    public void setChinese(int chinese) {
        this.chinese = chinese;
    }

    public int getMath() {
        return math;
    }

    public void setMath(int nath) {
        this.math = nath;
    }

    public int getEnglish() {
        return english;
    }

    public void setEnglish(int english) {
        this.english = english;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

}

Running interface shown below, other functions are normal here is not a screenshot

Insert picture description here


End