DBUtiles中的简单使用(QueryRunner和ResultSetHandler的手动实现)

DBUtiles是一个很好的处理JDBC的工具类。(DbUtils is a small set of classes designed to make working with JDBC easier )

DBUtiles中的QueryRunner和ResultSetHandler的手动实现

其中比较常用的就是QueryRunner类和ResultSetHandler接口。通过它们可以很方便的实现JDBC的功能。

QueryRunner类,有四个构造方法,其中有的构造方法可以接受一个DataSource

例如:QueryRunner runner = new QueryRunner(new ComboPooledDataSource());

当我们获得QueryRunner的实例对象时,就能通过QueryRunner类的方法方便的操作数据库。QueryRunner类主要有三类方法,batch()方法,query()方法,update()方法。

例如:

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. QueryRunner runner=new QueryRunner(new ComboPooledDataSource());  
  2.         runner.update("insert into account values(null,?,?)","e",888);  
  3.         runner.update("update account set money=0 where name=?", "e");  

 

查询的方法稍微麻烦一点,因为我们需要对查询到的结果集进行设置。通常需要把结果集ResultSet封装到JavaBean或者集合或者数组中。

查看一个方法:  <T> T   query(String sql, ResultSetHandler<T> rsh, Object... params) 

这里第一个参数是sql语句字符串,第二个参数是一个实现了ResultSetHandler接口的类对象,第三个参数是Object类型的可变参数。返回值是一个T类型。

如果我们用的eclipse或者MyEclipse 鼠标放到ResutlSetHandlet上面,按F2,会有针对T的说明。<T> the target type the input ResultSet will be converted to.

意思是,T 代表 ResultSet结果集要装入的目标类型。也就是我们前面提到的数组,集合,甚至javabean.

 

下面用一段代码来实现把结果集装入一个List数组中。其中Account是一个javaBean,符合account表。

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. public static List test2() throws Exception{  
  2.         QueryRunner runner = new QueryRunner(new ComboPooledDataSource());  
  3.         return runner.query("select * from account where name=?",new ResultSetHandler<List<Account>>(){  
  4.             public List<Account> handle(ResultSet rs) throws SQLException {  
  5.                 List<Account> list = new ArrayList<Account>();  
  6.                 while(rs.next()){  
  7.                     Account acc = new Account();  
  8.                     acc.setId(rs.getInt("id"));  
  9.                     acc.setName(rs.getString("name"));  
  10.                     acc.setMoney(rs.getDouble("money"));  
  11.                     list.add(acc);  
  12.                 }  
  13.                   
  14.                 return list;  
  15.             }  
  16.         } , "a");  
  17.     }  

 

接下来,我们用两段代码来模拟QueryRunner和ResultSetHandler的实现原理。

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package cn.itheima.dbutils;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.ParameterMetaData;  
  5. import java.sql.PreparedStatement;  
  6. import java.sql.ResultSet;  
  7. import java.sql.SQLException;  
  8. import java.util.ArrayList;  
  9. import java.util.List;  
  10.   
  11. import javax.sql.DataSource;  
  12.   
  13. import cn.itheima.domain.Account;  
  14.   
  15. import com.mchange.v2.c3p0.ComboPooledDataSource;  
  16.   
  17. public class MyQueryRunner {  
  18.     private DataSource source = null;  
  19.     public MyQueryRunner(DataSource source) {  
  20.         this.source = source;  
  21.     }  
  22.       
  23.     //查询原理:利用MyResourceHandler处理利用sql和objs拼写出来的sql语句查询出来的resultSet,处理  
  24.     public <T> T query(String sql,MyResultSetHandler<T> handler,Object ...objs){  
  25.         Connection conn = null;  
  26.         PreparedStatement ps = null;  
  27.         ResultSet rs = null;  
  28.         try {  
  29.             conn = source.getConnection();  
  30.             ps = conn.prepareStatement(sql);  
  31.             ParameterMetaData metaData = ps.getParameterMetaData();  
  32.             for(int i=1;i<=metaData.getParameterCount();i++){  
  33.                 ps.setObject(i, objs[i-1]);  
  34.             }  
  35.               
  36.             rs = ps.executeQuery();  
  37.             return handler.handle(rs);  
  38.               
  39.         } catch (Exception e) {  
  40.             e.printStackTrace();  
  41.             throw new RuntimeException();  
  42.         } finally {  
  43.             if (rs != null) {  
  44.                 try {  
  45.                     rs.close();  
  46.                 } catch (SQLException e) {  
  47.                     e.printStackTrace();  
  48.                 } finally {  
  49.                     rs = null;  
  50.                 }  
  51.             }  
  52.             if (ps != null) {  
  53.                 try {  
  54.                     ps.close();  
  55.                 } catch (SQLException e) {  
  56.                     e.printStackTrace();  
  57.                 } finally {  
  58.                     ps = null;  
  59.                 }  
  60.             }  
  61.             if (conn != null) {  
  62.                 try {  
  63.                     conn.close();  
  64.                 } catch (SQLException e) {  
  65.                     e.printStackTrace();  
  66.                 } finally {  
  67.                     conn = null;  
  68.                 }  
  69.             }  
  70.         }  
  71.   
  72.     }  
  73.       
  74.       
  75.     public int update(String sql,Object ...objs){  
  76.         Connection conn  = null;  
  77.         PreparedStatement ps = null;  
  78.         try{  
  79.             conn = source.getConnection();  
  80.             ps = conn.prepareStatement(sql);  
  81.             ParameterMetaData metaData = ps.getParameterMetaData();  
  82.             for(int i=1;i<=metaData.getParameterCount();i++){  
  83.                 ps.setObject(i, objs[i-1]);  
  84.             }  
  85.               
  86.             return ps.executeUpdate();  
  87.         }catch (Exception e) {  
  88.             e.printStackTrace();  
  89.             throw new RuntimeException();  
  90.         }finally{  
  91.             if(ps!=null){  
  92.                 try {  
  93.                     ps.close();  
  94.                 } catch (SQLException e) {  
  95.                     e.printStackTrace();  
  96.                 }finally{  
  97.                     ps = null;  
  98.                 }  
  99.             }  
  100.             if(conn!=null){  
  101.                 try {  
  102.                     conn.close();  
  103.                 } catch (SQLException e) {  
  104.                     e.printStackTrace();  
  105.                 }finally{  
  106.                     conn = null;  
  107.                 }  
  108.             }  
  109.         }  
  110.     }  
  111. }  

 

MyResultSetHandler接口

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package cn.itheima.dbutils;  
  2.   
  3. import java.sql.ResultSet;  
  4. import java.sql.SQLException;  
  5.   
  6. public interface  MyResultSetHandler <T>{  
  7.     T handle(ResultSet rs)throws SQLException;  
  8. }  

 

当然,实际应用中没有这么麻烦。因为DBUtils已经帮我们实现了很多ResultSetHandler的实现类。通过这些类可以很方便的对结果集进行封装。

 

ResultSetHandler的实现类

 

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
    1. //1.ArrayHandler 将查询结果每一行转换为一个数组对象返回  
    2.         //ResultSetHandler implementation that converts a ResultSet into an Object[]. This class is thread safe.  
    3.         Object[] objs = runner.query("select * from account where name=?",new ArrayHandler() , "c");  
    4.         System.out.println(objs);  
    5.           
    6.         //2.ArrayListHandler 将查询结果的每一行转换为一个Object[]数组,然后装入一个ArrayList集合  
    7.         //ResultSetHandler implementation that converts the ResultSet into a List of Object[]s. This class is thread safe.  
    8.         List<Object[]> list = runner.query("select * from account",new ArrayListHandler() );  
    9.         System.out.println(list);  
    10.   
    11.         //手动实现ArrayListHandler的功能  
    12.   
    13.         public static List test2() throws Exception{  
    14.         QueryRunner runner = new QueryRunner(new ComboPooledDataSource());  
    15.         return runner.query("select * from account where name=?",new ResultSetHandler<List<Account>>(){  
    16.             public List<Account> handle(ResultSet rs) throws SQLException {  
    17.                 List<Account> list = new ArrayList<Account>();  
    18.                 while(rs.next()){  
    19.                     Account acc = new Account();  
    20.                     acc.setId(rs.getInt("id"));  
    21.                     acc.setName(rs.getString("name"));  
    22.                     acc.setMoney(rs.getDouble("money"));  
    23.                     list.add(acc);  
    24.                 }  
    25.                 return list;  
    26.             }  
    27.         } , "a");  
    28.     }  
    29.           
    30.         //3.BeanHandler,将查询结果的第一行转换为一个JavaBean对象返回  
    31.         //ResultSetHandler implementation that converts the first ResultSet row into a JavaBean. This class is thread safe.  
    32.         Account acc = runner.query("select * from account where name=?",new BeanHandler<Account>(Account.class) , "c");  
    33.         System.out.println(acc);  
    34.           
    35.         //4.BeanListHandler:将结果集中的每一行数据都封装到一个对应的JavaBean实例中,存放到List里。  
    36.         //ResultSetHandler implementation that converts a ResultSet into a List of beans. This class is thread safe.  
    37.         List<Account> acclist = runner.query("select * from account",new BeanListHandler<Account>(Account.class) );  
    38.         System.out.println(acclist);  
    39.           
    40.         //5.MapHandler:将结果集中的第一行数据封装到一个Map里,key是列名,value就是对应的值。  
    41.         //ResultSetHandler implementation that converts the first ResultSet row into a Map. This class is thread safe.  
    42.         Map map = runner.query("select * from account",new MapHandler() );  
    43.         System.out.println(map);  
    44.           
    45.         //6.MapListHandler:将结果集中的每一行数据都封装到一个Map里,然后再存放到List  
    46.         //ResultSetHandler implementation that converts a ResultSet into a List of Maps. This class is thread safe  
    47.         List<Map<String, Object>> maplist = runner.query("select * from account",new MapListHandler() );  
    48.         System.out.println(maplist);  
    49.           
    50.         //7.ColumnListHandler:将结果集中某一列的数据存放到List中。  
    51.         //ResultSetHandler implementation that converts one ResultSet column into a List of Objects. This class is thread safe.  
    52.         List<Object> columnList = runner.query("select * from account",new ColumnListHandler(2) );  
    53.         System.out.println(columnList);  
    54.           
    55.         //8.KeyedHandler(name):将结果集中的每一行数据都封装到一个Map里(List<Map>),再把这些map再存到一个map里,其key为指定的列。  
    56.         //ResultSetHandler implementation that returns a Map of Maps. ResultSet rows are converted into Maps which are then stored in a Map under the given key.  
    57.         Map<Object, Map<String, Object>> keymap = runner.query("select * from account",new KeyedHandler("id") );  
    58.         System.out.println(keymap);  
    59.           
    60.         //9.ScalarHandler: 单值查询  
    61.         //ResultSetHandler implementation that converts one ResultSet column into an Object. This class is thread safe.  
    62.         //select count(*) from account;  
    63.         Long count = (Long)runner.query("select count(*) from account",new ScalarHandler(1) );  
    64.         System.out.println(count);  
posted @ 2017-01-25 15:22  Syria  阅读(1040)  评论(0编辑  收藏  举报