dbUtils结果集处理器

1,首先要有c3p0-0.9.2-pre1.jar,commons-dbutils-1.4jar,mcchange-commons-0.2jar,mysql-connector-java-5.1.28-bin.jar包

2,在src同级目录下添加c3p0-config.xml文件 :

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <c3p0-config>
 3     <!-- 这是默认配置信息 -->
 4     <default-config>
 5         <!-- 连接四大参数配置 -->
 6         <property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
 7         <property name="driverClass">com.mysql.jdbc.Driver</property>
 8         <property name="user">root</property>
 9         <property name="password">[email protected]</property>
10         <!-- 池参数配置 -->
11         <property name="acquireIncrement">3</property>
12         <property name="initialPoolSize">10</property>
13         <property name="minPoolSize">2</property>
14         <property name="maxPoolSize">10</property>
15     </default-config>
16
17     <!-- 专门为oracle提供的配置信息 -->
18     <named-config name="oracle-config">
19         <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb1</property>
20         <property name="driverClass">com.mysql.jdbc.Driver</property>
21         <property name="user">root</property>
22         <property name="password">123</property>
23         <property name="acquireIncrement">3</property>
24         <property name="initialPoolSize">10</property>
25         <property name="minPoolSize">2</property>
26         <property name="maxPoolSize">10</property>
27     </named-config>
28
29 </c3p0-config>

3,写实体类:person的set,get方法,构造函数,等

4,引入jdbcUtils类:

 1 package cn.itcast.utils;
 2
 3 import java.sql.Connection;
 4 import java.sql.SQLException;
 5
 6 import javax.sql.DataSource;
 7
 8 import com.mchange.v2.c3p0.ComboPooledDataSource;
 9
10 public class JdbcUtils {
11     // 配置文件的默认配置!要求你必须给出c3p0-config.xml!!!
12     private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
13
14     /**
15      * 使用连接池返回一个连接对象
16      * @return
17      * @throws SQLException
18      */
19     public static Connection getConnection() throws SQLException {
20         return dataSource.getConnection();
21     }
22
23     /**
24      * 返回连接池对象!
25      * @return
26      */
27     public static DataSource getDataSource() {
28         return dataSource;
29     }
30 }

5:结果集处理的几种方法,非常简便,

  1 package cn.itcast.dbutils;
  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.BeanHandler;
  9 import org.apache.commons.dbutils.handlers.BeanListHandler;
 10 import org.apache.commons.dbutils.handlers.MapHandler;
 11 import org.apache.commons.dbutils.handlers.MapListHandler;
 12 import org.apache.commons.dbutils.handlers.ScalarHandler;
 13 import org.junit.Test;
 14
 15 import cn.itcast.jdbc.JdbcUtils;
 16
 17 public class Demo3 {
 18     @Test
 19     public void fun1() throws SQLException {
 20         QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
 21         String sql = "insert into t_stu values(?,?,?,?)";
 22         Object[] params = {1002, "liSi", 88, "female"};
 23
 24         qr.update(sql, params);
 25     }
 26
 27     @Test
 28     public void fun2() throws SQLException {
 29         // 创建QueryRunner,需要提供数据库连接池对象
 30         QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
 31         // 给出sql模板
 32         String sql = "select * from t_stu where sid=?";
 33         // 给出参数
 34         Object[] params = {1001};
 35
 36 //        ResultSetHandler<Stu> rsh = new ResultSetHandler<Stu>() {
 37 //
 38 //            @Override
 39 //            public Stu handle(ResultSet rs) throws SQLException {
 40 //                // TODO Auto-generated method stub
 41 //                return null;
 42 //            }
 43 //        };
 44         // 执行query()方法,需要给出结果集处理器,即ResultSetHandler的实现类对象
 45         // 我们给的是BeanHandler,它实现了ResultSetHandler
 46         // 它需要一个类型,然后它会把rs中的数据封装到指定类型的javabean对象中,然后返回javabean
 47         Stu stu = qr.query(sql, new BeanHandler<Stu>(Stu.class), params);
 48         System.out.println(stu);
 49     }
 50
 51     /**
 52      * BeanListHandler的应用,它是多行处理器
 53      * 每行对象一个Stu对象!
 54      * @throws Exception
 55      */
 56     @Test
 57     public void fun3() throws Exception {
 58         QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
 59         String sql = "select * from t_stu";
 60         List<Stu> stuList = qr.query(sql, new BeanListHandler<Stu>(Stu.class));
 61
 62         System.out.println(stuList);
 63     }
 64
 65     /**
 66      * MapHandler的应用,它是单行处理器,把一行转换成一个Map对象
 67      * @throws SQLException
 68      */
 69     @Test
 70     public void fun4() throws SQLException  {
 71         QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
 72         String sql = "select * from t_stu where sid=?";
 73         Object[] params = {1001};
 74         Map map = qr.query(sql, new MapHandler(), params);
 75
 76         System.out.println(map);
 77     }
 78
 79     /**
 80      * MapListHandler,它是多行处理器,把每行都转换成一个Map,即List<Map>
 81      * @throws SQLException
 82      */
 83     @Test
 84     public void fun5() throws SQLException  {
 85         QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
 86         String sql = "select * from t_stu";
 87         List<Map<String,Object>> mapList = qr.query(sql, new MapListHandler());
 88
 89         System.out.println(mapList);
 90     }
 91
 92     /**
 93      * ScalarHandler,它是单行单列时使用,最为合适!
 94      * @throws SQLException
 95      */
 96     @Test
 97     public void fun6() throws SQLException {
 98         QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
 99         String sql = "select count(*) from t_stu";
100         /*
101          * Integer、Long、BigInteger
102          */
103         Number cnt = (Number)qr.query(sql, new ScalarHandler());
104
105         long c = cnt.longValue();
106         System.out.println(c);
107     }
108 }
时间: 2025-01-02 15:13:57

dbUtils结果集处理器的相关文章

Apache DBUtils框架 结果处理器

package com.itheima.dbutil; 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.a

Commons - dbutils

时间:2016-12-4 18:06 --基本类 自己写的JDBC小工具: public class JDBCUtils { // 使用的是配置文件的默认配置,即必须给出c3p0-config.xml配置文件 private static ComboPooledDataSource dataSource = new ComboPooledDataSource(); /* * 使用连接池返回一个连接对象 */ public static Connection getConnection() thr

Apache的DBUtils框架学习

commons-dbutils简介 commons-dbutils是Apache组织提供的一个开源JDBC工具类库,它是对JDBC的简单封装,学习成本极低,并且使用dbutils能极大简化jdbc编码的工作量,同时也不会影响程序的性能.因此dbutils成为很多不喜欢hibernate的公司的首选. commons-dbutils API介绍: org.apache.commons.dbutils.QueryRunner QueryRunner中有update()和query()方法 org.a

开源JDBC工具类DbUtils

本篇将会详细地介绍Apache公司的JDBC帮助工具类DbUtils以及如何使用.在上一篇中我们已经通过将以前对dao层使用JDBC操作数据库的冗余代码进行了简易封装形成自己的简单工具类JdbcUtils,而在这过程中很多都是借鉴和参考了DbUtils的代码,因此通过上一篇的学习,会让我们在对DbUtils进行更快速简单的认识. 俗话说学习一个开源的工具最好的方法就是看其官方文档,是的,在Apache官网中对DbUtils进行了详细的介绍:http://commons.apache.org/pr

DBUtils学习

1.       DBUtils是JDBC的简单封装,可以和JDBC混合使用. 2.       DBUtils对结果集自动封装为JavaBean是有着苛刻要求的:必须满足JavaBean的规范,其次Bean的getter与setter方法的名字与结果集的列名一一对应,而不要求JavaBean的私有成员与表结果集列名一一对应. 比如: person表中有个字段叫:address,那么对应的JavaBean的Person类中必须有getAddress和setAddress两个方法,而Person类

Apache Commons DbUtils使用手册

Apache Commons DbUtils使用手册 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs 一.介绍 DBUtils是个小巧的JDBC轻量级封装的工具包,其最核心的特性是在JDBC的基础上做了一层封装,主要是对结果集的封装,可以直接将查询出来的结果集封装成JavaBean,旨在简化JDBC代码混乱与重复.JDBC代码开发,存在很多难点:1)操作过程复杂,代码操作一个模式,大量的重复.2)结果集难以处理.3)到处都强制检查SQLExcepti

数据库实用小工具之-DBUtils简单入门

1 DBUtils简介 DBUtils是Apache Commons组件中的一员,开源免费! DBUtils是对JDBC的简单封装,但是它还是被很多公司使用! DBUtils的Jar包:dbutils.jar 2 DBUtils主要类 ? DbUtils:都是静态方法,一系列的close()方法: ? QueryRunner: ? update():执行insert.update.delete: ? query():执行select语句: ? batch():执行批处理. OK,我们卡死写一个例

Apache的DBUtils使用详解

一.commons-dbutils简介 commons-dbutils 是 Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装,学习成本极低,并且使用dbutils能极大简化jdbc编码的工作量,同时也不会影响程序的性能. org.apache.commons.dbutils (该包中的类主要帮助我们更便捷的操作JDBC) org.apache.commons.dbutils.handlers(该包中的类都是实现org.apache.commons.dbutils.Re

数据库dbutils

common-dbutils.jar QueryRunner update方法:* int update(String sql, Object... params) -->  可执行增.删.改语句* int update(Connection con, String sql, Object... parmas) --> 需要调用者提供Connection,这说明本方法不再管理Connection了.支持事务! query方法:* T query(String sql, ResultSetHan