实训日志2

本文详细介绍了使用Java进行数据库操作的方法,包括数据访问接口实现类的创建、SQL语句的执行、结果集的处理以及异常处理等关键步骤。通过具体示例,展示了如何实现学校和状态信息的数据访问和更新。

今天完成了一小部分的测试,但还是不仔细,一会又报错,一会又异常,大部分原因是符号或字母的问题:
数据访问接口实现类CollegeDaoImpl:

package net.hw.student.bean.dao.impl;

import net.hw.student.bean.College;
import net.hw.student.bean.dao.CollegeDao;
import net.hw.student.bean.dbutil.ConnectionManager;

import java.sql.*;

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

创建test包,在里面创建测试类TestCollegeDaoImpl:

package net.hw.student.bean.test;

import net.hw.student.bean.College;
import net.hw.student.bean.dao.CollegeDao;
import net.hw.student.bean.dao.impl.CollegeDaoImpl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * 功能:测试学校数据访问接口实现类
 * 日期:2019年6月17日
 */
public class TestCollegeDaoImpl {
    @Before
    public void beforeTest(){
        System.out.println("单元测试开始了·");
    }
    @Test
    public void testFindByid(){
        CollegeDao dao=new CollegeDaoImpl();
        College college=dao.findById(1);
        System.out.println(college);
    }
    @Test
    public void testUpdate(){
        CollegeDao dao=new CollegeDaoImpl();
        College college=dao.findById(1);
        college.setPresident("王洪礼");
        dao.update(college);
        college=dao.findById(1);
        System.out.println(college);
    }
    @After
    public void afterTest(){
        System.out.println("单元测试结束了·");
    }
}

运行结果:
在这里插入图片描述
状态数据访问接口实现类StatusDaoImpl:

package net.hw.student.bean.dao.impl;

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

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

/**
 * 功能:状态数据访问接口实现类
 * 日期:2019年6月17日
 */
public class StatusDaoImpl implements StatusDao {
    @Override
    public Status findById(int id) {
        //声明状态对象
        Status status = null;
        //1.获取数据库连接对象
        Connection conn = ConnectionManager.getConnection();
        //2.定义SQL字符串
        String strSQL = "select * from t_status where id=?";
        try {
            //3.创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            //4.设置占位符的值
            pstmt.setInt(1, id);
            //5.执行SQL查询,返回结果集
            ResultSet rs = pstmt.executeQuery();
            //6.判断结果集是否有记录
            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"));
            }
            //7.关闭预备语句对象
            pstmt.close();
            //8.关闭结果集对象
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }
        //返回状态对象
        return status;
    }
    @Override
    public int update(Status status){
        //定义更新记录数
        int count=0;

        //1.获取数据库连接
        Connection conn=ConnectionManager.getConnection();
        //2.定义SQL字符串
        String strSQL="update t_status set college=?,version =?,author=?,"
                +"telephone=?,address=?,email=? where id=?";
        try {
            //3.创建预备语句对象
            PreparedStatement pstmt=conn.prepareStatement(strSQL);
            //4.设置占位符的值
            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());
            //5.执行更新操作,更新记录
            count=pstmt.executeUpdate();
            //6.关闭预备语句对象
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }
        //返回更新记录
        return count;
    }
}

创建测试类TestStatusDaoImpl:

package net.hw.student.bean.test;

import net.hw.student.bean.Status;
import net.hw.student.bean.dao.StatusDao;
import net.hw.student.bean.dao.impl.StatusDaoImpl;
import org.junit.Test;

/**
 * 功能:测试状态数据访问接口实现类
 * 日期:2019年6月17日
 */
public class TestStatusDaoImpl {
    @Test
    public void testFindById(){
        StatusDao dao=new StatusDaoImpl();
        Status status=dao.findById(1);
        System.out.println(status);
    }@Test
    public void testUpdate(){
        StatusDao dao=new StatusDaoImpl();
        Status status=dao.findById(1);
        status.setAuthor("耿耿余淮");
        status.setTelephone("12345678901");
        status.setEmail("genggeng@163.com");
        dao.update(status);
        status=dao.findById(1);
        System.out.println(status);
    }

}

运行结果:

学生数据访问接口实现类StudentDaoImpl:

在这里插入代码片
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值