P44CustomerDAO及CustomerDAOImpl的实现与测试

CustomerDAO及CustomerDAOImpl的实现

/*
此接口用于规范针对于customers表的常用操作
 */
public interface CustomerDAO {
    //将cust对象添加到数据库中
    void insert(Connection conn, Customer cust);

    //根据指定的ID删除表中的一条记录
    void deleteById(Connection conn,int id);

    //针对内存中的cust对象,去修改数据表中指定的记录
    void updateById(Connection conn,Customer cust);

    //针对指定的id查询得到对应的customer对象
    Customer getCustomerById(Connection conn,int id);

    //查询表中的所有记录构成的集合
    List<Customer> getAll(Connection conn);

    //返回数据表中数据的条目数
    Long getCount(Connection conn);

    //返回数据表中最大的生日
    Date getMaxBirth(Connection conn);
}
public class CustomerDAOImpl extends BaseDAO implements CustomerDAO {

    @Override
    public void insert(Connection conn, Customer cust) {
        String sql="insert into customers(name,email,birth)values(?,?,?)";
        update(conn, sql, cust.getName(),cust.getEmail(),cust.getBirth());
    }

    @Override
    public void deleteById(Connection conn, int id) {
        String sql="delete from customers where id=?";
        update(conn, sql, id);
    }

    @Override
    public void updateById(Connection conn, Customer cust) {
        String sql="update customers set name=?,email=?,birth=? where id=?";
        update(conn, sql, cust.getName(),cust.getEmail(),cust.getBirth(),cust.getId());
    }

    @Override
    public Customer getCustomerById(Connection conn, int id) {
        String sql="select id,name,email,birth from customers where id=?";
        Customer customer = getInstance(conn, Customer.class, sql, id);
        return customer;
    }

    @Override
    public List<Customer> getAll(Connection conn) {
        String sql="select id,name,email,birth from customers";
        List<Customer> list = getForList(conn, Customer.class, sql);
        return list;
    }

    @Override
    public Long getCount(Connection conn) {
        String sql="select count(*) from customers";
        return getValue(conn, sql);
    }

    @Override
    public Date getMaxBirth(Connection conn) {
        String sql="select max(birth) from customers";
        return getValue(conn, sql);
    }
}

CustomerDAOImpl的测试

public class testCustomerDAOImpl {
    private CustomerDAOImpl dao=new CustomerDAOImpl();

    @Test
    public void testInsert()  {
        Connection conn = null;
        try {
            conn = JDBCUtil.getConnection();
            Customer cust = new Customer(1, "cxy", "cxy@qq.com", new Date(4213123123L));
            dao.insert(conn,cust);
            System.out.println("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.closeResource(conn, null);
        }
    }
    @Test
    public void testDeleteById(){
        Connection conn = null;
        try {
            conn = JDBCUtil.getConnection();

            dao.deleteById(conn, 13);
            System.out.println("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.closeResource(conn, null);
        }
    }
    @Test
    public void testUpdateConnectionCustomer(){
        Connection conn = null;
        try {
            conn = JDBCUtil.getConnection();
            Customer cust = new Customer(18, "贝多芬", "bei@qq.com", new Date(321321321L));
            dao.updateById(conn,cust);
            System.out.println("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.closeResource(conn, null);
        }
    }

    @Test
    public void testGetCustomerById(){
        Connection conn = null;
        try {
            conn = JDBCUtil.getConnection();

            Customer cust = dao.getCustomerById(conn, 18);
            System.out.println(cust);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.closeResource(conn, null);
        }
    }
    @Test
    public void testGetAll(){
        Connection conn = null;
        try {
            conn = JDBCUtil.getConnection();

            List<Customer> list = dao.getAll(conn);
            list.forEach(System.out::println);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.closeResource(conn, null);
        }
    }
    @Test
    public void testGetCount(){
        Connection conn = null;
        try {
            conn = JDBCUtil.getConnection();

            Long count = dao.getCount(conn);
            System.out.println("表中记录数为:"+count);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.closeResource(conn, null);
        }
    }
    @Test
    public void testMaxBirth(){
        Connection conn = null;
        try {
            conn = JDBCUtil.getConnection();

            Date maxBirth = dao.getMaxBirth(conn);
            System.out.println("最大生日为:"+maxBirth);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.closeResource(conn, null);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值