Java实训日志05

八、项目开发实现步骤

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

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

1.创建学校数据

  • net.zhanghao.student.dao.impl包里创建CollegeDaoImpl
    在这里插入图片描述
  • 实现CollegeDao接口
  • 按+组合键

在这里插入图片描述

  • 选择实现的抽象方法
    在这里插入图片描述

  • 单击【ok】按钮

在这里插入图片描述

(1)编写按标识符查询学校记录方法
@Override // 按标识符查询学校记录
    public College findById(int id) {
        // 定义学校对象
        College college = null;

        // 获取数据库的连接
        Connection conn = ConnectionManger.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 {
            ConnectionManger.closeConnection(conn);// 关闭数据库连接
        }

        // 返回学校对象
        return college;
    }
(2)编写更新学校记录方法

在这里插入图片描述

@Override // 编写更新学校记录
    public int update(College college) {
        // 定义更新记录
        int count = 0;

        // 获取数据库的连接
        Connection conn = ConnectionManger.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.getName());
            pstmt.setTimestamp(3, new Timestamp(college.getStartTime().getTime()));
            pstmt.setString(4, college.getName());
            pstmt.setString(5, college.getName());
            pstmt.setString(6, college.getName());
            pstmt.setString(7, college.getName());
            count = pstmt.executeUpdate();
            // 关闭预备语句对象
            pstmt.close();
        } catch (SQLException e) {
            System.err.println(e.getMessage());
        } finally {
            ConnectionManger.closeConnection(conn);
        }
        // 返回更新记录数
        return count;
    }
}

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

  • test目录里创建net.zhanghao.student.dao.impl包,在包里创建TestCollegeDaoImpl
    在这里插入图片描述
(1)编写测试按标识符查询学校记录方法
package net.zhanghao.student.dao.impl;

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

/**
 * 功能:测试学校数据访问接口实现类
 * 作者:张豪
 * 日期:2023年06月14日
 */
public class TestCollegeDaoImpl {
    @Test // 测试标识符查询学校记录
    public void testFindById(){
        // 定义标识符变量
        int id = 10;
        // 创建学校数据访问接口对象
        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 +"]的学校记录不存在~");
        }
    }
}

  • 运行代码,查看结果
    在这里插入图片描述
  • 修改标识符变量值,查看运行结果
    在这里插入图片描述
(2)编写更新学校记录方法
  • 运行代码,查看结果
package net.zhanghao.student.dao.impl;

import net.zhanghao.student.bean.College;
import net.zhanghao.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 +"]的学校记录不存在~");
        }
    }
    @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)编写按标识符查询状态记录方法
package net.zhanghao.student.dao.impl;

import net.zhanghao.student.bean.Status;
import net.zhanghao.student.dao.StatusDao;
import net.zhanghao.student.dbutil.ConnectionManager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 功能:状态数据访问接口实现类
 * 作者:华卫
 * 日期:2023年06月14日
 */
public class StatusDaoImpl implements StatusDao {
    @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;
    }

    @Override
    public int update(Status status) {
        return 0;
    }
}

(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.zhanghao.student.dao.impl包里创建TestStatusDaoImpl
    在这里插入图片描述
package net.zhanghao.student.dao.impl;

import net.zhanghao.student.bean.Status;
import net.zhanghao.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()方法,查看结果
    在这里插入图片描述
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值