怎么运用IntelliJ IDEA创建:1-建立学生和课程表35 要求:学生可以选择多个课程,每个课程可以被多个学生选择。 查询某个学生所选的所有课程列表 查询某个课程的学生列表 学生可以修改所选的课程
时间: 2024-03-13 20:48:13 浏览: 103
IntelliJ IDEA使用
可以按照以下步骤在IntelliJ IDEA中创建一个Java项目,并使用JDBC连接MySQL数据库,实现学生和课程表的创建和操作:
1. 打开IntelliJ IDEA,点击"Create New Project",选择"Java"并点击"Next"。
2. 填写项目名称和项目路径,点击"Next"。
3. 在"Project SDK"中选择Java版本,点击"Next"。
4. 在"Additional Libraries and Frameworks"中选择"JDBC",点击"Next"。
5. 在"Project Template"中选择"Command Line App",点击"Next"。
6. 在"Command Line Options"中填写"run",点击"Finish"。
7. 在项目根目录下创建一个名为"lib"的文件夹,并将MySQL JDBC驱动程序(mysql-connector-java.jar)复制到该文件夹中。
8. 在项目根目录下创建一个名为"src"的文件夹,并在该文件夹下创建一个名为"main"的文件夹和一个名为"test"的文件夹。
9. 在"main"文件夹下创建一个名为"java"的文件夹,并在该文件夹下创建一个名为"com.example"的包。
10. 在"com.example"包下创建一个名为"Student"的类,用于表示学生信息,包括学生ID和姓名。在类中定义以下属性和方法:
```java
public class Student {
private int id;
private String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
```
11. 在"com.example"包下创建一个名为"Course"的类,用于表示课程信息,包括课程ID和名称。在类中定义以下属性和方法:
```java
public class Course {
private int id;
private String name;
public Course(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
```
12. 在"com.example"包下创建一个名为"StudentCourse"的类,用于表示学生和课程之间的关系。在类中定义以下属性和方法:
```java
public class StudentCourse {
private int id;
private int studentId;
private int courseId;
public StudentCourse(int id, int studentId, int courseId) {
this.id = id;
this.studentId = studentId;
this.courseId = courseId;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public int getCourseId() {
return courseId;
}
public void setCourseId(int courseId) {
this.courseId = courseId;
}
}
```
13. 在"com.example"包下创建一个名为"Database"的类,用于封装与MySQL数据库的连接和操作。在类中定义以下方法:
```java
import java.sql.*;
public class Database {
private Connection conn;
public Database(String url, String user, String password) throws SQLException {
conn = DriverManager.getConnection(url, user, password);
}
public void createStudentTable() throws SQLException {
String sql = "CREATE TABLE student (id INT PRIMARY KEY, name VARCHAR(255))";
try (Statement stmt = conn.createStatement()) {
stmt.executeUpdate(sql);
}
}
public void createCourseTable() throws SQLException {
String sql = "CREATE TABLE course (id INT PRIMARY KEY, name VARCHAR(255))";
try (Statement stmt = conn.createStatement()) {
stmt.executeUpdate(sql);
}
}
public void createStudentCourseTable() throws SQLException {
String sql = "CREATE TABLE student_course (id INT PRIMARY KEY, student_id INT, course_id INT, FOREIGN KEY (student_id) REFERENCES student(id), FOREIGN KEY (course_id) REFERENCES course(id))";
try (Statement stmt = conn.createStatement()) {
stmt.executeUpdate(sql);
}
}
public void addStudent(Student student) throws SQLException {
String sql = "INSERT INTO student (id, name) VALUES (?, ?)";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, student.getId());
pstmt.setString(2, student.getName());
pstmt.executeUpdate();
}
}
public void addCourse(Course course) throws SQLException {
String sql = "INSERT INTO course (id, name) VALUES (?, ?)";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, course.getId());
pstmt.setString(2, course.getName());
pstmt.executeUpdate();
}
}
public void addStudentCourse(StudentCourse studentCourse) throws SQLException {
String sql = "INSERT INTO student_course (id, student_id, course_id) VALUES (?, ?, ?)";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, studentCourse.getId());
pstmt.setInt(2, studentCourse.getStudentId());
pstmt.setInt(3, studentCourse.getCourseId());
pstmt.executeUpdate();
}
}
public void updateStudentCourse(int studentId, int oldCourseId, int newCourseId) throws SQLException {
String sql = "UPDATE student_course SET course_id = ? WHERE student_id = ? AND course_id = ?";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, newCourseId);
pstmt.setInt(2, studentId);
pstmt.setInt(3, oldCourseId);
pstmt.executeUpdate();
}
}
public ResultSet queryStudentCourses(int studentId) throws SQLException {
String sql = "SELECT course.* FROM student_course INNER JOIN course ON student_course.course_id = course.id WHERE student_course.student_id = ?";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, studentId);
return pstmt.executeQuery();
}
}
public ResultSet queryCourseStudents(int courseId) throws SQLException {
String sql = "SELECT student.* FROM student_course INNER JOIN student ON student_course.student_id = student.id WHERE student_course.course_id = ?";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, courseId);
return pstmt.executeQuery();
}
}
public void close() throws SQLException {
conn.close();
}
}
```
14. 在"com.example"包下创建一个名为"Main"的类,用于测试数据库操作。在类中定义以下方法:
```java
public class Main {
public static void main(String[] args) {
try {
Database db = new Database("jdbc:mysql://localhost:3306/test", "root", "password");
db.createStudentTable();
db.createCourseTable();
db.createStudentCourseTable();
Student s1 = new Student(1, "Tom");
Student s2 = new Student(2, "Jerry");
db.addStudent(s1);
db.addStudent(s2);
Course c1 = new Course(1, "Math");
Course c2 = new Course(2, "English");
db.addCourse(c1);
db.addCourse(c2);
StudentCourse sc1 = new StudentCourse(1, 1, 1);
StudentCourse sc2 = new StudentCourse(2, 1, 2);
StudentCourse sc3 = new StudentCourse(3, 2, 1);
db.addStudentCourse(sc1);
db.addStudentCourse(sc2);
db.addStudentCourse(sc3);
ResultSet rs1 = db.queryStudentCourses(1);
while (rs1.next()) {
System.out.println(rs1.getString("name"));
}
ResultSet rs2 = db.queryCourseStudents(1);
while (rs2.next()) {
System.out.println(rs2.getString("name"));
}
db.updateStudentCourse(1, 1, 2);
db.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
其中,"jdbc:mysql://localhost:3306/test"是MySQL数据库的连接字符串,"root"和"password"分别是数据库的用户名和密码。
以上就是使用IntelliJ IDEA创建学生和课程表的简单示例。通过这个示例,你可以了解到如何使用JDBC连接MySQL数据库,如何创建表、插入数据和查询数据等基本操作。
阅读全文