标签:for cli ack base ase fork img lis number
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()方法。
例如:
- QueryRunner runner=new QueryRunner(new ComboPooledDataSource());
- runner.update("insert into account values(null,?,?)","e",888);
- 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表。
- public static List test2() throws Exception{
- QueryRunner runner = new QueryRunner(new ComboPooledDataSource());
- return runner.query("select * from account where name=?",new ResultSetHandler<List<Account>>(){
- public List<Account> handle(ResultSet rs) throws SQLException {
- List<Account> list = new ArrayList<Account>();
- while(rs.next()){
- Account acc = new Account();
- acc.setId(rs.getInt("id"));
- acc.setName(rs.getString("name"));
- acc.setMoney(rs.getDouble("money"));
- list.add(acc);
- }
-
- return list;
- }
- } , "a");
- }
接下来,我们用两段代码来模拟QueryRunner和ResultSetHandler的实现原理。
MyResultSetHandler接口
- package cn.itheima.dbutils;
-
- import java.sql.ResultSet;
- import java.sql.SQLException;
-
- public interface MyResultSetHandler <T>{
- T handle(ResultSet rs)throws SQLException;
- }
当然,实际应用中没有这么麻烦。因为DBUtils已经帮我们实现了很多ResultSetHandler的实现类。通过这些类可以很方便的对结果集进行封装。
ResultSetHandler的实现类
-
- Object[] objs = runner.query("select * from account where name=?",new ArrayHandler() , "c");
- System.out.println(objs);
-
-
-
- List<Object[]> list = runner.query("select * from account",new ArrayListHandler() );
- System.out.println(list);
-
-
-
- public static List test2() throws Exception{
- QueryRunner runner = new QueryRunner(new ComboPooledDataSource());
- return runner.query("select * from account where name=?",new ResultSetHandler<List<Account>>(){
- public List<Account> handle(ResultSet rs) throws SQLException {
- List<Account> list = new ArrayList<Account>();
- while(rs.next()){
- Account acc = new Account();
- acc.setId(rs.getInt("id"));
- acc.setName(rs.getString("name"));
- acc.setMoney(rs.getDouble("money"));
- list.add(acc);
- }
- return list;
- }
- } , "a");
- }
-
-
-
- Account acc = runner.query("select * from account where name=?",new BeanHandler<Account>(Account.class) , "c");
- System.out.println(acc);
-
-
-
- List<Account> acclist = runner.query("select * from account",new BeanListHandler<Account>(Account.class) );
- System.out.println(acclist);
-
-
-
- Map map = runner.query("select * from account",new MapHandler() );
- System.out.println(map);
-
-
-
- List<Map<String, Object>> maplist = runner.query("select * from account",new MapListHandler() );
- System.out.println(maplist);
-
-
-
- List<Object> columnList = runner.query("select * from account",new ColumnListHandler(2) );
- System.out.println(columnList);
-
-
-
- Map<Object, Map<String, Object>> keymap = runner.query("select * from account",new KeyedHandler("id") );
- System.out.println(keymap);
-
-
-
-
- Long count = (Long)runner.query("select count(*) from account",new ScalarHandler(1) );
- System.out.println(count);
DBUtiles中的简单使用(QueryRunner和ResultSetHandler的手动实现)
标签:for cli ack base ase fork img lis number
原文地址:http://www.cnblogs.com/Syria/p/6349531.html