dbUtils的增删查改操作和九个处理器

dbutils是对JDBC的一个简单的包装,可以说是JDBC的一个比较实用的工具,现在有些公司还都在使用dbutils开发,所以wo'm我们有必要了解一下dbutils。首先说一个dbutils的一个环境,使用dbutils需要commons-dbutils-1.2.jar jar包依赖,如果c3p0做连接池还需要c3p0-0.9.2-pre1.jar 和 mchange-commons-0.2.jar jar包,如果shi'使用dbcp做连接池,还需要commons-dbcp-1.2.2.jar 和 commons-pool.jar 和 commons-logging-1.1.1.jar jar包依赖。搭建好huan环境,下面我们来了解一下处理器的名称和作用,增删除改操作都是基于某些处理器上实现的。

结果集处理器:                                                      作用
        (1)ArrayHandler                                  把结果集第一行数据转化成对象数组
        (2)ArrayListHandler                             把结果集中的每一行数据都转成一个数组,在存放到List中
        (3)BeanHandler                                  将结果集的第一行封装到第一个对应的JavaBean实例中
        (4)BeanListHander                             将结果集中的每一行数据都封装到一个对应的JavaBean实例中,存放到List里。
        (5)ColumnListHandler("列名)               将结果集中某一列的数据存放到List中
        (6)KeyedHandler(key)                        将结果集中的每一行数据都封装到一个Map里,再把这些map再存到一个map里,
                                                                 其key为指定的key
        (7)MapHandler                                   将结果集中的第一行数据封装到一个Map里,key是列名,value就是对应的值。
        (8)MapListHandler                              将结果集中的每一行数据都封装到一个Map里,然后再存放到List
        (9)ScalarHandler                                将处理的结果转到一个对象中去    

 具体案例如下:

    @Test
    public void Insert() throws SQLException{
        QueryRunner runner = new QueryRunner(JDBCUtil_c3p0.getDataSource());
        System.out.println("runner:"+runner);
        String sql = "insert into user(id,name,password,email,birthday) values(?,?,?,?,?)";
        Object params[] = {1,"aaa","123","[email protected]",new Date()};
        runner.update(sql, params);
    }
    
    @Test
    public void update() throws SQLException{
        QueryRunner runner = new QueryRunner(JDBCUtil_c3p0.getDataSource());
        System.out.println("runner:"+runner);
        String sql = "update user set email = ? where id = ?";
        Object params[] = {"[email protected]",1};
        runner.update(sql, params);
    }
    
    @Test
    public void delete() throws SQLException{
        QueryRunner runner = new QueryRunner(JDBCUtil_c3p0.getDataSource());
        System.out.println("runner:"+runner);
        String sql = "delete from user where id = ?";
        runner.update(sql, 1);
    }
    
    /**
         BeanHandler(对像)
         User:com.nyist.dbutils.model.user@5b464ce8
     * @throws SQLException
     */
    @Test
    public void find() throws SQLException{
        QueryRunner runner = new QueryRunner(JDBCUtil_c3p0.getDataSource());
        System.out.println("runner:"+runner);
        String sql = "select * from user where id = ?";
        user user = (user) runner.query(sql, 1, new BeanHandler(user.class));
        System.out.println("User:"+user);
    }
    
    /**
         BeanListHandler(对象)
         user:[com.nyist.dbutils.model.user@340f438e, com.nyist.dbutils.model.user@30c7da1e, com.nyist.dbutils.model.user@5b464ce8]
     * @throws SQLException
     */
    @Test
    public void getAll() throws SQLException{
        QueryRunner runner = new QueryRunner(JDBCUtil_c3p0.getDataSource());
        System.out.println("runner:"+runner);
        String sql = "select * from user";
        List<user> user = (List<user>) runner.query(sql,new BeanListHandler(user.class));
        System.out.println("user:"+user);
    }
    
    //batch批处理sql
    @Test
    public void batch() throws SQLException{
        QueryRunner runner = new QueryRunner(JDBCUtil_c3p0.getDataSource());
        String sql = "insert into user(id,name,password,email,birthday) values(?,?,?,?,?)";
        Object params[][] = new Object[3][5];                        //二位数组用来保存sql 的长度和sql 的个数
        int i =0;
        for(i=0;i<params.length;i++){
            params[i] =  new Object[]{i+1,"ccc","123",i+"[email protected]",new Date()};            //利用for循环填充二位数组
        }
        runner.batch(sql, params);
    }
    
    /**
         ScalarHandler
         TotalRecord:3
     * @throws SQLException
     */
    @Test
    public void getCount() throws SQLException{
        QueryRunner runner = new QueryRunner(JDBCUtil_c3p0.getDataSource());
        String sql = "select count(*) from user";
        //Object result[] = (Object[]) runner.query(sql,new ArrayHandler());
        /*    第一种做法
        long num = (long)result[0];
        int number = (int)num;
        System.out.println("number:"+number);*/
        /*第二种做法
        int totalrecord = ((Long)result[0]).intValue();
        System.out.print("TotalRecord:"+totalrecord);*/
        /*
         第三种做法   使用ScalarHandler        作用:将处理的结果转到一个对象中去
         * */
        int totalrecord = ((Long)runner.query(sql,new ScalarHandler())).intValue();
        System.out.println("TotalRecord:"+totalrecord);
    }

处理器应用案例:

package com.nyist.dbutils.Demo;

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.ColumnListHandler;
import org.apache.commons.dbutils.handlers.KeyedHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.junit.Test;

import com.nyist.dbutils.Utils.JDBCUtil_c3p0;

public class Demo2 {
    
    /**
         ArrayHandler()
         Object:1
        Object:[email protected]
     * @throws SQLException
     */
    @Test
    public void test1() throws SQLException{
        QueryRunner runner = new QueryRunner(JDBCUtil_c3p0.getDataSource());
        System.out.println("runner:"+runner);
        String sql = "select * from user where id = ?";
        Object object[]=  (Object[]) runner.query(sql, 1, new ArrayHandler());
        System.out.println("Object:"+object[0]);
        System.out.println("Object:"+object[3]);
    }
    
    /**
         ArrayListHandler()
         List:[[Ljava.lang.Object;@57829d67, [Ljava.lang.Object;@19dfb72a, [Ljava.lang.Object;@17c68925]
     * @throws SQLException
     */
    @Test
    public void test2() throws SQLException{
        QueryRunner runner = new QueryRunner(JDBCUtil_c3p0.getDataSource());
        System.out.println("runner:"+runner);
        String sql = "select * from user";
        List list = (List)runner.query(sql,new ArrayListHandler());
        System.out.println("List:"+list);
    }
    
    /**
         ColumnListHandler()
         List:[[email protected], [email protected], [email protected]]
     * @throws SQLException
     */
    @Test
    public void test3() throws SQLException{
        QueryRunner runner = new QueryRunner(JDBCUtil_c3p0.getDataSource());
        System.out.println("runner:"+runner);
        String sql = "select * from user";
        List list = (List)runner.query(sql,new ColumnListHandler("email"));
        System.out.println("List:"+list);
    }
    
    /**
         KeyedHandler("key")
         ID:1
        birthday=2018-07-18
        password=123
        name=ccc
        id=1
        [email protected]
        ID:2
        birthday=2018-07-18
        password=123
        name=ccc
        id=2
        [email protected]
        ID:3
        birthday=2018-07-18
        password=123
        name=ccc
        id=3
        [email protected]
     * @throws SQLException
     */
    @Test
    public void test4() throws SQLException{
        QueryRunner runner = new QueryRunner(JDBCUtil_c3p0.getDataSource());
        System.out.println("runner:"+runner);
        String sql = "select * from user";
        Map<Integer,Map<String,Object>> map = (Map<Integer,Map<String,Object>>) runner.query(sql,new KeyedHandler("id"));
        for(Map.Entry<Integer,Map<String,Object>> me : map.entrySet()) {
            int id = me.getKey();
            System.out.println("ID:"+id);
            for(Map.Entry<String,Object> entry:me.getValue().entrySet()){
                String key = entry.getKey();
                Object object = entry.getValue();
                System.out.println(key + "=" +object);
            }
        }
    }
    
    /**
         MapHandler()
         birthday=2018-07-18
        password=123
        name=ccc
        id=3
        [email protected]
     * @throws SQLException
     */
    @Test
    public void test5() throws SQLException{
        QueryRunner runner = new QueryRunner(JDBCUtil_c3p0.getDataSource());
        System.out.println("runner:"+runner);
        String sql = "select * from user where id = ?";
        Map<String,Object> map = (Map) runner.query(sql,3,new MapHandler());
        for(Map.Entry<String,Object> me:map.entrySet()){
            String key = me.getKey();
            Object object = me.getValue();
            System.out.println(key+"="+object);
        }
    }
    
    /**
         MapListHandler()
         birthday=2018-07-18
        password=123
        name=ccc
        id=1
        [email protected]
        birthday=2018-07-18
        password=123
        name=ccc
        id=2
        [email protected]
        birthday=2018-07-18
        password=123
        name=ccc
        id=3
        [email protected]
     * @throws SQLException
     */
    @Test
    public void test6() throws SQLException{
        QueryRunner runner = new QueryRunner(JDBCUtil_c3p0.getDataSource());
        System.out.println("runner:"+runner);
        String sql = "select * from user";
        List<Map<String,Object>> list = (List) runner.query(sql,new MapListHandler());
        for(int i=0;i<list.size();i++){
            Map<String,Object> map = list.get(i);
            for (Map.Entry<String,Object> entry:map.entrySet()) {
                String key = entry.getKey();
                Object object = entry.getValue();
                System.out.println(key+"="+object);
            }
        }
    }
}
 

猜你喜欢

转载自blog.csdn.net/lvhaoguang0/article/details/81094036
今日推荐