用Java 和MySQL做一个学生选课系统
时间: 2024-01-30 10:02:28 浏览: 158
好的,以下是一个使用 Java 和 MySQL 的学生选课系统的代码示例:
首先,需要建立一个名为 `student_course_system` 的数据库,并在其中创建两个表格:`students` 和 `courses`。
`students` 表格包含 `id`、`name`、`age` 和 `gender` 四个字段,其中 `id` 是主键;`courses` 表格包含 `id`、`name`、`teacher` 和 `capacity` 四个字段,其中 `id` 是主键。这两个表格可以通过以下 SQL 语句创建:
```sql
CREATE DATABASE student_course_system;
USE student_course_system;
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT,
gender VARCHAR(10)
);
CREATE TABLE courses (
id INT PRIMARY KEY,
name VARCHAR(50),
teacher VARCHAR(50),
capacity INT
);
```
接下来,可以使用 Java 中的 JDBC API 连接到 MySQL 数据库,并实现学生选课系统的功能。以下是一个简单的代码示例:
```java
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class StudentCourseSystem {
// 数据库连接信息
private static final String URL = "jdbc:mysql://localhost:3306/student_course_system";
private static final String USERNAME = "root";
private static final String PASSWORD = "password";
// 数据库连接对象
private Connection connection;
public StudentCourseSystem() {
try {
// 建立数据库连接
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
}
// 关闭数据库连接
public void close() {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 添加学生
public void addStudent(Student student) {
try {
PreparedStatement statement = connection.prepareStatement("INSERT INTO students VALUES (?, ?, ?, ?)");
statement.setInt(1, student.getId());
statement.setString(2, student.getName());
statement.setInt(3, student.getAge());
statement.setString(4, student.getGender());
statement.executeUpdate();
System.out.println("添加学生成功!");
} catch (SQLException e) {
e.printStackTrace();
}
}
// 添加课程
public void addCourse(Course course) {
try {
PreparedStatement statement = connection.prepareStatement("INSERT INTO courses VALUES (?, ?, ?, ?)");
statement.setInt(1, course.getId());
statement.setString(2, course.getName());
statement.setString(3, course.getTeacher());
statement.setInt(4, course.getCapacity());
statement.executeUpdate();
System.out.println("添加课程成功!");
} catch (SQLException e) {
e.printStackTrace();
}
}
// 学生选课
public void selectCourse(Student student, Course course) {
try {
// 检查课程容量是否已满
PreparedStatement capacityStatement = connection.prepareStatement("SELECT COUNT(*) FROM selections WHERE course_id = ?");
capacityStatement.setInt(1, course.getId());
ResultSet capacityResult = capacityStatement.executeQuery();
capacityResult.next();
int selectedCount = capacityResult.getInt(1);
if (selectedCount == course.getCapacity()) {
System.out.println("课程容量已满!");
return;
}
// 添加选课记录
PreparedStatement statement = connection.prepareStatement("INSERT INTO selections VALUES (?, ?)");
statement.setInt(1, student.getId());
statement.setInt(2, course.getId());
statement.executeUpdate();
System.out.println("学生选课成功!");
} catch (SQLException e) {
e.printStackTrace();
}
}
// 查询学生的选课情况
public void querySelections(Student student) {
try {
// 查询学生选课记录
PreparedStatement statement = connection.prepareStatement("SELECT * FROM selections WHERE student_id = ?");
statement.setInt(1, student.getId());
ResultSet result = statement.executeQuery();
ArrayList<Course> selectedCourses = new ArrayList<>();
while (result.next()) {
int courseId = result.getInt("course_id");
PreparedStatement courseStatement = connection.prepareStatement("SELECT * FROM courses WHERE id = ?");
courseStatement.setInt(1, courseId);
ResultSet courseResult = courseStatement.executeQuery();
if (courseResult.next()) {
String courseName = courseResult.getString("name");
String teacher = courseResult.getString("teacher");
int capacity = courseResult.getInt("capacity");
Course course = new Course(courseId, courseName, teacher, capacity);
selectedCourses.add(course);
}
}
// 输出选课情况
System.out.println(student.getName() + "选了以下课程:");
for (Course course : selectedCourses) {
System.out.println(course.getName() + "(" + course.getTeacher() + ")");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
StudentCourseSystem system = new StudentCourseSystem();
Scanner scanner = new Scanner(System.in);
// 添加学生
System.out.print("请输入学生姓名:");
String name = scanner.nextLine();
System.out.print("请输入学生年龄:");
int age = scanner.nextInt();
scanner.nextLine();
System.out.print("请输入学生性别:");
String gender = scanner.nextLine();
Student student = new Student(0, name, age, gender);
system.addStudent(student);
// 添加课程
System.out.print("请输入课程名称:");
String courseName = scanner.nextLine();
System.out.print("请输入授课教师:");
String teacher = scanner.nextLine();
System.out.print("请输入课程容量:");
int capacity = scanner.nextInt();
Course course = new Course(0, courseName, teacher, capacity);
system.addCourse(course);
// 学生选课
system.selectCourse(student, course);
// 查询选课情况
system.querySelections(student);
// 关闭数据库连接
system.close();
}
}
class Student {
private int id;
private String name;
private int age;
private String gender;
public Student(int id, String name, int age, String gender) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
}
class Course {
private int id;
private String name;
private String teacher;
private int capacity;
public Course(int id, String name, String teacher, int capacity) {
this.id = id;
this.name = name;
this.teacher = teacher;
this.capacity = capacity;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getTeacher() {
return teacher;
}
public int getCapacity() {
return capacity;
}
}
```
以上代码中使用了 `PreparedStatement` 来防止 SQL 注入攻击。同时,为了防止课程容量超限,还需要先查询选课记录中已经选了该课程的人数。如果已经选课的人数达到了课程容量,就不能再选这门课程了。
阅读全文