简单的学生信息管理系统

简单的学生信息管理系统

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();
        }
    }


}
相关推荐
王强你强18 分钟前
MySQL 高级查询:JOIN、子查询、窗口函数
数据库·mysql
草巾冒小子19 分钟前
brew 安装mysql,启动,停止,重启
数据库·mysql
腥臭腐朽的日子熠熠生辉39 分钟前
解决maven失效问题(现象:maven中只有jdk的工具包,没有springboot的包)
java·spring boot·maven
ejinxian41 分钟前
Spring AI Alibaba 快速开发生成式 Java AI 应用
java·人工智能·spring
杉之1 小时前
SpringBlade 数据库字段的自动填充
java·笔记·学习·spring·tomcat
圈圈编码1 小时前
Spring Task 定时任务
java·前端·spring
俏布斯1 小时前
算法日常记录
java·算法·leetcode
27669582921 小时前
美团民宿 mtgsig 小程序 mtgsig1.2 分析
java·python·小程序·美团·mtgsig·mtgsig1.2·美团民宿
爱的叹息1 小时前
Java 连接 Redis 的驱动(Jedis、Lettuce、Redisson、Spring Data Redis)分类及对比
java·redis·spring
程序猿chen2 小时前
《JVM考古现场(十五):熵火燎原——从量子递归到热寂晶壁的代码涅槃》
java·jvm·git·后端·java-ee·区块链·量子计算