实训3

    技术2025-01-05  18

    本次实训创建数据访问接口实现类以及测试实现类中的各个方法 创建数据访问接口 CollegeDao、StatusDao、StudentDao与UserDao 一、创建学校数据访问接口 package net.zty.student.dao;

    import net.zty.student.bean.College;

    public interface CollegeDao { College findById(int id); int update(College college); } 二、创建状态数据访问接口 package net.zty.student.dao;

    import net.zty.student.bean.Status;

    public interface StatusDao { Status findById(int id); int update(Status status); } 三、创建学生数据访问接口 package net.zty.student.dao;

    import net.zty.student.bean.Student; import java.util.Vector; import java.util.List;

    public interface StudentDao { int insert(Student student);//插入学生记录 int deleteById(int id);//按编号删除学生记录 int deleteByClass(String clazz);//按班级删除学生记录 int deleteByDepartment(String department);//按系部删除学生记录 int update(Student student);//更新学生记录 Student findById(int id);//按编号查询学生 List findByName(String name);//按姓名查询学生记录 List findByClass(String clazz);//按班级查询学生姓名 List findByDepartment(String department);//按系部查询学生 List findByAll();//查询全部学生记录 Vector findRowsBySex();//按性别统计人数 Vector findRowsByClass();//按班级统计人数 Vector findRowsBydepartment();//按系部统计人数 } 四、创建用户数据访问接口 package net.zty.student.dao;

    import net.zty.student.bean.User; import java.util.List;

    public interface UserDao { int insert(User user); int deleteById(int id); int update(User user); User findById(int id); List findAll(); User loin(String username, String psaaword); boolean isUsernameExisted(String username);

    } 创建数据访问接口实现类 在dao下面建一个名为impl的子包,里面放实现类。 创建学校数据访问接口实现类CollegeDaoImpl package net.zty.student.dao.impl;

    import net.zty.student.bean.College; import net.zty.student.dao.CollegeDao; import net.zty.student.dbutile.ConnectionManager;

    import java.sql.*;

    /**

    功能:学校数据访问接口实现类作者:周天宇日期:2020年07月03日 */

    public class CollegeDaolmpl implements CollegeDao { /** * 按编号查询学校记录 * @param id * @return */

    @Override public College findById(int id) { //声明学校对象 College college = null; //1.获取数据库连接 Connection conn = ConnectionManager.getConnection(); try { //2.定义SQL字符串 String strSQL = "select * from t_college where id = ?"; //3.创建预备语句对象 PreparedStatement pstmt = conn.prepareStatement(strSQL); //4.设置占位符的值 pstmt.setInt(1,id); //5.执行SQL查询,返回结果集 ResultSet rs = pstmt.executeQuery(); //6.判断结果集是否为空 if (rs.next()){ //7.创建学校实体对象 college = new College(); //8.用当前记录字段值设置实体属性 college.setId(rs.getInt("id")); college.setName(rs.getString("name")); college.setPresident(rs.getString("president")); college.setStartTime(rs.getTimestamp("start_time")); college.setTelephone(rs.getString("telephone")); college.setEmail(rs.getString("email")); college.setAddress(rs.getString("address")); college.setProfile(rs.getString("profile")); } } catch (SQLException e) { e.printStackTrace(); } finally { //9.关闭数据库连接 ConnectionManager.closeConnection(conn); } //返回学校对象 return college; } @Override public int update(College college) { //定义更新的记录数 int count = 0; //1.获取数据库连接 Connection conn = ConnectionManager.getConnection(); //定义SQL字符串 String strSQL = "update t_college set name = ?,president = ?,start_time = ?," +"telephone = ?,email = ?,address = ?,profile = ? where id = ?"; try { //3.创建预备语句对象 PreparedStatement pstmt = conn.prepareStatement(strSQL); //4.设置占位符的值 pstmt.setString(1,college.getName()); pstmt.setString(2,college.getPresident()); pstmt.setTimestamp(3,new Timestamp(college.getStartTime().getTime())); pstmt.setString(4,college.getTelephone()); pstmt.setString(5,college.getEmail()); pstmt.setString(6,college.getAddress()); pstmt.setString(7,college.getProfile()); pstmt.setInt(8,college.getId()); //5.执行SQL,返回更新记录数 count = pstmt.executeUpdate(); //6.关闭预备语句对象 pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } finally { //7.关闭数据库连接 ConnectionManager.closeConnection(conn); } //返回更新记录数 return count; }

    } 对CollegeDaoImpl进行单元测试创建Test 出现错误 解决方法:将鼠标移到@Test,按+组合键,弹出快捷菜单,选择第一项Add ‘JUnit4’ to classpath 编写单元测试方法testFindById()代码 编写测试方法testUpdate() 创建用户数据访问接口实现类 package net.zty.student.dao.impl;

    import net.zty.student.bean.User; import net.zty.student.dao.UserDao; import net.zty.student.dbutile.ConnectionManager;

    import java.sql.*; import java.util.ArrayList; import java.util.List;

    /** * 功能:用户数据访问接口实现类 * 作者:周天宇 * 日期:2020年07月04日 */ public class UserDaolmpl implements UserDao { /** * 插入用户记录 * * @param user * @return 插入记录数 */ @Override public int insert(User user) { // 定义插入记录数 int count = 0; // 1. 获得数据库连接 Connection conn = ConnectionManager.getConnection(); // 2. 定义SQL字符串 String strSQL = "insert into t_user (username, password, telephone, register_time)" + " values (?, ?, ?, ?)"; // 不允许用户表里插入两条用户名相同的记录 if (!isUsernameExisted(user.getUsername())) { try { // 3. 创建预备语句对象 PreparedStatement pstmt = conn.prepareStatement(strSQL); // 4. 设置占位符的值 pstmt.setString(1, user.getUsername()); pstmt.setString(2, user.getPassword()); pstmt.setString(3, user.getTelephone()); pstmt.setTimestamp(4, new Timestamp(user.getRegisterTime().getTime())); // 5. 执行SQL,返回插入记录数 count = pstmt.executeUpdate(); // 6. 关闭预备语句对象 pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } finally { // 关闭数据库连接 ConnectionManager.closeConnection(conn); } } // 返回插入记录数 return count; } /** * 按id删除用户记录 * * @param id * @return 删除记录数 */ @Override public int deleteById(int id) { // 定义删除记录数 int count = 0; // 1. 获取数据库连接 Connection conn = ConnectionManager.getConnection(); // 2. 定义SQL字符串 String strSQL = "delete from t_user where id = ?"; try { // 3. 创建预备语句对象 PreparedStatement pstmt = conn.prepareStatement(strSQL); // 4. 设置占位符的值 pstmt.setInt(1, id); // 5. 执行SQL,返回删除记录数 count = pstmt.executeUpdate(); // 6. 关闭预备语句对象 pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } finally { // 关闭数据库连接 ConnectionManager.closeConnection(conn); } // 返回删除记录数 return count; } /** * 更新用户记录 * * @param user * @return 更新记录数 */ @Override public int update(User user) { // 定义更新记录数 int count = 0; // 1. 获得数据库连接 Connection conn = ConnectionManager.getConnection(); // 2. 定义SQL字符串 String strSQL = "update t_user set username = ?, password = ?, telephone = ?," + " register_time = ? where id = ?"; try { // 3. 创建预备语句对象 PreparedStatement pstmt = conn.prepareStatement(strSQL); // 4. 设置占位符的值 pstmt.setString(1, user.getUsername()); pstmt.setString(2, user.getPassword()); pstmt.setString(3, user.getTelephone()); pstmt.setTimestamp(4, new Timestamp(user.getRegisterTime().getTime())); pstmt.setInt(5, user.getId()); // 5. 执行SQL,返回更新记录数 count = pstmt.executeUpdate(); // 6. 关闭预备语句对象 pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } finally { // 关闭数据库连接 ConnectionManager.closeConnection(conn); } // 返回更新记录数 return count; } /** * 按id查询用户 * * @param id * @return 用户实体 */ @Override public User findById(int id) { // 声明用户对象 User user = null; // 1. 获取数据库连接对象 Connection conn = ConnectionManager.getConnection(); // 2. 定义SQL字符串 String strSQL = "select * from t_user where id = ?"; try { // 3. 创建预备语句对象 PreparedStatement pstmt = conn.prepareStatement(strSQL); // 4. 设置占位符的值 pstmt.setInt(1, id); // 5. 执行SQL,返回结果集 ResultSet rs = pstmt.executeQuery(); // 6. 判断结果集是否有记录 if (rs.next()) { // 创建用户实体 user = new User(); // 利用当前记录各字段值设置用户实体属性 user.setId(rs.getInt("id")); user.setUsername(rs.getString("username")); user.setPassword(rs.getString("password")); user.setTelephone(rs.getString("telephone")); user.setRegisterTime(rs.getTimestamp("register_time")); } } catch (SQLException e) { e.printStackTrace(); } finally { // 关闭数据库连接 ConnectionManager.closeConnection(conn); } // 返回用户对象 return user; } /** * 查询所有用户 * * @return 用户列表 */ @Override public List<User> findAll() { // 声明用户列表 List<User> users = new ArrayList<User>(); // 1. 获取数据库连接对象 Connection conn = ConnectionManager.getConnection(); // 2. 定义SQL字符串 String strSQL = "select * from t_user"; try { // 3. 创建语句对象 Statement stmt = conn.createStatement(); // 4. 执行SQL,返回结果集 ResultSet rs = stmt.executeQuery(strSQL); // 5. 遍历结果集 while (rs.next()) { // 创建用户实体 User user = new User(); // 利用当前记录各字段值设置用户实体属性 user.setId(rs.getInt("id")); user.setUsername(rs.getString("username")); user.setPassword(rs.getString("password")); user.setTelephone(rs.getString("telephone")); user.setRegisterTime(rs.getTimestamp("register_time")); // 将实体添加到用户列表 users.add(user); } // 6. 关闭结果集 rs.close(); // 7. 关闭语句对象 stmt.close(); } catch (SQLException e) { e.printStackTrace(); } finally { // 关闭数据库连接 ConnectionManager.closeConnection(conn); } // 返回用户列表 return users; } @Override public User loin(String username, String psaaword) { return null; } /** * 用户登录 * * @param username * @param password * @return 登录用户实体 */ @Override public User login(String username, String password) { // 声明用户对象 User user = null; // 1. 获取数据库连接 Connection conn = ConnectionManager.getConnection(); // 2. 定义SQL字符串 String strSQL = "select * from t_user where username = ? and password = ?"; try { // 3. 创建预备语句对象 PreparedStatement pstmt = conn.prepareStatement(strSQL); // 4. 设置占位符的值 pstmt.setString(1, username); pstmt.setString(2, password); // 5. 执行SQL,返回结果集 ResultSet rs = pstmt.executeQuery(); // 6. 判断结果集是否有记录 if (rs.next()) { // 实例化用户 user = new User(); // 利用当前记录各字段值设置用户实体属性 user.setId(rs.getInt("id")); user.setUsername(rs.getString("username")); user.setPassword(rs.getString("password")); user.setTelephone(rs.getString("telephone")); user.setRegisterTime(rs.getTimestamp("register_time")); } } catch (SQLException e) { e.printStackTrace(); } finally { // 关闭数据库连接 ConnectionManager.closeConnection(conn); } // 返回用户对象 return user; } @Override public boolean isUsernameExisted(String username) { // 定义存在与否变量 boolean existed = false; // 1. 获取数据库连接 Connection conn = ConnectionManager.getConnection(); // 2. 定义SQL字符串 String strSQL = "select * from t_user where username = ?"; try { // 3. 创建预备语句对象 PreparedStatement pstmt = conn.prepareStatement(strSQL); // 4. 设置占位符的值 pstmt.setString(1, username); // 5. 执行SQL,返回结果集 ResultSet rs = pstmt.executeQuery(); // 6. 判断结果集是否有记录 if (rs.next()) { existed = true; } // 7. 关闭预备语句对象 pstmt.close(); // 8. 关闭结果集对象 rs.close(); } catch (SQLException e) { e.printStackTrace(); } finally { // 关闭数据库连接 ConnectionManager.closeConnection(conn); } // 返回存在与否变量 return existed; }

    } 对UserDaoImpl进行单元测试

    Processed: 0.008, SQL: 9