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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM