c0p3的导入请参考前文

https://www.cnblogs.com/appium/p/10183016.html

JdbcUtils:

package cn.itcast.jdbc;

import java.sql.Connection;
import java.sql.SQLException; import javax.sql.DataSource; import com.mchange.v2.c3p0.ComboPooledDataSource; public class JdbcUtils {
// 配置文件的默认配置!要求你必须给出c3p0-config.xml!!!
private static ComboPooledDataSource dataSource = new ComboPooledDataSource(); /**
* 使用连接池返回一个连接对象
* @return
* @throws SQLException
*/
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
} /**
* 返回连接池对象!
* @return
*/
public static DataSource getDataSource() {
return dataSource;
}
}

列子:

import java.sql.SQLException;
import java.util.List;
import java.util.Map; import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
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 cn.itcast.jdbc.JdbcUtils; public class Demo3 {
@Test
public void fun1() throws SQLException {
QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
String sql = "insert into t_stu values(?,?,?,?)";
Object[] params = {1002, "liSi", 88, "female"}; qr.update(sql, params);
} @Test
public void fun2() throws SQLException {
// 创建QueryRunner,需要提供数据库连接池对象
QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
// 给出sql模板
String sql = "select * from t_stu where sid=?";
// 给出参数
Object[] params = {1001}; // ResultSetHandler<Stu> rsh = new ResultSetHandler<Stu>() {
//
// @Override
// public Stu handle(ResultSet rs) throws SQLException {
// // TODO Auto-generated method stub
// return null;
// }
// };
// 执行query()方法,需要给出结果集处理器,即ResultSetHandler的实现类对象
// 我们给的是BeanHandler,它实现了ResultSetHandler
// 它需要一个类型,然后它会把rs中的数据封装到指定类型的javabean对象中,然后返回javabean
Stu stu = qr.query(sql, new BeanHandler<Stu>(Stu.class), params);
System.out.println(stu);
} /**
* BeanListHandler的应用,它是多行处理器
* 每行对象一个Stu对象!
* @throws Exception
*/
@Test
public void fun3() throws Exception {
QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
String sql = "select * from t_stu";
List<Stu> stuList = qr.query(sql, new BeanListHandler<Stu>(Stu.class)); System.out.println(stuList);
} /**
* MapHandler的应用,它是单行处理器,把一行转换成一个Map对象
* @throws SQLException
*/
@Test
public void fun4() throws SQLException {
QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
String sql = "select * from t_stu where sid=?";
Object[] params = {1001};
Map map = qr.query(sql, new MapHandler(), params); System.out.println(map);
} /**
* MapListHandler,它是多行处理器,把每行都转换成一个Map,即List<Map>
* @throws SQLException
*/
@Test
public void fun5() throws SQLException {
QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
String sql = "select * from t_stu";
List<Map<String,Object>> mapList = qr.query(sql, new MapListHandler()); System.out.println(mapList);
} /**
* ScalarHandler,它是单行单列时使用,最为合适!
* @throws SQLException
*/
@Test
public void fun6() throws SQLException {
QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
String sql = "select count(*) from t_stu";
/*
* Integer、Long、BigInteger
*/
Number cnt = (Number)qr.query(sql, new ScalarHandler()); long c = cnt.longValue();
System.out.println(c);
}
}

commons.dbutils 的使用列子的更多相关文章

  1. 高性能jdbc封装工具 Apache Commons DbUtils 1.6(转载)

    转载自原文地址:http://gao-xianglong.iteye.com/blog/2166444 前言 关于Apache的DbUtils中间件或许了解的人并不多,大部分开发人员在生成环境中更多的 ...

  2. Apache Commons DbUtils 快速上手

    原文出处:http://lavasoft.blog.51cto.com/62575/222771 Hibernate太复杂,iBatis不好用,JDBC代码太垃圾,DBUtils在简单与优美之间取得了 ...

  3. 写一个ORM框架的第一步(Apache Commons DbUtils)

    新一次的内部提升开始了,如果您想写一个框架从Apache Commons DbUtils开始学习是一种不错的选择,我们先学习应用这个小“框架”再把源代码理解,然后写一个属于自己的ORM框架不是梦. 一 ...

  4. java JDBC (七) org.apache.commons.dbutils 查询

    package cn.sasa.demo1; import java.sql.Connection; import java.sql.SQLException; import java.util.Li ...

  5. java JDBC (六) org.apache.commons.dbutils 增删改

    dbutils是apache封装了JDBC的工具类,比mysql-connector更方便些 下载地址:http://commons.apache.org/proper/commons-dbutils ...

  6. Java连接数据库 #04# Apache Commons DbUtils

    索引 通过一个简单的调用看整体结构 Examples 修改JAVA连接数据库#03#中的代码 DbUtils并非是什么ORM框架,只是对原始的JDBC进行了一些封装,以便我们少写一些重复代码.就“用” ...

  7. 《笔者带你剖析Apache Commons DbUtils 1.6》(转)

    前言 关于Apache的DbUtils中间件或许了解的人并不多,大部分开发人员在生成环境中更 多的是依靠Hibernate.Ibatis.Spring JDBC.JPA等大厂提供的持久层技术解决方案, ...

  8. java.lang.ClassNotFoundException: org.apache.commons.dbutils.QueryRunner

    七月 28, 2017 11:06:33 下午 org.apache.catalina.core.StandardWrapperValve invoke严重: Servlet.service() fo ...

  9. 模拟apache commons dbutils 实现自己的BeanListHandler(回调应用)

    首先dbcp相关的jar包和MySQL的驱动包导入到项目中. dbcp.properties配置文件如下,并放到项目根目录下. driverClassName=com.mysql.jdbc.Drive ...

随机推荐

  1. 小记——GTMD校园网

    前言 学校一年前开通了校园网,然鹅信号未覆盖我们住的公寓,又多忍受了一年的小破宽带(10M带宽,100块300个小时) 上个星期,架设了一年的校园网终于通了,然后我们发现——校园网69元一个月,一个用 ...

  2. [转]C#——细说事务

    转自:风尘浪子 - 博客园 引言 其实事务在数据层.服务层.业务逻辑层多处地方都会使用到,在本篇文章将会为大家一一细说. 其中前面四节是事务的基础,后面的三节是事务的重点,对事务有基础的朋友可以跳过前 ...

  3. 【2014秋季版】【辛星php】【0】清晰的认识一下PHP语言

    *********************PHP情结***************** 1.假设您和我经历非常相似,也可能会有这种PHP情结,为什么呢.由于我最先学习的是Java.然后学习了C++,开 ...

  4. ios 使用Safari浏览器跳转打开、唤醒app

    常常使用Safari浏览器浏览网页点击url会唤醒该站点的手机版app 须要在app的project中设置 1.打开project中的myapp-Info.plist文件 2.打开文件里新增URL T ...

  5. POJ 3748:位操作

    位操作 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8964   Accepted: 3581 Description 如 ...

  6. Apache Traffic Server 5.3.1公布

    本文来源于我在InfoQ中文站翻译的文章,原文地址是:www.infoq.com/cn/news/2015/07/traffic-server-5.3.1-release 近日,Apache软件基金会 ...

  7. Codeforces Round #332 (Div. 2)A. Patrick and Shopping 水

    A. Patrick and Shopping   Today Patrick waits for a visit from his friend Spongebob. To prepare for ...

  8. 关于form/input 的autocomplete="off"属性

    转自:http://blog.sina.com.cn/s/blog_b49f96a701019m0d.html 一. 有过表单设计经验的朋友肯定知道,当我们在浏览器中输入表单信息的时候,往往input ...

  9. 第14课 SourceTree程序操作介绍

    http://www.atlassian.com/software/sourcetree/overview https://www.microsoft.com/net/framework/versio ...

  10. Ruby类扩张(extension)

    创建: 2017/09/07 更新: 2017/09/16 修改标题字母大小写 ruby ---> Ruby    扩张类  class 类名     扩张的内容  end           ...