码迷,mamicode.com
首页 > 数据库 > 详细

20160410javaweb之JDBC---DBUtils框架

时间:2016-04-10 10:11:45      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:

DBUtils

1.DbUtils
工具类

2.QueryRunner -- 两行代码搞定增删改查

(1)QueryRunner() --需要控制事务时,使用这组方法
int update(Connection conn, String sql)
Execute an SQL INSERT, UPDATE, or DELETE query without replacement parameters.
int update(Connection conn, String sql, Object... params)
Execute an SQL INSERT, UPDATE, or DELETE query.
int update(Connection conn, String sql, Object param)
Execute an SQL INSERT, UPDATE, or DELETE query with a single replacement parameter.

<T> T query(Connection conn, String sql, ResultSetHandler<T> rsh)
Execute an SQL SELECT query without any replacement parameters.
<T> T query(Connection conn, String sql, ResultSetHandler<T> rsh, Object... params)
Execute an SQL SELECT query with replacement parameters.

(2)QueryRunner(DataSource ds) --不需要控制事务用这组方法
int update(String sql)
Executes the given INSERT, UPDATE, or DELETE SQL statement without any replacement parameters.
int update(String sql, Object... params)
Executes the given INSERT, UPDATE, or DELETE SQL statement.
int update(String sql, Object param)
Executes the given INSERT, UPDATE, or DELETE SQL statement with a single replacement parameter.

<T> T query(String sql, ResultSetHandler<T> rsh)
Executes the given SELECT SQL without any replacement parameters.
<T> T query(String sql, ResultSetHandler<T> rsh, Object... params)
Executes the given SELECT SQL query and returns a result object.

package com.dzq.dbutils;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.junit.Test;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class DbUtilsUpdate {
   
    @Test
    public void add() throws SQLException{
        QueryRunner runner=new QueryRunner(new ComboPooledDataSource());
        runner.update("update account set money=? where name=?",888,"a");
    }
}
package com.dzq.dbutils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.junit.Test;

import com.dzq.domain.Account;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class DBUtilsQuery {
   @Test
   public void find() throws SQLException{
       QueryRunner runner=new QueryRunner(new ComboPooledDataSource());
       String sql="select * from account where money>?";
     List<Account> list=  runner.query(sql, new ResultSetHandler<List<Account>>() {

        @Override
        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;
        }
       
    },500);
     System.out.println(list);
   }
}

 

 

3.ResultSetHandler 实现类

ArrayHandler:把结果集中的第一行数据转成对象数组。
ArrayListHandler:把结果集中的每一行数据都转成一个对象数组,再存放到List中。
!!!!BeanHandler:将结果集中的第一行数据封装到一个对应的JavaBean实例中。
!!!!BeanListHandler:将结果集中的每一行数据都封装到一个对应的JavaBean实例中,存放到List里。
MapHandler:将结果集中的第一行数据封装到一个Map里,key是列名,value就是对应的值。
MapListHandler:将结果集中的每一行数据都封装到一个Map里,然后再存放到List
ColumnListHandler:将结果集中某一列的数据存放到List中。
KeyedHandler(name):将结果集中的每一行数据都封装到一个Map里(List<Map>),再把这些map再存到一个map里,其key为指定的列。
!!!!!ScalarHandler:获取结果集中第一行数据指定列的值,常用来进行单值查询

示例代码:

package com.itheima.dbutils;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ColumnListHandler;
import org.apache.commons.dbutils.handlers.KeyedHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.junit.Test;

import com.itheima.domain.Account;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class RSHanlderDemo {
    //ScalarHandler:获取结果集中第一行数据指定列的值,常用来进行单值查询
    @Test
    public void tes9() throws SQLException{
        QueryRunner runner = new QueryRunner(new ComboPooledDataSource());
        Long count = (Long)runner.query("select count(*) from account",new ScalarHandler());
        System.out.println(count);
    }
    
    //KeyedHandler(name):将结果集中的每一行数据都封装到一个Map里(List<Map>),再把这些map再存到一个map里,其key为指定的列。
    @Test
    public void tes8() throws SQLException{
        QueryRunner runner = new QueryRunner(new ComboPooledDataSource());
         Map<Object, Map<String, Object>> map = runner.query("select * from account where money>?", new KeyedHandler("id"),500);
        System.out.println(map);
    }
    //ColumnListHandler:将结果集中某一列的数据存放到List中。
    @Test
    public void tes7() throws SQLException{
        QueryRunner runner = new QueryRunner(new ComboPooledDataSource());
        List<Object>list = runner.query("select * from account where money>?", new ColumnListHandler(3),500);
        System.out.println(list);
    }
    //MapListHandler:将结果集中的每一行数据都封装到一个Map里,然后再存放到List
    @Test
    public void tes6() throws SQLException{
        QueryRunner runner = new QueryRunner(new ComboPooledDataSource());
        List<Map<String, Object>> list = runner.query("select * from account where money>?", new MapListHandler(),500);
        System.out.println(list);
    }
    
    //MapHandler:将结果集中的第一行数据封装到一个Map里,key是列名,value就是对应的值。
    @Test
    public void tes5() throws SQLException{
        QueryRunner runner = new QueryRunner(new ComboPooledDataSource());
         Map<String, Object> map = runner.query("select * from account where money>?", new MapHandler(),500);
        System.out.println(map);
    }
    
    //BeanListHandler:将结果集中的每一行数据都封装到一个对应的JavaBean实例中,存放到List里。
    @Test
    public void tes4() throws SQLException{
        QueryRunner runner = new QueryRunner(new ComboPooledDataSource());
        List<Account>list = runner.query("select * from account where money>?", new BeanListHandler<Account>(Account.class),500);
        System.out.println(list);
    }
    
    //BeanHandler:将结果集中的第一行数据封装到一个对应的JavaBean实例中。
    @Test
    public void tes3() throws SQLException{
        QueryRunner runner = new QueryRunner(new ComboPooledDataSource());
        Account acc = runner.query("select * from account where money>?", new BeanHandler<Account>(Account.class),500);
        System.out.println(acc);
    }
    //ArrayListHandler:把结果集中的每一行数据都转成一个对象数组,再存放到List中。
    @Test
    public void tes2() throws SQLException{
        QueryRunner runner = new QueryRunner(new ComboPooledDataSource());
        List<Object[]> list = runner.query("select * from account where money>?", new ArrayListHandler(),500);
        System.out.println(list);
    }
    
    //ArrayHandler:把结果集中的第一行数据转成对象数组。
    @Test
    public void test1() throws SQLException{
        QueryRunner runner = new QueryRunner(new ComboPooledDataSource());
        Object[] objs = runner.query("select * from account where money>?", new ArrayHandler(),500);
        System.out.println(objs);
    }
}

 

20160410javaweb之JDBC---DBUtils框架

标签:

原文地址:http://www.cnblogs.com/xiaoduc-org/p/5373409.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!