
开发环境准备
确保已安装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);
}
注意事项
- 使用PreparedStatement防止SQL注入
- 数据库连接及时关闭避免资源泄漏
- 对用户输入进行有效性验证
- 重要操作添加事务处理
- 使用Logger代替System.out打印日志
项目结构建议采用Maven标准目录布局,添加mysql-connector-java依赖。完整实现应考虑异常处理、日志记录等生产级要求。
Java控制台学生选课系统开发

被折叠的 条评论
为什么被折叠?



