QueryRunner 结果处理器

QueryRunner 结果处理器

复制代码
 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//BeanHandler
26     public void test1() throws SQLException{
27         Account a = qr.query("select * from account where id=?", new BeanHandler<Account>(Account.class), 1);
28         System.out.println(a);
29     }
30     @Test//BeanListHandler
31     public void test2() throws SQLException{
32         List<Account> list = qr.query("select * from account", new BeanListHandler<Account>(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<Object[]> list = qr.query("select * from account", new ArrayListHandler());
48         for(Object[] objs:list){
49             System.out.println("-----------------");
50             for(Object o:objs)
51                 System.out.println(o);
52         }
53     }
54     @Test//ColumnListHandler:将结果集中某一列的数据存放到List中
55     public void test5() throws SQLException{
56         List<Object> list = qr.query("select * from account", new ColumnListHandler("id"));
57         for(Object o:list)
58             System.out.println(o);
59     }
60     @Test//KeyedHandler(name):将结果集中的每一行数据都封装到一个Map<列名,列值>里,再把这些map再存到一个map里,其key为指定的key。
61     public void test6() throws SQLException{
62         Map<Object, Map<String,Object>> bmap= qr.query("select * from account", new KeyedHandler("id"));
63         for(Map.Entry<Object, Map<String,Object>> bme:bmap.entrySet()){
64             System.out.println("-----------------");
65             Map<String,Object> lmap = bme.getValue();
66             for(Map.Entry<String,Object> lme:lmap.entrySet()){
67                 System.out.println(lme.getKey()+"="+lme.getValue());
68             }
69         }
70     }
71     @Test//MapHandler:将结果集中的第一行数据封装到一个Map里,key是列名,value就是对应的值。
72     public void test7() throws SQLException{
73         Map<String,Object> map= qr.query("select * from account where id=?", new MapHandler(),1);
74         for(Map.Entry<String, Object> me:map.entrySet()){
75             System.out.println(me.getKey()+"="+me.getValue());
76         }
77     }
78     @Test//MapListHandler:将结果集中的每一行数据都封装到一个Map里,然后再存放到List
79     public void test8() throws SQLException{
80         List<Map<String,Object>> list= qr.query("select * from account", new MapListHandler());
81         for(Map<String,Object> map:list){
82             System.out.println("-----------------");
83             for(Map.Entry<String, Object> me:map.entrySet()){
84                 System.out.println(me.getKey()+"="+me.getValue());
85             }
86         }
87     }
88     @Test//ScalarHandler 适合取一条一列的记录。比如记录总数
89     public void test9() throws SQLException{
90         Object obj = qr.query("select count(*) from account", new ScalarHandler(1));
91         System.out.println(obj.getClass().getName());
92         int num = ((Long)obj).intValue();
93         System.out.println(num);
94     }
95 }

猜你喜欢

转载自whxhz.iteye.com/blog/2295483