•commons-dbutils 是 Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装。学习成本极低。而且使用dbutils能极大简化jdbc编码的工作量,同一时候也不会影响程序的性能。
•API介绍:
–org.apache.commons.dbutils.QueryRunner
–org.apache.commons.dbutils.ResultSetHandler
–工具类
•org.apache.commons.dbutils.DbUtils。
QueryRunner类有两类主要方法:
query() : 用于查询数据
update() : 用于增删改
ResultSetHandler接口用与将查询到的数据按要求转换为还有一种格式。
•ArrayHandler:把结果集中的第一行数据转成对象数组。
•ArrayListHandler:把结果集中的每一行数据都转成一个数组,再存放到List中。
•BeanHandler:将结果集中的第一行数据封装到一个相应的JavaBean实例中。
•BeanListHandler:将结果集中的每一行数据都封装到一个相应的JavaBean实例中,存放到List里。
•ColumnListHandler:将结果集中某一列的数据存放到List中。
•KeyedHandler(name):将结果集中的每一行数据都封装到一个Map里,再把这些map再存到一个map里,其key为指定的key。
•MapHandler:将结果集中的第一行数据封装到一个Map里,key是列名,value就是相应的值。
•MapListHandler:将结果集中的每一行数据都封装到一个Map里,然后再存放到List
-------------------------------------------------下面为详细的Demo--------------------------------------------------------------
QueryRunner.update()
<span style="font-size:14px;"> /** * 測试 QueryRunner 类的 update 方法 * 该方法可用于 INSERT, UPDATE 和 DELETE */ @Test public void testQueryRunnerUpdate() { //1. 创建 QueryRunner 的实现类 QueryRunner queryRunner = new QueryRunner(); String sql = "DELETE FROM customers " + "WHERE id IN (?,?)"; Connection connection = null; try { connection = JDBCTools.getConnection(); //2. 使用其 update 方法 queryRunner.update(connection, sql, 12, 13); } catch (Exception e) { e.printStackTrace(); } finally{ JDBCTools.releaseDB(null, null, connection); } }</span>
BeanHandler:把结果集的第一条记录转为创建BeanHandler对象时传入的Class參数相应的对象.
<span style="font-family:SimSun;font-size:14px;"><span style="font-size:14px;font-weight: normal;">public void testBeanHandler() {
QueryRunner queryRunner = new QueryRunner();
Connection conn = null;
try {
conn = JDBCTools.getConnection();
String sql = "SELECT id, name customerName, email, birth "
+ "FROM customers WHERE id>=?";
Customer customer = queryRunner.query(conn, sql, new BeanHandler(
Customer.class), 5);
System.out.println(customer);
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.releaseDB(null, null, conn);
}
}</span></span>
BeanListHandler:把结果集转为一个List,该List不为null,但可能为空集合(即size()方法返回0)
<span style="font-family:SimSun;font-size:14px;">public void testBeanListHandler(){
String sql = "SELECT id, name customerName, email, birth " +
"FROM customers";
QueryRunner queryRunner = new QueryRunner();
Connection conn = null;
try {
conn = JDBCTools.getConnection();
Object object = queryRunner.query(conn, sql,
new BeanListHandler<>(Customer.class));
System.out.println(object);
} catch (Exception e) {
e.printStackTrace();
} finally{
JDBCTools.releaseDB(null, null, conn);
}
}</span>
MapHandler:返回SQL语句相应的第一条记录相应的Map对象.键:SQL查询的列名(不是列的别名),值:列的值.
<span style="font-family:SimSun;font-size:14px;"><span style="font-size:14px;font-weight: normal;">public void testMapHandler(){
Connection connection = null;
QueryRunner queryRunner = new QueryRunner();
String sql = "SELECT id, name customerName, email, birth " +
"FROM customers WHERE id = ?";
try {
connection = JDBCTools.getConnection();
Map<String, Object> map = queryRunner.query(connection,
sql, new MapHandler(), 4);
System.out.println(map);
} catch (Exception e) {
e.printStackTrace();
} finally{
JDBCTools.releaseDB(null, null, connection);
}
}</span></span>
MapLiatHandler:将结果转化为一个Map的List. MapLiatHandler:返回的多条记录相应的Map的集合.
"; try { connection = JDBCTools.getConnection(); Map<String, Object> map = queryRunner.query(connection, sql, new MapHandler(), 4); System.out.println(map); } catch (Exception e) { e.printStackTrace(); } finally{ JDBCTools.releaseDB(null, null, connection); } }</span></span>
<span style="font-family:SimSun;font-size:14px;font-weight: normal;">public void testMapListHandler(){
Connection connection = null;
QueryRunner queryRunner = new QueryRunner();
String sql = "SELECT id, name, email, birth " +
"FROM customers";
try {
connection = JDBCTools.getConnection();
List<Map<String, Object>> mapList = queryRunner.query(connection,
sql, new MapListHandler());
System.out.println(mapList);
} catch (Exception e) {
e.printStackTrace();
} finally{
JDBCTools.releaseDB(null, null, connection);
}
}</span>
ScalarHandler:把结果集转为一个数值(能够是随意类型)返回
<span style="font-family:SimSun;">public void testScalarHandler(){ Connection connection = null; QueryRunner queryRunner = new QueryRunner(); String sql = "SELECT name FROM customers " + "WHERE id = ?"; try { connection = JDBCTools.getConnection(); Object count = queryRunner.query(connection, sql, new ScalarHandler(), 6); System.out.println(count); } catch (Exception e) { e.printStackTrace(); } finally{ JDBCTools.releaseDB(null, null, connection); } }</span>