本节学习目的:
1. 体验使用DbUtils组件的便利性
2. 对DbUtils组件实现结果集的快速封装进行详细讲解
public class App_query {
private Connection conn = null;
DbUtils支持使用自定义的ResultSetHandler处理结果集
这种处理方式的优点在于:拥有极大的灵活性
缺点在于:实现方式比较繁琐,增加编码复杂度
我们先看看使用这个组件实现的自定义结果集处理方法testMyQuery():
// 使用DbUtils提供的自定义封装结果集的方法
@Test
public void testMyQuery() {
String sql = "select * from admin where id = ?";
try {
conn = JdbcUtil.getConnection();
QueryRunner qr = new QueryRunner();
Admin admin = qr.query(conn, sql, new ResultSetHandler() {
@Override
public Admin handle(ResultSet rs) throws SQLException {
Admin admin = new Admin();
while (rs.next()) {
admin.setId(rs.getInt("id"));
admin.setUserName(rs.getString("userName"));
admin.setPwd(rs.getString("pwd"));
admin.setAge(rs.getInt("age"));
}
return admin;
}
}, 4);
System.out.println(admin);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
JdbcUtil.closeAll(conn, null, null);
}
}
使用DbUtils提供的方法,可以对结果集进行快速的封装
/**
* 1.BeaListHandler:将返回结果用bean封装起来
*/
@Test
public void testQueryOne() {
String sql = "select * from admin where id = ?";
try {
conn = JdbcUtil.getConnection();
QueryRunner qr = new QueryRunner();
Admin admin = qr.query(conn, sql, new BeanHandler(
Admin.class), 4);
System.out.println(admin);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
JdbcUtil.closeAll(conn, null, null);
}
}
这种返回值类型通过泛型进行指定,传入对应类的class类型即可
/**
* 2.BeanListHandler:将返回结果用list集合封装起来,其中的元素类型是Admin
*/
@Test
public void testQueryAll() {
String sql = "select * from admin";
try {
conn = JdbcUtil.getConnection();
QueryRunner qr = new QueryRunner();
List list = qr.query(conn, sql, new BeanListHandler(
Admin.class));
System.out.println(list);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
JdbcUtil.closeAll(conn, null, null);
}
}
/**
* 3. ScalarHandler:返回结果集中第一行第一列的内容,常用于聚合函数中
*
* @throws SQLException
*/
@Test
public void testQuery3() throws Exception {
String sql = "select count(*) from admin";
conn = JdbcUtil.getConnection();
QueryRunner qr = new QueryRunner();
Long in = qr.query(conn, sql, new ScalarHandler());
System.out.println(in);
conn.close();
}
/**
* 4. ArrayHandler:将结果集中第一行中的内容封装成为数组返回
*
* @throws Exception
*/
@Test
public void testQuery4() throws Exception {
String sql = "select * from admin";
conn = JdbcUtil.getConnection();
QueryRunner qr = new QueryRunner();
Object[] arr = qr.query(conn, sql, new ArrayHandler());
for (Object o : arr) {
System.out.println(o);
}
conn.close();
}
/**
* 5. ArrayListHandler:以List集合的形式返回结果集 将每一行以数组封装起来,然后添加到list集合中
*
* @throws Exception
*/
@Test
public void testQuery5() throws Exception {
String sql = "select * from admin";
conn = JdbcUtil.getConnection();
QueryRunner qr = new QueryRunner();
List
/**
* 7. MapListHandler:以Map的形式返回结果集第一行的内容
*
* @throws Exception
*/
@Test
public void testQuery7() throws Exception {
String sql = "select * from admin";
conn = JdbcUtil.getConnection();
QueryRunner qr = new QueryRunner();
List
/**
* 8. ColumnListHandler:根据传入的列名称或者列的下标获取结果集中的一列内容
*
* @throws Exception
*/
@Test
public void testQuery8() throws Exception {
String sql = "select * from admin";
conn = JdbcUtil.getConnection();
QueryRunner qr = new QueryRunner();
List list = qr.query(conn, sql, new ColumnListHandler(
"pwd"));
conn.close();
}
根据传入的列名称或者列的下标作为map的key(默认以第一列作为key), 使用结果集中的所有列值组成map的value
/**
* 9. :根据传入的列名称或者列的下标作为map的key(默认以第一列作为key),
* 使用结果集中的所有列值组成map的value
*/
@Test
public void testQuery9() throws Exception {
String sql = "select * from admin";
conn = JdbcUtil.getConnection();
QueryRunner qr = new QueryRunner();
Map