用java和myql写一个学生选课系统
时间: 2024-03-05 12:47:48 浏览: 145
好的,学生选课系统可以分为三个主要部分:学生信息管理、课程信息管理和选课管理。以下是一个简单的Java和MySQL实现,可以供您参考:
1. 学生信息管理
创建一个名为Student的Java类,用于存储学生信息,包括学号、姓名、年龄、性别等。
```java
public 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;
}
// getter and setter methods
}
```
在MySQL数据库中创建一个名为students的表,用于存储学生信息。表格包含id、name、age和gender列。
```sql
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT,
gender VARCHAR(10)
);
```
使用JDBC连接MySQL数据库,并编写代码实现学生信息的增加、删除、修改和查询操作。
```java
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class StudentDAO {
private Connection conn;
public StudentDAO() {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "root";
conn = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
public void addStudent(Student student) {
String sql = "INSERT INTO students(id, name, age, gender) VALUES(?, ?, ?, ?)";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, student.getId());
pstmt.setString(2, student.getName());
pstmt.setInt(3, student.getAge());
pstmt.setString(4, student.getGender());
pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void deleteStudent(int id) {
String sql = "DELETE FROM students WHERE id=?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void updateStudent(Student student) {
String sql = "UPDATE students SET name=?, age=?, gender=? WHERE id=?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, student.getName());
pstmt.setInt(2, student.getAge());
pstmt.setString(3, student.getGender());
pstmt.setInt(4, student.getId());
pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public List<Student> getAllStudents() {
List<Student> students = new ArrayList<>();
String sql = "SELECT * FROM students";
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
String gender = rs.getString("gender");
students.add(new Student(id, name, age, gender));
}
rs.close();
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
return students;
}
public Student getStudentById(int id) {
Student student = null;
String sql = "SELECT * FROM students WHERE id=?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
String name = rs.getString("name");
int age = rs.getInt("age");
String gender = rs.getString("gender");
student = new Student(id, name, age, gender);
}
rs.close();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
return student;
}
}
```
2. 课程信息管理
创建一个名为Course的Java类,用于存储课程信息,包括课程编号、课程名称、学分等。
```java
public class Course {
private int id;
private String name;
private int credit;
public Course(int id, String name, int credit) {
this.id = id;
this.name = name;
this.credit = credit;
}
// getter and setter methods
}
```
在MySQL数据库中创建一个名为courses的表,用于存储课程信息。表格包含id、name和credit列。
```sql
CREATE TABLE courses (
id INT PRIMARY KEY,
name VARCHAR(50),
credit INT
);
```
使用JDBC连接MySQL数据库,并编写代码实现课程信息的增加、删除、修改和查询操作。
```java
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class CourseDAO {
private Connection conn;
public CourseDAO() {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "root";
conn = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
public void addCourse(Course course) {
String sql = "INSERT INTO courses(id, name, credit) VALUES(?, ?, ?)";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, course.getId());
pstmt.setString(2, course.getName());
pstmt.setInt(3, course.getCredit());
pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void deleteCourse(int id) {
String sql = "DELETE FROM courses WHERE id=?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void updateCourse(Course course) {
String sql = "UPDATE courses SET name=?, credit=? WHERE id=?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, course.getName());
pstmt.setInt(2, course.getCredit());
pstmt.setInt(3, course.getId());
pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public List<Course> getAllCourses() {
List<Course> courses = new ArrayList<>();
String sql = "SELECT * FROM courses";
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int credit = rs.getInt("credit");
courses.add(new Course(id, name, credit));
}
rs.close();
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
return courses;
}
public Course getCourseById(int id) {
Course course = null;
String sql = "SELECT * FROM courses WHERE id=?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
String name = rs.getString("name");
int credit = rs.getInt("credit");
course = new Course(id, name, credit);
}
rs.close();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
return course;
}
}
```
3. 选课管理
创建一个名为CourseSelection的Java类,用于存储选课信息,包括学号、课程编号等。
```java
public class CourseSelection {
private int studentId;
private int courseId;
public CourseSelection(int studentId, int courseId) {
this.studentId = studentId;
this.courseId = courseId;
}
// getter and setter methods
}
```
在MySQL数据库中创建一个名为course_selection的表,用于存储选课信息。表格包含student_id和course_id列。
```sql
CREATE TABLE course_selection (
student_id INT,
course_id INT,
PRIMARY KEY(student_id, course_id),
FOREIGN KEY(student_id) REFERENCES students(id),
FOREIGN KEY(course_id) REFERENCES courses(id)
);
```
使用JDBC连接MySQL数据库,并编写代码实现选课和退课操作。
```java
import java.sql.*;
public class CourseSelectionDAO {
private Connection conn;
public CourseSelectionDAO() {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "root";
conn = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
public void selectCourse(int studentId, int courseId) {
String sql = "INSERT INTO course_selection(student_id, course_id) VALUES(?, ?)";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, studentId);
pstmt.setInt(2, courseId);
pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void dropCourse(int studentId, int courseId) {
String sql = "DELETE FROM course_selection WHERE student_id=? AND course_id=?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, studentId);
pstmt.setInt(2, courseId);
pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
以上是一个简单的学生选课系统的Java和MySQL实现,您可以根据自己的需要进行修改和扩展。
阅读全文