实现代码
时间: 2023-10-26 12:15:23 浏览: 72
很抱歉,由于时间和篇幅的限制,这里无法提供完整的实现代码。但是,我可以为您提供一些参考资料,帮助您开始实现这个学生管理系统。
首先,您需要设置与数据库的连接。您可以使用JDBC或者其他ORM框架来实现这个连接。以下是使用JDBC连接MySQL数据库的示例代码:
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBUtil {
// MySQL数据库的连接信息
private static final String URL = "jdbc:mysql://localhost:3306/students";
private static final String USER = "root";
private static final String PASSWORD = "******";
// 获取数据库连接
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, USER, PASSWORD);
}
}
```
然后,您需要编写一个DAO层的代码,用于与数据库进行交互。以下是一个使用JDBC实现的StudentDAO类的示例:
```java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class StudentDAO {
// 查询所有学生信息
public List<Student> findAll() {
List<Student> students = new ArrayList<>();
try (Connection conn = DBUtil.getConnection();
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM students");
ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
student.setGender(rs.getString("gender"));
student.setMajor(rs.getString("major"));
students.add(student);
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return students;
}
// 根据ID查询学生信息
public Student findById(int id) {
Student student = null;
try (Connection conn = DBUtil.getConnection();
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM students WHERE id=?")) {
stmt.setInt(1, id);
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
student.setGender(rs.getString("gender"));
student.setMajor(rs.getString("major"));
}
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return student;
}
// 添加学生信息
public void add(Student student) {
try (Connection conn = DBUtil.getConnection();
PreparedStatement stmt = conn.prepareStatement("INSERT INTO students(name, age, gender, major) VALUES(?,?,?,?)")) {
stmt.setString(1, student.getName());
stmt.setInt(2, student.getAge());
stmt.setString(3, student.getGender());
stmt.setString(4, student.getMajor());
stmt.executeUpdate();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
// 根据ID删除学生信息
public void delete(int id) {
try (Connection conn = DBUtil.getConnection();
PreparedStatement stmt = conn.prepareStatement("DELETE FROM students WHERE id=?")) {
stmt.setInt(1, id);
stmt.executeUpdate();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
// 更新学生信息
public void update(Student student) {
try (Connection conn = DBUtil.getConnection();
PreparedStatement stmt = conn.prepareStatement("UPDATE students SET name=?, age=?, gender=?, major=? WHERE id=?")) {
stmt.setString(1, student.getName());
stmt.setInt(2, student.getAge());
stmt.setString(3, student.getGender());
stmt.setString(4, student.getMajor());
stmt.setInt(5, student.getId());
stmt.executeUpdate();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
```
最后,您需要编写一个控制器类,用于处理来自用户界面的请求,并调用DAO层的代码来处理这些请求。以下是一个使用Spring MVC框架实现的控制器类的示例:
```java
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Controller
@RequestMapping("/students")
public class StudentController {
private StudentDAO studentDAO = new StudentDAO();
// 显示所有学生信息
@GetMapping
public String list(Model model) {
List<Student> students = studentDAO.findAll();
model.addAttribute("students", students);
return "list";
}
// 显示添加学生信息的界面
@GetMapping("/add")
public String add(Model model) {
model.addAttribute("student", new Student());
return "add";
}
// 处理添加学生信息的请求
@PostMapping("/add")
public String add(@ModelAttribute("student") Student student) {
studentDAO.add(student);
return "redirect:/students";
}
// 显示更新学生信息的界面
@GetMapping("/update/{id}")
public String update(@PathVariable("id") int id, Model model) {
Student student = studentDAO.findById(id);
model.addAttribute("student", student);
return "update";
}
// 处理更新学生信息的请求
@PostMapping("/update")
public String update(@ModelAttribute("student") Student student) {
studentDAO.update(student);
return "redirect:/students";
}
// 处理删除学生信息的请求
@GetMapping("/delete/{id}")
public String delete(@PathVariable("id") int id) {
studentDAO.delete(id);
return "redirect:/students";
}
}
```
希望这些示例代码能够帮助您开始实现这个学生管理系统。
阅读全文