• 简单的学生信息管理系统


    简单的学生信息管理系统

    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();
            }
        }
    
    
    }
    
  • 相关阅读:
    Flink SQl 语法(hint,with,select,分组窗口聚合,时间属性(处理,事件))
    unity快捷键
    JAVA 中集合取交集
    【风控】评分卡建模的流程和要点
    [附源码]java毕业设计汽车票售票系统lunwen
    微信API接口、微信二次开发API调用
    适配器模式
    【双指针】:Leetcode1089.复写零
    使用ChatGPT高效完成简历制作[中篇]-有爱AI实战教程(五)
    一文速通MybatisPlus
  • 原文地址:https://blog.csdn.net/CRH1129/article/details/139478834