玩转Java控制台 用MySQL做后端,开发一个炫酷的字符界面学生选课系统

Java控制台学生选课系统开发

开发环境准备

确保已安装JDK 8+、MySQL 8.0+。推荐使用IntelliJ IDEA或Eclipse作为IDE。MySQL需提前配置好用户名和密码(如root/123456)。

数据库设计

创建数据库student_course,设计三张核心表:

CREATE TABLE `student` (
  `id` int PRIMARY KEY AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `password` varchar(50) NOT NULL
);

CREATE TABLE `course` (
  `id` int PRIMARY KEY AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `teacher` varchar(50) NOT NULL,
  `credit` int NOT NULL
);

CREATE TABLE `selection` (
  `student_id` int NOT NULL,
  `course_id` int NOT NULL,
  PRIMARY KEY (`student_id`, `course_id`),
  FOREIGN KEY (`student_id`) REFERENCES `student` (`id`),
  FOREIGN KEY (`course_id`) REFERENCES `course` (`id`)
);

控制台UI设计

使用ANSI转义码实现彩色输出:

public class ConsoleUI {
    public static final String RESET = "\033[0m";
    public static final String RED = "\033[31m";
    public static final String GREEN = "\033[32m";
    public static final String YELLOW = "\033[33m";
    
    public static void printHeader() {
        System.out.println(GREEN + "╔════════════════════════════╗");
        System.out.println("║   学生选课管理系统   ║");
        System.out.println("╚════════════════════════════╝" + RESET);
    }
}

JDBC连接管理

创建数据库连接工具类:

public class DBUtil {
    private static final String URL = "jdbc:mysql://localhost:3306/student_course";
    private static final String USER = "root";
    private static final String PASSWORD = "123456";

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URL, USER, PASSWORD);
    }
}

核心功能实现

学生登录验证:

public boolean validateStudent(int id, String password) {
    String sql = "SELECT * FROM student WHERE id=? AND password=?";
    try (Connection conn = DBUtil.getConnection();
         PreparedStatement stmt = conn.prepareStatement(sql)) {
        stmt.setInt(1, id);
        stmt.setString(2, password);
        return stmt.executeQuery().next();
    } catch (SQLException e) {
        e.printStackTrace();
        return false;
    }
}

课程查询与选择:

public List<Course> getAvailableCourses() {
    List<Course> courses = new ArrayList<>();
    String sql = "SELECT * FROM course WHERE id NOT IN " +
                 "(SELECT course_id FROM selection WHERE student_id=?)";
    // 实现查询逻辑
    return courses;
}

public boolean selectCourse(int studentId, int courseId) {
    String sql = "INSERT INTO selection VALUES(?, ?)";
    try (Connection conn = DBUtil.getConnection();
         PreparedStatement stmt = conn.prepareStatement(sql)) {
        stmt.setInt(1, studentId);
        stmt.setInt(2, courseId);
        return stmt.executeUpdate() > 0;
    } catch (SQLException e) {
        e.printStackTrace();
        return false;
    }
}

交互流程设计

主菜单循环结构:

while (true) {
    ConsoleUI.printMainMenu();
    int choice = scanner.nextInt();
    switch (choice) {
        case 1:
            handleLogin();
            break;
        case 2:
            handleCourseSelection();
            break;
        case 3:
            System.exit(0);
    }
}

增强功能建议

添加进度条显示:

public static void showProgressBar(int progress) {
    System.out.print("\r[");
    for (int i = 0; i < 50; i++) {
        System.out.print(i < progress / 2 ? "=" : " ");
    }
    System.out.printf("] %d%%", progress);
}

实现ASCII艺术字:

public static void printAsciiArt() {
    String art = 
        "  _____ _           _   _      _____           _       _   \n" +
        " / ____| |         | | | |    / ____|         (_)     | |  \n" +
        "| (___ | |__  _   _| |_| |_  | (___   ___  ___ _  __ _| |_ \n" +
        " \\___ \\| '_ \\| | | | __| __|  \\___ \\ / _ \\/ __| |/ _` | __|\n" +
        " ____) | | | | |_| | |_| |_   ____) |  __/ (__| | (_| | |_ \n" +
        "|_____/|_| |_|\\__,_|\\__|\\__| |_____/ \\___|\\___|_|\\__,_|\\__|\n";
    System.out.println(YELLOW + art + RESET);
}

注意事项

  1. 使用PreparedStatement防止SQL注入
  2. 数据库连接及时关闭避免资源泄漏
  3. 对用户输入进行有效性验证
  4. 重要操作添加事务处理
  5. 使用Logger代替System.out打印日志

项目结构建议采用Maven标准目录布局,添加mysql-connector-java依赖。完整实现应考虑异常处理、日志记录等生产级要求。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值