1 package cn.itcast.dbutil; 2 3 import java.sql.SQLException; 4 import java.util.List; 5 import java.util.Map; 6 7 import org.apache.commons.dbutils.QueryRunner; 8 import org.apache.commons.dbutils.handlers.ArrayHandler; 9 import org.apache.commons.dbutils.handlers.ArrayListHandler;10 import org.apache.commons.dbutils.handlers.BeanHandler;11 import org.apache.commons.dbutils.handlers.BeanListHandler;12 import org.apache.commons.dbutils.handlers.ColumnListHandler;13 import org.apache.commons.dbutils.handlers.KeyedHandler;14 import org.apache.commons.dbutils.handlers.MapHandler;15 import org.apache.commons.dbutils.handlers.MapListHandler;16 import org.apache.commons.dbutils.handlers.ScalarHandler;17 import org.junit.Test;18 19 import cn.itcast.domain.Account;20 import cn.itcast.util.DBCPUtil;21 22 //查询练习23 public class DbUtilDemo2 {24 private QueryRunner qr = new QueryRunner(DBCPUtil.getDataSource());25 @Test//BeanHandler26 public void test1() throws SQLException{27 Account a = qr.query("select * from account where id=?", new BeanHandler (Account.class), 1);28 System.out.println(a);29 }30 @Test//BeanListHandler31 public void test2() throws SQLException{32 List list = qr.query("select * from account", new BeanListHandler (Account.class));33 for(Account a:list)34 System.out.println(a);35 }36 37 @Test//ArrayHandler:把结果集中的第一行数据转成对象数组。只适合结果集有一条记录的情况38 public void test3() throws SQLException{39 //该数组中每个元素就是记录的每列的值40 Object objs[] = qr.query("select * from account where id=?", new ArrayHandler(),1);41 for(Object o:objs)42 System.out.println(o);43 }44 @Test//ArrayListHandler:把结果集中的每一行数据都转成一个数组,再存放到List中。45 public void test4() throws SQLException{46 //该数组中每个元素就是记录的每列的值47 List