CustomerDAO及CustomerDAOImpl的实现
public interface CustomerDAO {
void insert(Connection conn, Customer cust);
void deleteById(Connection conn,int id);
void updateById(Connection conn,Customer cust);
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);
}
}
}