简单的学生信息管理系统

简单的学生信息管理系统

java 复制代码
import java.sql.*;
import java.util.Scanner;

public class StudentManagementSystem {

    private static final String URL = "jdbc:mysql://localhost:3306/test";
    private static final String USER = "root";
    private static final String PASSWORD = "root";

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String input;
        System.out.println("\n欢迎使用学生管理系统,请登录:");
        while (!login(scanner)) {

        }

        do {
            System.out.println("\n学生管理系统");
            System.out.println("1. 增加学生");
            System.out.println("2. 删除学生");
            System.out.println("3. 修改学生信息");
            System.out.println("4. 查询学生信息");
            System.out.println("5. 浏览所有学生");
            System.out.println("0. 退出");
            System.out.print("请选择操作: ");
            input = scanner.nextLine();
            switch (input) {
                case "1":
                    addStudent(scanner);
                    break;
                case "2":
                    deleteStudent(scanner);
                    break;
                case "3":
                    updateStudent(scanner);
                    break;
                case "4":
                    searchStudent(scanner);
                    break;
                case "5":
                    listStudents(scanner);
                    break;
                case "0":
                    System.out.println("退出系统");
                    break;
                default:
                    System.out.println("无效的输入,请重新选择。");
            }
        } while (!input.equals("0"));

        scanner.close();
    }

    private static boolean login(Scanner scanner) {
        System.out.print("请输入用户名: ");
        String username = scanner.nextLine();
        System.out.print("请输入密码: ");
        String password = scanner.nextLine();

        if ("root".equals(username) && "root".equals(password)) {
            System.out.println("登录成功!");
            return true;
        } else {
            System.out.println("用户名或密码错误!");
            return false;
        }
    }

    // 增加学生信息
    private static void addStudent(Scanner scanner) {
        Connection conn = connectDatabase();
        if (conn != null) {
            try {
                System.out.print("输入学生姓名: ");
                String name = scanner.nextLine();
                System.out.print("输入学生年龄: ");
                int age = Integer.parseInt(scanner.nextLine());
                System.out.print("输入学生年级: ");
                String grade = scanner.nextLine();

                String sql = "INSERT INTO students (name, age, grade) VALUES (?, ?, ?)";
                try (PreparedStatement stmt = conn.prepareStatement(sql)) {
                    stmt.setString(1, name);
                    stmt.setInt(2, age);
                    stmt.setString(3, grade);
                    int rowsAffected = stmt.executeUpdate();
                    if (rowsAffected > 0) {
                        System.out.println("学生信息添加成功!");
                    } else {
                        System.out.println("添加失败!");
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                closeConnection(conn);
            }
        }
    }

    // 删除学生信息
    private static void deleteStudent(Scanner scanner) {
        Connection conn = connectDatabase();
        if (conn != null) {
            try {
                System.out.print("输入要删除的学生ID: ");
                int id = Integer.parseInt(scanner.nextLine());

                String sql = "DELETE FROM students WHERE id = ?";
                try (PreparedStatement stmt = conn.prepareStatement(sql)) {
                    stmt.setInt(1, id);
                    int rowsAffected = stmt.executeUpdate();
                    if (rowsAffected > 0) {
                        System.out.println("学生信息删除成功!");
                    } else {
                        System.out.println("删除失败,可能该学生ID不存在!");
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                closeConnection(conn);
            }
        }
    }

    // 修改学生信息
    private static void updateStudent(Scanner scanner) {
        Connection conn = connectDatabase();
        if (conn != null) {
            try {
                System.out.print("输入要修改的学生ID: ");
                int id = Integer.parseInt(scanner.nextLine());
                System.out.print("输入新的姓名: ");
                String name = scanner.nextLine();
                System.out.print("输入新的年龄: ");
                int age = Integer.parseInt(scanner.nextLine());
                System.out.print("输入新的年级: ");
                String grade = scanner.nextLine();

                String sql = "UPDATE students SET name = ?, age = ?, grade = ? WHERE id = ?";
                try (PreparedStatement stmt = conn.prepareStatement(sql)) {
                    stmt.setString(1, name);
                    stmt.setInt(2, age);
                    stmt.setString(3, grade);
                    stmt.setInt(4, id);
                    int rowsAffected = stmt.executeUpdate();
                    if (rowsAffected > 0) {
                        System.out.println("学生信息更新成功!");
                    } else {
                        System.out.println("更新失败,可能该学生ID不存在!");
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                closeConnection(conn);
            }
        }
    }

    // 查询学生信息
    private static void searchStudent(Scanner scanner) {
        Connection conn = connectDatabase();
        if (conn != null) {
            try {
                System.out.print("输入要查询的学生ID: ");
                int id = Integer.parseInt(scanner.nextLine());

                String sql = "SELECT * FROM students WHERE id = ?";
                try (PreparedStatement stmt = conn.prepareStatement(sql)) {
                    stmt.setInt(1, id);
                    ResultSet rs = stmt.executeQuery();
                    if (rs.next()) {
                        int studentId = rs.getInt("id");
                        String studentName = rs.getString("name");
                        int studentAge = rs.getInt("age");
                        String studentGrade = rs.getString("grade");
                        System.out.println("学生ID: " + studentId);
                        System.out.println("姓名: " + studentName);
                        System.out.println("年龄: " + studentAge);
                        System.out.println("年级: " + studentGrade);
                    } else {
                        System.out.println("未找到该学生信息!");
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                closeConnection(conn);
            }
        }
    }

    // 浏览所有学生信息
    private static void listStudents(Scanner scanner) {
        Connection conn = connectDatabase();
        if (conn != null) {
            try {
                String sql = "SELECT * FROM students";
                try (Statement stmt = conn.createStatement()) {
                    ResultSet rs = stmt.executeQuery(sql);
                    while (rs.next()) {
                        int studentId = rs.getInt("id");
                        String studentName = rs.getString("name");
                        int studentAge = rs.getInt("age");
                        String studentGrade = rs.getString("grade");
                        System.out.println("学生ID: " + studentId + ", 姓名: " + studentName + ", 年龄: " + studentAge + ", 年级: " + studentGrade);
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                closeConnection(conn);
            }
        }
    }

    // 连接数据库的方法
    private static Connection connectDatabase() {
        // 这里省略了异常处理和资源关闭,实际应用中需要添加
        try {
            return DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    // 断开数据库连接的方法
    private static void closeConnection(Connection connection) {
        // 这里省略了异常处理,实际应用中需要添加
        try {
            if (connection != null && !connection.isClosed()) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


}
相关推荐
空の鱼2 分钟前
java开发,IDEA转战VSCODE配置(mac)
java·vscode
P7进阶路1 小时前
Tomcat异常日志中文乱码怎么解决
java·tomcat·firefox
小丁爱养花2 小时前
Spring MVC:HTTP 请求的参数传递2.0
java·后端·spring
CodeClimb2 小时前
【华为OD-E卷 - 第k个排列 100分(python、java、c++、js、c)】
java·javascript·c++·python·华为od
等一场春雨2 小时前
Java设计模式 九 桥接模式 (Bridge Pattern)
java·设计模式·桥接模式
带刺的坐椅2 小时前
[Java] Solon 框架的三大核心组件之一插件扩展体系
java·ioc·solon·plugin·aop·handler
不惑_3 小时前
深度学习 · 手撕 DeepLearning4J ,用Java实现手写数字识别 (附UI效果展示)
java·深度学习·ui
费曼乐园3 小时前
Kafka中bin目录下面kafka-run-class.sh脚本中的JAVA_HOME
java·kafka
feilieren3 小时前
SpringBoot 搭建 SSE
java·spring boot·spring
阿岳3164 小时前
Java导出通过Word模板导出docx文件并通过QQ邮箱发送
java·开发语言