DBUtils:commons-dbutils是Apache组织提供的一个开源JDBC工具库,它是对JDBC的简单封装,并且使用dbutils会极大的简化jdbc编码的工作量,同时不会影响到程序的性能。
两个重要组成:
类:QueryRunner:SQL语句的操作对象,可以设置查询结果集的封装策略,线程安全。
接口:ResultSetHandle:封装数据的策略对象——将封装结果集中的数据,转换到另一个对象
方法之间的关系
实例代码:
public class DBUtils_12 {
//1.创建QueryRunner的实现类
QueryRunner queryRunner = new QueryRunner();
/**
* 1.测试QueryRunner的实现类的update方法
* SQL语句可以是增、删、改
* 步骤: ①获取连接
* ②准备SQL语句
* ③QueryRunner对象调用update方法,同时把连接、SQL语句、占位符传进去
* ④关闭连接
*/
@Test
public void testUpdate(){
String sql = "delete from customers where id in(?,?)";
Connection conn = null;
try {
conn = TestTools.getConnectionWithC3P0();
//2.使用update方法
queryRunner.update(conn, sql, 19,20);
} catch (Exception e) {
e.printStackTrace();
}finally{
TestTools.release(null, conn);
}
}
/**
* 2.测试是QueryRunner的Query方法
* SQL语句是查询
* 重点:QueryRunner的query方法的返回值取决于其ResultSetHandler参数的handle方法的返回值
* 步骤: ①获取连接
* ②准备SQL语句
* ③QueryRunner对象调用query方法,同时传入连接、SQL语句、和
*/
@Test
public void testQuery(){
Connection conn = null;
try {
conn = TestTools.getConnectionWithC3P0();
String sql = "select id, name, age, birth, address from customers";
Object obj = queryRunner.query(conn, sql, new MyResultSetHandler());
System.out.println(obj);
} catch (Exception e) {
e.printStackTrace();
}finally{
TestTools.release(null, conn);
}
}
//内部类
class MyResultSetHandler implements ResultSetHandler{
@Override
public Object handle(ResultSet resultSet) throws SQLException{
/* System.out.println("handle...");
return "test";*/
List<Customers> customers = new ArrayList<>();
while(resultSet.next()){
Integer id = resultSet.getInt(1);
String name = resultSet.getString(2);
String age = resultSet.getString(3);
Date birth = resultSet.getDate(4);
String address = resultSet.getString(5);
Customers customer = new Customers(id, name, age, birth, address);
customers.add(customer);
}
return customers;
}
}
/**
* 一些比较重要的Handler方法
* BeanHandler()
* BeanListHandler()
* MapHandler()
* MapListHandler()
* ScalarHandler()
*
*/
//BeanHandler:把结果集的第一条记录转为创建BeanHandler对象时传入的Class参数对应的对象。
@Test
public void testBeanHandler(){
Connection conn = null;
try {
conn = TestTools.getConnectionWithC3P0();
String sql = "select id, name, age, birth,address from customers where id = ?";
Customers customer = queryRunner.query(conn, sql, new BeanHandler(Customers.class), 17);
System.out.println(customer);
} catch (Exception e) {
e.printStackTrace();
}finally{
TestTools.release(null, conn);
}
}
//BeanListHandler:把结果集转化为一个List,让list不为null,但有可能为空集合(size(),返回0)
//若SQL语句能够查询到记录,List中存放创建BeanListHandler时传入的Class对象对应的对象
@Test
public void testBeanListHandler(){
Connection conn = null;
try {
conn = TestTools.getConnectionWithC3P0();
String sql = "select id, name, age, birth,address from customers";
List<Customers> customers = queryRunner.query(conn, sql, new BeanListHandler(Customers.class));
System.out.println(customers);
} catch (Exception e) {
e.printStackTrace();
}finally{
TestTools.release(null, conn);
}
}
//MapHandler:返回SQL语句对应的第一条记录对应的Map对象,键:SQL查询的列名(非别名) 值:列的值
@Test
public void testMapHandler(){
Connection conn = null;
try {
conn = TestTools.getConnectionWithC3P0();
String sql = "select id, name, age, birth,address from customers";
Map<String, Object> result = queryRunner.query(conn, sql, new MapHandler());
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}finally{
TestTools.release(null, conn);
}
}
//MapListHandler:将结果集转为Map的List,Map对应查询的一条记录,MapListHandler返回多条记录对应的Map的集合
@Test
public void testMapListHandler(){
Connection conn = null;
try {
conn = TestTools.getConnectionWithC3P0();
String sql = "select id, name, age, birth,address from customers";
List<Map<String, Object>> results = queryRunner.query(conn, sql, new MapListHandler());
System.out.println(results);
} catch (Exception e) {
e.printStackTrace();
}finally{
TestTools.release(null, conn);
}
}
//ScalarHandler:把结果集转为一个数值返回,这个数值可以是任意数据类型的和字符串,Data等。
@Test
public void testScalarHandler(){
Connection conn = null;
try {
conn = TestTools.getConnectionWithC3P0();
String sql = "select count(id) from customers";
Object results = queryRunner.query(conn, sql, new ScalarHandler());
System.out.println(results);
} catch (Exception e) {
e.printStackTrace();
}finally{
TestTools.release(null, conn);
}
}
}