简单的学生信息管理系统

简单的学生信息管理系统

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


}
相关推荐
七星静香15 分钟前
laravel chunkById 分块查询 使用时的问题
java·前端·laravel
Jacob程序员15 分钟前
java导出word文件(手绘)
java·开发语言·word
ZHOUPUYU16 分钟前
IntelliJ IDEA超详细下载安装教程(附安装包)
java·ide·intellij-idea
stewie619 分钟前
在IDEA中使用Git
java·git
Elaine20239134 分钟前
06 网络编程基础
java·网络
G丶AEOM36 分钟前
分布式——BASE理论
java·分布式·八股
落落鱼201337 分钟前
tp接口 入口文件 500 错误原因
java·开发语言
想要打 Acm 的小周同学呀38 分钟前
LRU缓存算法
java·算法·缓存
镰刀出海41 分钟前
Recyclerview缓存原理
java·开发语言·缓存·recyclerview·android面试
瓜牛_gn2 小时前
mysql特性
数据库·mysql