java的实训日志5

文章目录

八、项目开发实现步骤

(七)创建数据访问接口实现类

在net.huawei.student.dao包里创建impl子包(impl: implementation)
在这里插入图片描述

1、创建学校数据访问接口实现类

在net.huawei.student.dao.impl包里创建CollegeDaoImpl类
在这里插入图片描述
实现CollegeDao接口
在这里插入图片描述
选择要实现的抽象方法
在这里插入图片描述
单击【OK】按钮
在这里插入图片描述

(1)编写按标识符查询学校记录方法
@Override // 按标识符查询学校记录                                                                              
public College findById(int id) {                                                      
    // 定义学校对象                                                                          
    College college = null;                                                            
                                                                                       
    // 获取数据库连接                                                                         
    Connection conn = ConnectionManager.getConnection();                               
    // 定义SQL字符串                                                                        
    String strSQL = "SELECT * FROM t_college WHERE id = ?";                            
    try {                                                                              
        // 创建预备语句对象                                                                    
        PreparedStatement pstmt = conn.prepareStatement(strSQL);                       
        // 设置占位符的值                                                                     
        pstmt.setInt(1, id);                                                           
        // 执行查询操作,返回结果集                                                                
        ResultSet rs = pstmt.executeQuery();                                           
        // 判断结果集是否为空                                                                   
        if (rs.next()) {                                                               
            // 创建学校对象                                                                  
            college = new College();                                                   
            // 利用当前记录字段值设置学校对象属性值                                                      
            college.setId(rs.getInt("id"));                                            
            college.setName(rs.getString("name"));                                     
            college.setPresident(rs.getString("president"));                           
            college.setStartTime(rs.getTimestamp("start_time"));                       
            college.setEmail(rs.getString("email"));                                   
            college.setAddress(rs.getString("address"));                               
            college.setProfile(rs.getString("profile"));                               
        }                                                                              
        // 关闭结果集                                                                       
        rs.close();                                                                    
        // 关闭预备语句对象                                                                    
        pstmt.close();                                                                 
    } catch (SQLException e) {                                                         
        System.err.println(e.getMessage());                                            
    } finally {                                                                        
        ConnectionManager.closeConnection(conn); // 关闭数据库连接                            
    }                                                                                  
                                                                                       
                                                                                       
    // 返回学校对象                                                                          
    return college;                                                                    
}                                                                                      

(2)编写更新学校记录方法
@Override // 更新学校记录                                                                                                                      
public int update(College college) {                                                                                                     
    // 定义更新记录数                                                                                                                           
    int count = 0;                                                                                                                       
                                                                                                                                         
    // 获取数据库连接                                                                                                                           
    Connection conn = ConnectionManager.getConnection();                                                                                 
    // 定义SQL语句对象                                                                                                                         
    String strSQL = "UPDATE t_college SET name = ?, president = ?, start_time = ?, email = ?, address = ?, profile = ? WHERE id = ?";    
    try {                                                                                                                                
        // 创建预备语句对象                                                                                                                      
        PreparedStatement pstmt = conn.prepareStatement(strSQL);                                                                         
        // 设置占位符的值                                                                                                                       
        pstmt.setString(1, college.getName());                                                                                           
        pstmt.setString(2, college.getPresident());                                                                                      
        pstmt.setTimestamp(3, new Timestamp(college.getStartTime().getTime()));                                                          
        pstmt.setString(4, college.getEmail());                                                                                          
        pstmt.setString(5, college.getAddress());                                                                                        
        pstmt.setString(6, college.getProfile());                                                                                        
        pstmt.setInt(7, college.getId());                                                                                                
        // 执行更新操作,返回更新记录数                                                                                                                
        count = pstmt.executeUpdate();                                                                                                   
        // 关闭预备语句对象                                                                                                                      
        pstmt.close();                                                                                                                   
    } catch (SQLException e) {                                                                                                           
        System.err.println(e.getMessage());                                                                                              
    } finally {                                                                                                                          
        ConnectionManager.closeConnection(conn); // 关闭数据库连接                                                                              
    }                                                                                                                                    
                                                                                                                                         
    // 返回更新记录数                                                                                                                           
    return count;                                                                                                                        
}                                                                                                                                        

1_、测试学校数据访问接口实现类

\在test目录里创建net.huawei.student.dao.impl包,在包里创建TestCollegeDaoImpl类
在这里插入图片描述

(1)编写测试按标识符查询学校记录方法

package net.huawei.student.dao.impl;

import net.huawei.student.bean.College;
import net.huawei.student.dao.CollegeDao;
import org.junit.Test;

/**
 * 功能:测试学校数据访问接口实现类
 * 作者:华卫
 * 日期:2023年06月14日
 */
public class TestCollegeDaoImpl {
    @Test // 测试按标识符查询学校记录
    public void testFindById() {
        // 定义标识符变量
        int id = 1;
        // 创建学校数据访问接口对象
        CollegeDao collegeDao = new CollegeDaoImpl();
        // 调用按标识符查询学校记录方法
        College college = collegeDao.findById(id);
        // 判断查询是否成功
        if (college != null) {
            System.out.println("标识符:" + college.getId());
            System.out.println("学校名称:" + college.getName());
            System.out.println("校长:" + college.getPresident());
            System.out.println("建校时间:" + college.getStartTime());
            System.out.println("电子邮箱:" + college.getEmail());
            System.out.println("通信地址:" + college.getAddress());
            System.out.println("学校概况:" + college.getProfile());
        } else {
            System.out.println("标识符为[" + id + "]的学校记录不存在~");
        }
    }
}

运行testFindById()方法,查看结果
在这里插入图片描述
修改标识符变量值,再运行测试方法,查看结果
在这里插入图片描述

(2)编写测试更新学校记录方法

@Test // 测试更新学校记录                                                 
public void testUpdate() {                                        
    // 创建学校数据访问接口对象                                               
    CollegeDao collegeDao = new CollegeDaoImpl();                 
    // 获取标识符为1的学校记录                                               
    College college = collegeDao.findById(1);                     
    // 输出更新前的学校信息                                                 
    System.out.println("更新前:" + college);                         
    // 设置学校对象属性                                                   
    college.setName("泸职院");                                       
    college.setPresident("萌萌哒");                                  
    college.setProfile("泸职院是省双高建设单位……");                          
    // 调用更新学校记录方法                                                 
    int count = collegeDao.update(college);                       
    // 判断更新是否成功                                                   
    if (count > 0) {                                              
        System.out.println("恭喜,学校记录更新成功~");                       
        System.out.println("更新后:" + collegeDao.findById(1));      
    } else {                                                      
        System.out.println("遗憾,学校记录更新失败~");                       
    }                                                             
}                                                                 

运行testUpdate()方法,查看结果
在这里插入图片描述

2、创建状态数据访问接口实现类

在net.huawei.student.dao.impl包里创建StatusDaoImpl类
在这里插入图片描述
实现StatusDao接口,空实现两个抽象方法
在这里插入图片描述

(1)编写按标识符查询状态记录方法

@Override // 按标识符查询状态记录                                                          
public Status findById(int id) {                                                 
    // 定义状态对象                                                                    
    Status status = null;                                                        
                                                                                 
    // 获取数据库连接                                                                   
    Connection conn = ConnectionManager.getConnection();                         
    // 定义SQL字符串                                                                  
    String strSQL = "SELECT * FROM t_status WHERE id = ?";                       
    try {                                                                        
        // 创建预备语句对象                                                              
        PreparedStatement pstmt = conn.prepareStatement(strSQL);                 
        // 设置占位符的值                                                               
        pstmt.setInt(1, id);                                                     
        // 执行查询操作,返回结果集                                                          
        ResultSet rs = pstmt.executeQuery();                                     
        // 判断结果集是否为空                                                             
        if (rs.next()) {                                                         
            // 创建状态对象                                                            
            status = new Status();                                               
            // 利用当前记录字段值设置状态对象属性                                                 
            status.setId(rs.getInt("id"));                                       
            status.setCollege(rs.getString("college"));                          
            status.setVersion(rs.getString("version"));                          
            status.setAuthor(rs.getString("author"));                            
            status.setTelephone(rs.getString("telephone"));                      
            status.setAddress(rs.getString("address"));                          
            status.setEmail(rs.getString("email"));                              
        }                                                                        
        // 关闭结果集                                                                 
        rs.close();                                                              
        // 关闭预备语句对象                                                              
        pstmt.close();                                                           
    } catch (SQLException e) {                                                   
        System.err.println(e.getMessage());                                      
    } finally {                                                                  
        ConnectionManager.closeConnection(conn); // 关闭数据库连接                      
    }                                                                            
                                                                                 
    // 返回状态对象                                                                    
    return status;                                                               
}                                                                                

(2)编写更新状态记录方法

@Override // 更新状态记录                                                                                                                       
public int update(Status status) {                                                                                                        
    // 定义更新记录数                                                                                                                            
    int count = 0;                                                                                                                        
                                                                                                                                          
    // 获取数据库连接                                                                                                                            
    Connection conn = ConnectionManager.getConnection();                                                                                  
    // 定义SQL字符串                                                                                                                           
    String strSQL = "UPDATE t_status SET college = ?, version = ?, author = ?, telephone = ?, address = ?, email = ? WHERE id = ?";       
    try {                                                                                                                                 
        // 创建预备语句对象                                                                                                                       
        PreparedStatement pstmt = conn.prepareStatement(strSQL);                                                                          
        // 设位置占位符的值                                                                                                                       
        pstmt.setString(1, status.getCollege());                                                                                          
        pstmt.setString(2, status.getVersion());                                                                                          
        pstmt.setString(3, status.getAuthor());                                                                                           
        pstmt.setString(4, status.getTelephone());                                                                                        
        pstmt.setString(5, status.getAddress());                                                                                          
        pstmt.setString(6, status.getEmail());                                                                                            
        pstmt.setInt(7, status.getId());                                                                                                  
        // 执行更新操作,返回更新记录数                                                                                                                 
        count = pstmt.executeUpdate();                                                                                                    
        // 关闭预备语句对象                                                                                                                       
        pstmt.close();                                                                                                                    
    } catch (SQLException e) {                                                                                                            
        System.err.println(e.getMessage());                                                                                               
    } finally {                                                                                                                           
        ConnectionManager.closeConnection(conn); // 关闭数据库连接                                                                               
    }                                                                                                                                     
                                                                                                                                          
    // 返回更新记录数                                                                                                                            
    return count;                                                                                                                         
}                                                                                                                                         

2_、测试状态数据访问接口实现类

(1)编写测试按标识符查询状态记录方法

在test目录的net.huawei.student.dao.impl包里创建TestStatusDaoImpl类
在这里插入图片描述

package net.huawei.student.dao.impl;

import net.huawei.student.bean.Status;
import net.huawei.student.dao.StatusDao;
import org.junit.Test;

/**
 * 功能:测试状态数据访问接口实现类
 * 作者:华卫
 * 日期:2023年06月14日
 */
public class TestStatusDaoImpl {
    @Test // 测试按标识符查询状态记录
    public void testFindById() {
        // 定义标识符变量
        int id = 1;
        // 创建状态数据访问接口对象
        StatusDao statusDao = new StatusDaoImpl();
        // 调用按标识符查询状态记录方法
        Status status = statusDao.findById(id);
        // 判断查询是否成功
        if (status != null) {
            System.out.println(status);
        } else {
            System.out.println("标识符为[" + id + "]的状态记录不存在~");
        }
    }
}

运行testFindById()方法,查看结果
在这里插入图片描述

(2)编写测试更新状态记录方法

@Test // 测试更新状态记录                                                          
public void testUpdate() {                                                 
    // 创建状态数据访问接口对象                                                        
    StatusDao statusDao = new StatusDaoImpl();                             
    // 获取标识符为1的状态记录                                                        
    Status status = statusDao.findById(1);                                 
    // 输出更新的状态记录                                                           
    System.out.println("更新前:" + status);                                   
    // 设置状态对象属性                                                            
    status.setCollege("泸州职业技术学院");                                         
    status.setVersion("2.0");                                              
    status.setVersion("无心剑");                                              
    status.setTelephone("15834345670");                                    
    status.setEmail("375912360@qq.com");                                   
    status.setAddress("泸州江阳区上平远路10号");                                     
    // 调用更新状态记录方法                                                          
    int count = statusDao.update(status);                                  
    // 判断更新是否成功                                                            
    if (count > 0) {                                                       
        System.out.println("恭喜,状态记录更新成功~");                                
        System.out.println("更新后:" + statusDao.findById(1));                
    } else {                                                               
        System.out.println("遗憾,状态记录更新失败~");                                
    }                                                                      
}                                                                          

运行testUpdate()方法,查看结果
在这里插入图片描述

3、创建学生数据访问接口实现类

在net.huawei.student.dao.impl包里创建StudentDaoImpl类
在这里插入图片描述
实现StudentDao接口,空实现所有抽象方法
在这里插入图片描述

(1)编写插入学生记录方法

@Override // 插入学生记录                                                                                                         
public int insert(Student student) {                                                                                        
    // 定义插入记录数                                                                                                              
    int count = 0;                                                                                                          
                                                                                                                            
    // 获得数据库连接                                                                                                              
    Connection conn = ConnectionManager.getConnection();                                                                    
    // 定义SQL字符串                                                                                                             
    String strSQL = "insert into t_student (id, name, sex, age, department, class, telephone) values (?, ?, ?, ?, ?, ?, ?)";
    try {                                                                                                                   
        // 创建预备语句对象                                                                                                         
        PreparedStatement pstmt = conn.prepareStatement(strSQL);                                                            
        // 设置占位符的值                                                                                                          
        pstmt.setString(1, student.getId());                                                                                
        pstmt.setString(2, student.getName());                                                                              
        pstmt.setString(3, student.getSex());                                                                               
        pstmt.setInt(4, student.getAge());                                                                                  
        pstmt.setString(5, student.getDepartment());                                                                        
        pstmt.setString(6, student.getClazz());                                                                             
        pstmt.setString(7, student.getTelephone());                                                                         
        // 执行更新操作,返回插入记录数                                                                                                   
        count = pstmt.executeUpdate();                                                                                      
        // 关闭预备语句对象                                                                                                         
        pstmt.close();                                                                                                      
    } catch (SQLException e) {                                                                                              
        System.err.println(e.getMessage());                                                                                 
    } finally {                                                                                                             
        ConnectionManager.closeConnection(conn); // 关闭数据库连接                                                                 
    }                                                                                                                       
                                                                                                                            
    // 返回插入记录数                                                                                                              
    return count;                                                                                                           
}                                                                                                                           

(2)编写按标识符删除学生记录方法

@Override // 按标识符删除学生记录                                                             
public int deleteById(String id) {                                                  
    // 定义删除记录数                                                                      
    int count = 0;                                                                  
                                                                                    
    // 获取数据库连接                                                                      
    Connection conn = ConnectionManager.getConnection();                            
    // 定义SQL字符串                                                                     
    String strSQL = "delete from t_student where id = ?";                           
    try {                                                                           
        // 创建预备语句对象                                                                 
        PreparedStatement pstmt = conn.prepareStatement(strSQL);                    
        // 设置占位符的值                                                                  
        pstmt.setString(1, id);                                                     
        // 执行更新操作,返回删除记录数                                                           
        count = pstmt.executeUpdate();                                              
        // 关闭预备语句对象                                                                 
        pstmt.close();                                                              
    } catch (SQLException e) {                                                      
        System.err.println(e.getMessage());                                         
    } finally {                                                                     
        ConnectionManager.closeConnection(conn); // 关闭数据库连接                         
    }                                                                               
                                                                                    
    // 返回删除记录数                                                                      
    return count;                                                                   
}                                                                                   

(3)编写按班级删除学生记录方法

@Override // 按班级删除学生记录                                                         
public int deleteByClass(String clazz) {                                       
    // 定义删除记录数                                                                 
    int count = 0;                                                             
                                                                               
    // 获取数据库连接                                                                 
    Connection conn = ConnectionManager.getConnection();                       
    // 定义SQL字符串                                                                
    String strSQL = "delete from t_student where class = ?";                   
    try {                                                                      
        // 创建预备语句对象                                                            
        PreparedStatement pstmt = conn.prepareStatement(strSQL);               
        // 设置占位符的值                                                             
        pstmt.setString(1, clazz);                                             
        // 执行更新操作,返回删除记录数                                                      
        count = pstmt.executeUpdate();                                         
        // 关闭预备语句对象                                                            
        pstmt.close();                                                         
    } catch (SQLException e) {                                                 
        System.err.println(e.getMessage());                                    
    } finally {                                                                
        ConnectionManager.closeConnection(conn); // 关闭数据库连接                    
    }                                                                          
                                                                               
    // 返回删除记录数                                                                 
    return count;                                                              
}                                                                              

(4)编写按系部删除学生记录方法

@Override // 按系部删除学生记录                                                                  
public int deleteByDepartment(String department) {                                      
    // 定义删除记录数                                                                          
    int count = 0;                                                                      
                                                                                        
    // 获得数据库连接                                                                          
    Connection conn = ConnectionManager.getConnection();                                
    // 定义SQL字符串                                                                         
    String strSQL = "delete from t_student where department = ?";                       
    try {                                                                               
        // 创建预备语句对象                                                                     
        PreparedStatement pstmt = conn.prepareStatement(strSQL);                        
        // 设置占位符的值                                                                      
        pstmt.setString(1, department);                                                 
        // 执行更新操作,返回删除记录数                                                               
        count = pstmt.executeUpdate();                                                  
        // 关闭预备语句对象                                                                     
        pstmt.close();                                                                  
    } catch (SQLException e) {                                                          
        System.err.println(e.getMessage());                                             
    } finally {                                                                         
        ConnectionManager.closeConnection(conn); // 关闭数据库连接                             
    }                                                                                   
                                                                                        
    // 返回删除记录数                                                                          
    return count;                                                                       
}                                                                                       

(5)编写更新学生记录方法

@Override // 更新学生记录                                                                                    
public int update(Student student) {                                                                   
    // 定义更新记录数                                                                                         
    int count = 0;                                                                                     
                                                                                                       
    // 获得数据库连接                                                                                         
    Connection conn = ConnectionManager.getConnection();                                               
    // 定义SQL字符串                                                                                        
    String strSQL = "update t_student set name = ?, sex = ?, age = ?,"                                 
            + " department = ?, class = ?, telephone = ? where id = ?";                                
    try {                                                                                              
        // 创建预备语句对象                                                                                    
        PreparedStatement pstmt = conn.prepareStatement(strSQL);                                       
        // 设置占位符的值                                                                                     
        pstmt.setString(1, student.getName());                                                         
        pstmt.setString(2, student.getSex());                                                          
        pstmt.setInt(3, student.getAge());                                                             
        pstmt.setString(4, student.getDepartment());                                                   
        pstmt.setString(5, student.getClazz());                                                        
        pstmt.setString(6, student.getTelephone());                                                    
        pstmt.setString(7, student.getId());                                                           
        // 执行更新操作,返回更新记录数                                                                              
        count = pstmt.executeUpdate();                                                                 
        // 关闭预备语句对象                                                                                    
        pstmt.close();                                                                                 
    } catch (SQLException e) {                                                                         
        System.err.println(e.getMessage());                                                            
    } finally {                                                                                        
        ConnectionManager.closeConnection(conn); // 关闭数据库连接                                            
    }                                                                                                  
                                                                                                       
    // 返回更新记录数                                                                                         
    return count;                                                                                      
}                                                                                                      

(6)编写按标识符查询学生记录方法

@Override // 按学号查询学生记录                                                                  
public Student findById(String id) {                                                    
    // 声明学生对象                                                                           
    Student student = null;                                                             
                                                                                        
    // 获取数据库连接对象                                                                        
    Connection conn = ConnectionManager.getConnection();                                
    // 定义SQL字符串                                                                         
    String strSQL = "select * from t_student where id = ?";                             
    try {                                                                               
        // 创建预备语句对象                                                                     
        PreparedStatement pstmt = conn.prepareStatement(strSQL);                        
        // 设置占位符的值                                                                      
        pstmt.setString(1, id);                                                         
        // 执行查询操作,返回结果集                                                                 
        ResultSet rs = pstmt.executeQuery();                                            
        // 判断结果集是否为空                                                                    
        if (rs.next()) {                                                                
            // 创建学生实体                                                                   
            student = new Student();                                                    
            // 利用当前记录各字段值设置学生实体属性                                                       
            student.setId(rs.getString("id"));                                          
            student.setName(rs.getString("name"));                                      
            student.setSex(rs.getString("sex"));                                        
            student.setAge(rs.getInt("age"));                                           
            student.setDepartment(rs.getString("department"));                          
            student.setClazz(rs.getString("class"));                                    
            student.setTelephone(rs.getString("telephone"));                            
        }                                                                               
    } catch (SQLException e) {                                                          
        System.err.println(e.getMessage());                                             
    } finally {                                                                         
        ConnectionManager.closeConnection(conn); // 关闭数据库连接                             
    }                                                                                   
                                                                                        
    // 返回学生对象                                                                           
    return student;                                                                     
}                                                                                       

(7)编写按姓名查询学生记录方法

@Override // 按姓名查询学生记录                                                                       
public List<Student> findByName(String name) {                                               
    // 声明学生列表                                                                                
    List<Student> students = new ArrayList<>();                                              
                                                                                             
    // 获取数据库连接对象                                                                             
    Connection conn = ConnectionManager.getConnection();                                     
    // 定义SQL字符串                                                                              
    String strSQL = "select * from t_student where name like ?";                             
    try {                                                                                    
        // 创建预备语句对象                                                                          
        PreparedStatement pstmt = conn.prepareStatement(strSQL);                             
        // 设置占位符的值                                                                           
        pstmt.setString(1, name + "%");                                                      
        // 执行查询操作,返回结果集                                                                      
        ResultSet rs = pstmt.executeQuery();                                                 
        // 遍历结果集                                                                             
        while (rs.next()) {                                                                  
            // 创建学生实体                                                                        
            Student student = new Student();                                                 
            // 利用当前记录各字段值设置学生实体属性                                                            
            student.setId(rs.getString("id"));                                               
            student.setName(rs.getString("name"));                                           
            student.setSex(rs.getString("sex"));                                             
            student.setAge(rs.getInt("age"));                                                
            student.setDepartment(rs.getString("department"));                               
            student.setClazz(rs.getString("class"));                                         
            student.setTelephone(rs.getString("telephone"));                                 
            // 将实体添加到学生列表                                                                    
            students.add(student);                                                           
        }                                                                                    
        // 关闭结果集                                                                             
        rs.close();                                                                          
        // 关闭预备语句对象                                                                          
        pstmt.close();                                                                       
    } catch (SQLException e) {                                                               
        System.err.println(e.getMessage());                                                  
    } finally {                                                                              
        ConnectionManager.closeConnection(conn); // 关闭数据库连接                                  
    }                                                                                        
                                                                                             
    // 返回学生列表                                                                                
    return students;                                                                         

(8)编写按班级查询学生记录方法

@Override // 按班级查询学生记录                                                               
public List<Student> findByClass(String clazz) {                                     
    // 声明学生列表                                                                        
    List<Student> students = new ArrayList<>();                                      
                                                                                     
    // 获取数据库连接对象                                                                     
    Connection conn = ConnectionManager.getConnection();                             
    // 定义SQL字符串                                                                      
    String strSQL = "select * from t_student where class like ?";                    
    try {                                                                            
        // 创建预备语句对象                                                                  
        PreparedStatement pstmt = conn.prepareStatement(strSQL);                     
        // 设置占位符的值                                                                   
        pstmt.setString(1, clazz + "%");                                             
        // 执行查询操作,返回结果集                                                              
        ResultSet rs = pstmt.executeQuery();                                         
        // 遍历结果集                                                                     
        while (rs.next()) {                                                          
            // 创建学生实体                                                                
            Student student = new Student();                                         
            // 利用当前记录各字段值设置学生实体属性                                                    
            student.setId(rs.getString("id"));                                       
            student.setName(rs.getString("name"));                                   
            student.setSex(rs.getString("sex"));                                     
            student.setAge(rs.getInt("age"));                                        
            student.setDepartment(rs.getString("department"));                       
            student.setClazz(rs.getString("class"));                                 
            student.setTelephone(rs.getString("telephone"));                         
            // 将实体添加到学生列表                                                            
            students.add(student);                                                   
        }                                                                            
        // 关闭结果集                                                                     
        rs.close();                                                                  
        // 关闭预备语句对象                                                                  
        pstmt.close();                                                               
    } catch (SQLException e) {                                                       
        System.err.println(e.getMessage());                                          
    } finally {                                                                      
        ConnectionManager.closeConnection(conn); // 关闭数据库连接                          
    }                                                                                
                                                                                     
    // 返回学生列表                                                                        
    return students;                                                                 
}                                                                                    

(9)编写按系部查询学生记录方法

 @Override // 按系部查询学生记录                                                                                 
 public List<Student> findByDepartment(String department) {                                             
     // 声明学生列表                                                                                          
     List<Student> students = new ArrayList<>();                                                        
                                                                                                        
     // 获取数据库连接对象                                                                                       
     Connection conn = ConnectionManager.getConnection();                                               
     // 定义SQL字符串                                                                                        
     String strSQL = "select * from t_student where department like ?";                                 
     try {                                                                                              
         // 创建预备语句对象                                                                                    
         PreparedStatement pstmt = conn.prepareStatement(strSQL);                                       
         // 设置占位符的值                                                                                     
         pstmt.setString(1, department + "%");                                                          
         // 执行查询操作,返回结果集                                                                                
         ResultSet rs = pstmt.executeQuery();                                                           
         // 遍历结果集                                                                                       
         while (rs.next()) {                                                                            
             // 创建学生实体                                                                                  
             Student student = new Student();                                                           
             // 利用当前记录各字段值设置学生实体属性                                                                      
             student.setId(rs.getString("id"));                                                         
             student.setName(rs.getString("name"));                                                     
             student.setSex(rs.getString("sex"));                                                       
             student.setAge(rs.getInt("age"));                                                          
             student.setDepartment(rs.getString("department"));                                         
             student.setClazz(rs.getString("class"));                                                   
             student.setTelephone(rs.getString("telephone"));                                           
             // 将实体添加到学生列表                                                                              
             students.add(student);                                                                     
         }                                                                                              
         // 关闭结果集                                                                                       
         rs.close();                                                                                    
         // 关闭预备语句对象                                                                                    
         pstmt.close();                                                                                 
     } catch (SQLException e) {                                                                         
         System.err.println(e.getMessage());                                                            
     } finally {                                                                                        
         ConnectionManager.closeConnection(conn); // 关闭数据库连接                                            
     }                                                                                                  
                                                                                                        
     // 返回学生列表                                                                                          
     return students;                                                                                   
 }                                                                                                      

(10)编写查询全部学生记录方法

@Override // 查询全部学生记录                                                               
public List<Student> findAll() {                                                    
    // 声明学生列表                                                                       
    List<Student> students = new ArrayList<Student>();                              
                                                                                    
    // 获取数据库连接对象                                                                    
    Connection conn = ConnectionManager.getConnection();                            
    // 定义SQL字符串                                                                     
    String strSQL = "select * from t_student";                                      
    try {                                                                           
        // 创建语句对象                                                                   
        Statement stmt = conn.createStatement();                                    
        // 执行查询操作,返回结果集                                                             
        ResultSet rs = stmt.executeQuery(strSQL);                                   
        // 遍历结果集                                                                    
        while (rs.next()) {                                                         
            // 创建学生实体                                                               
            Student student = new Student();                                        
            // 利用当前记录各字段值设置学生实体属性                                                   
            student.setId(rs.getString("id"));                                      
            student.setName(rs.getString("name"));                                  
            student.setSex(rs.getString("sex"));                                    
            student.setAge(rs.getInt("age"));                                       
            student.setDepartment(rs.getString("department"));                      
            student.setClazz(rs.getString("class"));                                
            student.setTelephone(rs.getString("telephone"));                        
            // 将实体添加到学生列表                                                           
            students.add(student);                                                  
        }                                                                           
        // 关闭结果集                                                                    
        rs.close();                                                                 
        // 关闭语句对象                                                                   
        stmt.close();                                                               
    } catch (SQLException e) {                                                      
        System.err.println(e.getMessage());                                         
    } finally {                                                                     
        ConnectionManager.closeConnection(conn); // 关闭数据库连接                         
    }                                                                               
                                                                                    
    // 返回学生列表                                                                       
    return students;                                                                

(11)编写按性别统计人数方法

@Override // 按性别统计人数                                                               
public Vector findRowsBySex() {                                                    
    // 定义行集向量                                                                      
    Vector rows = new Vector();                                                    
                                                                                   
    // 获取数据库连接对象                                                                   
    Connection conn = ConnectionManager.getConnection();                           
    // 定义SQL字符串                                                                    
    String strSQL = "select sex as '性别', count(*) as '人数'"                         
            + " from t_student group by sex order by sex desc";                    
    try {                                                                          
        // 创建语句对象                                                                  
        Statement stmt = conn.createStatement();                                   
        // 执行查询操作,返回结果集                                                            
        ResultSet rs = stmt.executeQuery(strSQL);                                  
        // 遍历结果集                                                                   
        while (rs.next()) {                                                        
            // 定义当前行向量                                                             
            Vector<String> currentRow = new Vector();                              
            // 利用当前记录字段值设置当前行向量的元素值                                                
            currentRow.addElement(rs.getString("性别"));                             
            currentRow.addElement(rs.getInt("人数") + "");                           
            // 将当前行向量添加到行集向量                                                       
            rows.addElement(currentRow);                                           
        }                                                                          
    } catch (SQLException e) {                                                     
        System.err.println(e.getMessage());                                        
    } finally {                                                                    
        ConnectionManager.closeConnection(conn); // 关闭数据库连接                        
    }                                                                              
                                                                                   
    // 返回行集向量                                                                      
    return rows;                                                                   

(12)编写按班级统计人数方法

@Override // 按班级统计人数                                                          
public Vector findRowsByClass() {                                             
    // 定义行集向量                                                                 
    Vector rows = new Vector();                                               
                                                                              
    // 获取数据库连接对象                                                              
    Connection conn = ConnectionManager.getConnection();                      
    // 定义SQL字符串                                                               
    String strSQL = "select sex as '班级', count(*) as '人数'"                    
            + " from t_student group by class order by class desc";           
    try {                                                                     
        // 创建语句对象                                                             
        Statement stmt = conn.createStatement();                              
        // 执行查询操作,返回结果集                                                       
        ResultSet rs = stmt.executeQuery(strSQL);                             
        // 遍历结果集                                                              
        while (rs.next()) {                                                   
            // 定义当前行向量                                                        
            Vector<String> currentRow = new Vector();                         
            // 利用当前记录字段值设置当前行向量的元素值                                           
            currentRow.addElement(rs.getString("班级"));                        
            currentRow.addElement(rs.getInt("人数") + "");                      
            // 将当前行向量添加到行集向量                                                  
            rows.addElement(currentRow);                                      
        }                                                                     
    } catch (SQLException e) {                                                
        System.err.println(e.getMessage());                                   
    } finally {                                                               
        ConnectionManager.closeConnection(conn); // 关闭数据库连接                   
    }                                                                         
                                                                              
    // 返回行集向量                                                                 
    return rows;                                                              
}                                                                             

(13)编写按系部统计人数方法

@Override // 按系部统计人数                                                                     
public Vector findRowsByDepartment() {                                                   
    // 定义行集向量                                                                            
    Vector rows = new Vector();                                                          
                                                                                         
    // 获取数据库连接对象                                                                         
    Connection conn = ConnectionManager.getConnection();                                 
    // 定义SQL字符串                                                                          
    String strSQL = "select sex as '系部', count(*) as '人数'"                               
            + " from t_student group by department order by department desc";            
    try {                                                                                
        // 创建语句对象                                                                        
        Statement stmt = conn.createStatement();                                         
        // 执行查询操作,返回结果集                                                                  
        ResultSet rs = stmt.executeQuery(strSQL);                                        
        // 遍历结果集                                                                         
        while (rs.next()) {                                                              
            // 定义当前行向量                                                                   
            Vector<String> currentRow = new Vector();                                    
            // 利用当前记录字段值设置当前行向量的元素值                                                      
            currentRow.addElement(rs.getString("系部"));                                   
            currentRow.addElement(rs.getInt("人数") + "");                                 
            // 将当前行向量添加到行集向量                                                             
            rows.addElement(currentRow);                                                 
        }                                                                                
    } catch (SQLException e) {                                                           
        System.err.println(e.getMessage());                                              
    } finally {                                                                          
        ConnectionManager.closeConnection(conn); // 关闭数据库连接                              
    }                                                                                    
                                                                                         
    // 返回行集向量                                                                            
    return rows;                                                                         
}                                                                                        

3_、测试学生数据访问接口实现类

在test目录的net.huawei.student.dao.impl包里创建TestStudentDaoImpl类
在这里插入图片描述

(1)编写测试按标识符查询学生记录方法

package net.huawei.student.dao.impl;

import net.huawei.student.bean.Student;
import net.huawei.student.dao.StudentDao;
import org.junit.Test;

/**
 * 功能:测试学生数据访问接口实现类
 * 作者:华卫
 * 日期:2023年06月16日
 */
public class TestStudentDaoImpl {
    
    private StudentDao dao = new StudentDaoImpl();
    
    @Test // 测试按学号查询学生记录
    public void testFindById() {
        // 定义学号变量
        String id = "20222005";
        // 调用按学号查询学生记录的方法
        Student student = dao.findById(id);
        // 判断查询是否成功
        if (student != null) {
            System.out.println(student);
        } else {
            System.out.println("学号为[" + id + "]的学生未找到~");
        }
    }
}

运行testFindById()方法,查看结果
在这里插入图片描述

4、创建用户数据访问接口实现类

在net.huawei.student.dao.impl包里创建UserDaoImpl类
在这里插入图片描述
实现UserDao接口,空实现所有抽象方法
在这里插入图片描述

(1)编写插入用户记录方法

@Override // 插入用户记录                                                                                                            
public int insert(User user) {                                                                                                 
    // 定义插入记录数                                                                                                                 
    int count = 0;                                                                                                             
                                                                                                                               
    // 获得数据库连接                                                                                                                 
    Connection conn = ConnectionManager.getConnection();                                                                       
    // 定义SQL字符串                                                                                                                
    String strSQL = "insert into t_user (username, password, telephone, register_time) values (?, ?, ?, ?)";                   
                                                                                                                               
    // 不允许用户表里插入两条用户名相同的记录                                                                                                     
    if (!isUsernameExisted(user.getUsername())) {                                                                              
        try {                                                                                                                  
            // 创建预备语句对象                                                                                                        
            PreparedStatement pstmt = conn.prepareStatement(strSQL);                                                           
            // 设置占位符的值                                                                                                         
            pstmt.setString(1, user.getUsername());                                                                            
            pstmt.setString(2, user.getPassword());                                                                            
            pstmt.setString(3, user.getTelephone());                                                                           
            pstmt.setTimestamp(4, new Timestamp(user.getRegisterTime().getTime()));                                            
            // 执行更新操作,返回插入记录数                                                                                                  
            count = pstmt.executeUpdate();                                                                                     
            // 关闭预备语句对象                                                                                                        
            pstmt.close();                                                                                                     
        } catch (SQLException e) {                                                                                             
            System.err.println(e.getMessage());                                                                                
        } finally {                                                                                                            
            ConnectionManager.closeConnection(conn); // 关闭数据库连接                                                                
        }                                                                                                                      
    }                                                                                                                          
                                                                                                                               
    // 返回插入记录数                                                                                                                 
    return count;                                                                                                              
}                                                                                                                              

(2)编写按标识符删除用户记录方法

@Override // 按标识符删除用户记录                                                   
public int deleteById(int id) {                                           
    // 定义删除记录数                                                            
    int count = 0;                                                        
                                                                          
    // 获取数据库连接                                                            
    Connection conn = ConnectionManager.getConnection();                  
    // 定义SQL字符串                                                           
    String strSQL = "delete from t_user where id = ?";                    
    try {                                                                 
        // 创建预备语句对象                                                       
        PreparedStatement pstmt = conn.prepareStatement(strSQL);          
        // 设置占位符的值                                                        
        pstmt.setInt(1, id);                                              
        // 执行更新操作,返回删除记录数                                                 
        count = pstmt.executeUpdate();                                    
        // 关闭预备语句对象                                                       
        pstmt.close();                                                    
    } catch (SQLException e) {                                            
        System.err.println(e.getMessage());                               
    } finally {                                                           
        ConnectionManager.closeConnection(conn); // 关闭数据库连接               
    }                                                                     
                                                                          
    // 返回删除记录数                                                            
    return count;                                                         
}                                                                         

(3)编写更新用户记录方法

@Override // 更新用户记录                                                                                                                                                      
public int update(User user) {                                                                                                                                           
    // 定义更新记录数                                                                                                                                                           
    int count = 0;                                                                                                                                                       
                                                                                                                                                                         
    // 获得数据库连接                                                                                                                                                           
    Connection conn = ConnectionManager.getConnection();                                                                                                                 
    // 定义SQL字符串                                                                                                                                                          
    String strSQL = "update t_user set username = ?, password = ?, telephone = ?, register_time = ? where id = ?";                                                       
    try {                                                                                                                                                                
        // 创建预备语句对象                                                                                                                                                      
        PreparedStatement pstmt = conn.prepareStatement(strSQL);                                                                                                         
        // 设置占位符的值                                                                                                                                                       
        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());                                                                                                                                   
        // 执行更新操作,返回更新记录数                                                                                                                                                
        count = pstmt.executeUpdate();                                                                                                                                   
        // 关闭预备语句对象                                                                                                                                                      
        pstmt.close();                                                                                                                                                   
    } catch (SQLException e) {                                                                                                                                           
        System.err.println(e.getMessage());                                                                                                                              
    } finally {                                                                                                                                                          
        ConnectionManager.closeConnection(conn); // 关闭数据库连接                                                                                                              
    }                                                                                                                                                                    
}                                                                                                                                                                   

(4)编写按标识符查询用户记录方法

@Override // 按标识符查询用户                                                                              
public User findById(int id) {                                                                     
    // 声明用户对象                                                                                      
    User user = null;                                                                              
                                                                                                   
    // 获取数据库连接对象                                                                                   
    Connection conn = ConnectionManager.getConnection();                                           
    // 定义SQL字符串                                                                                    
    String strSQL = "select * from t_user where id = ?";                                           
    try {                                                                                          
        // 创建预备语句对象                                                                                
        PreparedStatement pstmt = conn.prepareStatement(strSQL);                                   
        // 设置占位符的值                                                                                 
        pstmt.setInt(1, id);                                                                       
        // 执行SQL,返回结果集                                                                             
        ResultSet rs = pstmt.executeQuery();                                                       
        // 判断结果集是否有记录                                                                              
        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) {                                                                     
        System.err.println(e.getMessage());                                                        
    } finally {                                                                                    
        ConnectionManager.closeConnection(conn); // 关闭数据库连接                                        
    }                                                                                              
                                                                                                   
    // 返回用户对象                                                                                      
    return user;                                                                                   
}                                                                                                  

(5)编写查询全部用户记录方法

@Override // 查询全部用户记录                                                           
public List<User> findAll() {                                                   
    // 声明用户列表                                                                   
    List<User> users = new ArrayList<>();                                       
                                                                                
    // 获取数据库连接对象                                                                
    Connection conn = ConnectionManager.getConnection();                        
    // 定义SQL字符串                                                                 
    String strSQL = "select * from t_user";                                     
    try {                                                                       
        // 创建语句对象                                                               
        Statement stmt = conn.createStatement();                                
        // 执行查询操作,返回结果集                                                         
        ResultSet rs = stmt.executeQuery(strSQL);                               
        // 遍历结果集                                                                
        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);                                                    
        }                                                                       
        // 关闭结果集                                                                
        rs.close();                                                             
        // 关闭语句对象                                                               
        stmt.close();                                                           
    } catch (SQLException e) {                                                  
        System.err.println(e.getMessage());                                     
    } finally {                                                                 
        ConnectionManager.closeConnection(conn); // 关闭数据库连接                     
    }                                                                           
                                                                                
    // 返回用户列表                                                                   
    return users;                                                               
}                                                                               

(6)编写用户登录方法``

@Override // 用户登录                                                               
public User login(String username, String password) {                           
    // 声明用户对象                                                                   
    User user = null;                                                           
                                                                                
    // 获取数据库连接                                                                  
    Connection conn = ConnectionManager.getConnection();                        
    // 定义SQL字符串                                                                 
    String strSQL = "select * from t_user where username = ? and password = ?"; 
    try {                                                                       
        // 创建预备语句对象                                                             
        PreparedStatement pstmt = conn.prepareStatement(strSQL);                
        // 设置占位符的值                                                              
        pstmt.setString(1, username);                                           
        pstmt.setString(2, password);                                           
        // 执行查询操作,返回结果集                                                         
        ResultSet rs = pstmt.executeQuery();                                    
        // 判断结果集是否有记录                                                           
        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) {                                                  
        System.err.println(e.getMessage());                                     
    } finally {                                                                 
        ConnectionManager.closeConnection(conn); // 关闭数据库连接                     
    }                                                                           
                                                                                
    // 返回用户对象                                                                   
    return user;                                                                
}                                                                               

(7)编写用户名是否存在方法

@Override // 判断用户名是否存在                                                        
public boolean isUsernameExisted(String username) {                           
    // 定义存在与否变量                                                               
    boolean existed = false;                                                  
                                                                              
    // 获取数据库连接                                                                
    Connection conn = ConnectionManager.getConnection();                      
    // 定义SQL字符串                                                               
    String strSQL = "select * from t_user where username = ?";                
    try {                                                                     
        // 创建预备语句对象                                                           
        PreparedStatement pstmt = conn.prepareStatement(strSQL);              
        // 设置占位符的值                                                            
        pstmt.setString(1, username);                                         
        // 执行查询操作,返回结果集                                                       
        ResultSet rs = pstmt.executeQuery();                                  
        // 判断结果集是否为空                                                          
        if (rs.next()) {                                                      
            existed = true; // 存在此用户名                                         
        }                                                                     
        // 关闭结果集对象                                                            
        rs.close();                                                           
        // 关闭预备语句对象                                                           
        pstmt.close();                                                        
    } catch (SQLException e) {                                                
        System.err.println(e.getMessage());                                   
    } finally {                                                               
        ConnectionManager.closeConnection(conn); // 关闭数据库连接                   
    }                                                                         
                                                                              
    // 返回存在与否变量                                                               
    return existed;                                                           
}                                                                             

4_、测试用户数据访问接口实现类

在test目录的net.huawei.student.dao.impl包里创建TestUserDaoImpl类
在这里插入图片描述

(7)编写测试用户登录方法

package net.huawei.student.dao.impl;

import net.huawei.student.bean.User;
import net.huawei.student.dao.UserDao;
import org.junit.Test;

/**
 * 功能:测试用户数据访问接口实现类
 * 作者:晨无心
 * 日期:2023年06月16日
 */
public class TestUserDaoImpl {

    private UserDao dao = new UserDaoImpl();

    @Test // 测试用户登录方法
    public void testLogin() {
        // 定义用户名与密码
        String username = "王霞";
        String password = "111111";
        // 调用用户登录方法
        User user = dao.login(username, password);
        // 判断登录是否成功
        if (user != null) {
            System.out.println("恭喜,[" + username + "]登录成功~");
        } else {
            System.out.println("遗憾,[" + username + "]登录失败~");
        }
    }
}
运行testLogin()方法,查看结果
```![在这里插入图片描述](https://img-blog.csdnimg.cn/9b14aa23428644ac99ff2ffe0961ff15.png)

改错用户名或密码,再运行testLogin()方法,查看结果
![在这里插入图片描述](https://img-blog.csdnimg.cn/e4b7b22607c14fe89c18ff7c33a7701c.png)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值