|
|
@@ -0,0 +1,199 @@
|
|
|
+package space.anyi.studentManageSystem_JSP.dao;
|
|
|
+
|
|
|
+import space.anyi.studentManageSystem_JSP.model.Student;
|
|
|
+import space.anyi.studentManageSystem_JSP.util.JDBCUtil;
|
|
|
+
|
|
|
+import java.sql.*;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 学生数据访问对象类,实现对学生表的增删改查操作
|
|
|
+ */
|
|
|
+public class StudentDao {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加学生信息
|
|
|
+ * @param student 学生对象
|
|
|
+ * @return 是否添加成功
|
|
|
+ */
|
|
|
+ public boolean addStudent(Student student) {
|
|
|
+ Connection conn = null;
|
|
|
+ PreparedStatement pstmt = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ conn = JDBCUtil.getConnection();
|
|
|
+ String sql = "INSERT INTO student (name, student_id, class, dormitory_number, contact) VALUES (?, ?, ?, ?, ?)";
|
|
|
+ pstmt = conn.prepareStatement(sql);
|
|
|
+ pstmt.setString(1, student.getName());
|
|
|
+ pstmt.setString(2, student.getStudentId());
|
|
|
+ pstmt.setString(3, student.getClassName());
|
|
|
+ pstmt.setString(4, student.getDormitoryNumber());
|
|
|
+ pstmt.setString(5, student.getContact());
|
|
|
+
|
|
|
+ int result = pstmt.executeUpdate();
|
|
|
+ return result > 0;
|
|
|
+ } catch (SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return false;
|
|
|
+ } finally {
|
|
|
+ JDBCUtil.close(conn, pstmt);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据ID删除学生信息
|
|
|
+ * @param id 学生ID
|
|
|
+ * @return 是否删除成功
|
|
|
+ */
|
|
|
+ public boolean deleteStudent(int id) {
|
|
|
+ Connection conn = null;
|
|
|
+ PreparedStatement pstmt = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ conn = JDBCUtil.getConnection();
|
|
|
+ String sql = "DELETE FROM student WHERE id = ?";
|
|
|
+ pstmt = conn.prepareStatement(sql);
|
|
|
+ pstmt.setInt(1, id);
|
|
|
+
|
|
|
+ int result = pstmt.executeUpdate();
|
|
|
+ return result > 0;
|
|
|
+ } catch (SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return false;
|
|
|
+ } finally {
|
|
|
+ JDBCUtil.close(conn, pstmt);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新学生信息
|
|
|
+ * @param student 学生对象
|
|
|
+ * @return 是否更新成功
|
|
|
+ */
|
|
|
+ public boolean updateStudent(Student student) {
|
|
|
+ Connection conn = null;
|
|
|
+ PreparedStatement pstmt = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ conn = JDBCUtil.getConnection();
|
|
|
+ String sql = "UPDATE student SET name = ?, student_id = ?, class = ?, dormitory_number = ?, contact = ? WHERE id = ?";
|
|
|
+ pstmt = conn.prepareStatement(sql);
|
|
|
+ pstmt.setString(1, student.getName());
|
|
|
+ pstmt.setString(2, student.getStudentId());
|
|
|
+ pstmt.setString(3, student.getClassName());
|
|
|
+ pstmt.setString(4, student.getDormitoryNumber());
|
|
|
+ pstmt.setString(5, student.getContact());
|
|
|
+ pstmt.setInt(6, student.getId());
|
|
|
+
|
|
|
+ int result = pstmt.executeUpdate();
|
|
|
+ return result > 0;
|
|
|
+ } catch (SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return false;
|
|
|
+ } finally {
|
|
|
+ JDBCUtil.close(conn, pstmt);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据ID查询学生信息
|
|
|
+ * @param id 学生ID
|
|
|
+ * @return 学生对象,如果不存在返回null
|
|
|
+ */
|
|
|
+ public Student getStudentById(int id) {
|
|
|
+ Connection conn = null;
|
|
|
+ PreparedStatement pstmt = null;
|
|
|
+ ResultSet rs = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ conn = JDBCUtil.getConnection();
|
|
|
+ String sql = "SELECT * FROM student WHERE id = ?";
|
|
|
+ pstmt = conn.prepareStatement(sql);
|
|
|
+ pstmt.setInt(1, id);
|
|
|
+
|
|
|
+ rs = pstmt.executeQuery();
|
|
|
+ if (rs.next()) {
|
|
|
+ return resultSetToStudent(rs);
|
|
|
+ }
|
|
|
+ } catch (SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ JDBCUtil.close(conn, pstmt, rs);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据姓名查询学生信息
|
|
|
+ * @param name 学生姓名
|
|
|
+ * @return 学生列表
|
|
|
+ */
|
|
|
+ public List<Student> getStudentsByName(String name) {
|
|
|
+ Connection conn = null;
|
|
|
+ PreparedStatement pstmt = null;
|
|
|
+ ResultSet rs = null;
|
|
|
+ List<Student> students = new ArrayList<>();
|
|
|
+
|
|
|
+ try {
|
|
|
+ conn = JDBCUtil.getConnection();
|
|
|
+ String sql = "SELECT * FROM student WHERE name LIKE ?";
|
|
|
+ pstmt = conn.prepareStatement(sql);
|
|
|
+ pstmt.setString(1, "%" + name + "%");
|
|
|
+
|
|
|
+ rs = pstmt.executeQuery();
|
|
|
+ while (rs.next()) {
|
|
|
+ students.add(resultSetToStudent(rs));
|
|
|
+ }
|
|
|
+ } catch (SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ JDBCUtil.close(conn, pstmt, rs);
|
|
|
+ }
|
|
|
+ return students;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询所有学生信息
|
|
|
+ * @return 学生列表
|
|
|
+ */
|
|
|
+ public List<Student> getAllStudents() {
|
|
|
+ Connection conn = null;
|
|
|
+ PreparedStatement pstmt = null;
|
|
|
+ ResultSet rs = null;
|
|
|
+ List<Student> students = new ArrayList<>();
|
|
|
+
|
|
|
+ try {
|
|
|
+ conn = JDBCUtil.getConnection();
|
|
|
+ String sql = "SELECT * FROM student ORDER BY id DESC";
|
|
|
+ pstmt = conn.prepareStatement(sql);
|
|
|
+
|
|
|
+ rs = pstmt.executeQuery();
|
|
|
+ while (rs.next()) {
|
|
|
+ students.add(resultSetToStudent(rs));
|
|
|
+ }
|
|
|
+ } catch (SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ JDBCUtil.close(conn, pstmt, rs);
|
|
|
+ }
|
|
|
+ return students;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将ResultSet转换为Student对象
|
|
|
+ * @param rs ResultSet对象
|
|
|
+ * @return Student对象
|
|
|
+ * @throws SQLException
|
|
|
+ */
|
|
|
+ private Student resultSetToStudent(ResultSet rs) throws SQLException {
|
|
|
+ Student student = new Student();
|
|
|
+ student.setId(rs.getInt("id"));
|
|
|
+ student.setName(rs.getString("name"));
|
|
|
+ student.setStudentId(rs.getString("student_id"));
|
|
|
+ student.setClassName(rs.getString("class"));
|
|
|
+ student.setDormitoryNumber(rs.getString("dormitory_number"));
|
|
|
+ student.setContact(rs.getString("contact"));
|
|
|
+ return student;
|
|
|
+ }
|
|
|
+}
|