使用jdbd和三层结构写一个学生管理系统
时间: 2024-05-11 13:20:16 浏览: 123
学生管理系统是一个比较典型的 CRUD 应用,使用 JDBC 和三层结构可以实现对学生信息的增、删、改、查等操作。
首先,我们需要在数据库中创建一个学生表,例如:
```
CREATE TABLE student (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
gender VARCHAR(10) NOT NULL,
age INT NOT NULL,
department VARCHAR(50) NOT NULL
);
```
然后,我们可以定义一个 Student 类来表示学生信息,例如:
```java
public class Student {
private int id;
private String name;
private String gender;
private int age;
private String department;
// 构造方法、getter 和 setter 略
}
```
接着,我们可以定义一个 DAO 层来操作数据库中的学生表,例如:
```java
public class StudentDao {
// 数据库连接参数
private static final String URL = "jdbc:mysql://localhost:3306/test";
private static final String USERNAME = "root";
private static final String PASSWORD = "123456";
// 获取连接
private Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, USERNAME, PASSWORD);
}
// 添加学生
public void addStudent(Student student) {
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement("INSERT INTO student (name, gender, age, department) VALUES (?, ?, ?, ?)")) {
stmt.setString(1, student.getName());
stmt.setString(2, student.getGender());
stmt.setInt(3, student.getAge());
stmt.setString(4, student.getDepartment());
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 删除学生
public void deleteStudent(int id) {
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement("DELETE FROM student WHERE id = ?")) {
stmt.setInt(1, id);
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 修改学生
public void updateStudent(Student student) {
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement("UPDATE student SET name = ?, gender = ?, age = ?, department = ? WHERE id = ?")) {
stmt.setString(1, student.getName());
stmt.setString(2, student.getGender());
stmt.setInt(3, student.getAge());
stmt.setString(4, student.getDepartment());
stmt.setInt(5, student.getId());
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 获取学生列表
public List<Student> getStudentList() {
List<Student> studentList = new ArrayList<>();
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM student")) {
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setGender(rs.getString("gender"));
student.setAge(rs.getInt("age"));
student.setDepartment(rs.getString("department"));
studentList.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
}
return studentList;
}
// 根据 ID 获取学生
public Student getStudentById(int id) {
Student student = null;
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM student WHERE id = ?")) {
stmt.setInt(1, id);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setGender(rs.getString("gender"));
student.setAge(rs.getInt("age"));
student.setDepartment(rs.getString("department"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return student;
}
}
```
最后,我们可以定义一个 Service 层来调用 DAO 层的方法,例如:
```java
public class StudentService {
private StudentDao studentDao = new StudentDao();
// 添加学生
public void addStudent(Student student) {
studentDao.addStudent(student);
}
// 删除学生
public void deleteStudent(int id) {
studentDao.deleteStudent(id);
}
// 修改学生
public void updateStudent(Student student) {
studentDao.updateStudent(student);
}
// 获取学生列表
public List<Student> getStudentList() {
return studentDao.getStudentList();
}
// 根据 ID 获取学生
public Student getStudentById(int id) {
return studentDao.getStudentById(id);
}
}
```
这样,我们就实现了一个简单的学生管理系统,可以通过调用 Service 层的方法来对学生信息进行增、删、改、查等操作。
阅读全文