java实训03数据访问接口

1.建项目dao
DAO: Data Acess Object
在这里插入图片描述
系统有四张表:College、Status、Student与User表,对这四张表的操作就在相应的数据访问接口里进行规定,有四个数据访问接口:CollegeDao、StatusDao、StudentDao与UserDao。注意,这些接口要放到dao子包里。在这里插入图片描述
(1)学校数据访问接口CollegeDao
在这里插入图片描述
(2)状态数据访问接口StatusDao
在这里插入图片描述
(3)学生数据访问接口StudentDao
在这里插入图片描述
(4)用户数据访问接口UserDao
在这里插入图片描述
2.数据访问接口实现类
在dao子包里创建impl子包,然后在里面创建四个数据访问接口的实现类
在这里插入图片描述
(1)学校数据访问接口实现类CollegeDaoImpl
在这里插入图片描述
package net.lyy.student.dao.impl;

/**

  • 功能:学校数据访问接口实现类
  • 作者:李远燕
  • 日期:2019年6月27日
    */

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

import java.sql.*;

public class CollageDaoImpl implements CollegeDao {
/**
* 按id查找学校
*
* @param id
* @return 学习对象
*/
@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;
}

/**
 * 更新学校信息
 *
 * @param college
 * @return
 */

@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 = ?, address = ?,profile = ? where id = ? ";
    try {
        //3.创建预备语句对象
        PreparedStatement pstmt = conn.prepareStatement(strSQL);
        //4.设置占位符的值
        pstmt.setString(1,college.getName());
        pstmt.setString(2,college.getPresident());
        pstmt.setTimestamp(3,new Timestamp(college.getStartTime().getTime()));
        pstmt.setString(4,college.getTelephone());
        pstmt.setString(5,college.getEmail());
        pstmt.setString(6,college.getAddress());
        pstmt.setString(7,college.getProfile());
        pstmt.setInt(8,college.getId());
        //5.执行SQl,返回更新记录数
        count = pstmt.executeUpdate();
        //6. 关闭预备语句对象
        pstmt.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }finally {
        //关闭数据库连接
        ConnectionManager.closeConnection(conn);
    }

    //返回更新记录数
    return count;
}

}

(1)创建测试类TestCollegeDaoImpl
为了确保Dao层给上层提供正确的数据操作服务,我们应该进行单元测试,采用JUnit。
在根包net.lyy.student里创建test子包,在里面创建测试类TestCollegeDaoImpl
在这里插入图片描述在这里插入图片描述
在这里应该要导入一下Junit4,但由于我之前已经导入过所以这里没步骤,如果需要可以问下老师。

package net.lyy.student.test;

import net.lyy.student.bean.College;
import net.lyy.student.dao.CollegeDao;
import net.lyy.student.dao.impl.CollageDaoImpl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**

  • 功能:测试学校数据访问接口实现类

  • 姓名:李远燕

  • 日期:2019年6月27日
    */
    public class TestCollegeDaoImpl {
    //创建学校数据访问对象
    CollegeDao dao = new CollageDaoImpl();

    @Before
    public void beforeTest(){
    System.out.println(“温馨提示:单元测绘开始咯~”);
    }

    @After
    public void afterTest(){
    System.out.println(“温馨提示:单元测试结束咯~”);
    }

    @Test
    public void testFindById(){
    //调用学校数据访问对象的查询方法
    College college = dao.findById(1);
    //输出学校对象
    System.out.println(college);

    }

    @Test
    public void testUpadate(){
    //调用学校数据访问对象的查询方法
    College college = dao.findById(1);
    //修改学校信息
    college.setPresident(“李远燕”);
    //调用学校数据访问对象的更新方法
    int count = dao.update(college);
    //判断是否更新成功
    if (count > 0) {
    System.out.println(“学校记录更新成功!”);
    System.out.println(“新校长:” + dao.findById(1).getPresident());
    }else{
    System.out.println(“学校记录更新失败!”);
    }
    }
    }

运行结果如下
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值