commons.dbutils 的使用列子
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 的使用列子的更多相关文章
- 高性能jdbc封装工具 Apache Commons DbUtils 1.6(转载)
转载自原文地址:http://gao-xianglong.iteye.com/blog/2166444 前言 关于Apache的DbUtils中间件或许了解的人并不多,大部分开发人员在生成环境中更多的 ...
- Apache Commons DbUtils 快速上手
原文出处:http://lavasoft.blog.51cto.com/62575/222771 Hibernate太复杂,iBatis不好用,JDBC代码太垃圾,DBUtils在简单与优美之间取得了 ...
- 写一个ORM框架的第一步(Apache Commons DbUtils)
新一次的内部提升开始了,如果您想写一个框架从Apache Commons DbUtils开始学习是一种不错的选择,我们先学习应用这个小“框架”再把源代码理解,然后写一个属于自己的ORM框架不是梦. 一 ...
- java JDBC (七) org.apache.commons.dbutils 查询
package cn.sasa.demo1; import java.sql.Connection; import java.sql.SQLException; import java.util.Li ...
- java JDBC (六) org.apache.commons.dbutils 增删改
dbutils是apache封装了JDBC的工具类,比mysql-connector更方便些 下载地址:http://commons.apache.org/proper/commons-dbutils ...
- Java连接数据库 #04# Apache Commons DbUtils
索引 通过一个简单的调用看整体结构 Examples 修改JAVA连接数据库#03#中的代码 DbUtils并非是什么ORM框架,只是对原始的JDBC进行了一些封装,以便我们少写一些重复代码.就“用” ...
- 《笔者带你剖析Apache Commons DbUtils 1.6》(转)
前言 关于Apache的DbUtils中间件或许了解的人并不多,大部分开发人员在生成环境中更 多的是依靠Hibernate.Ibatis.Spring JDBC.JPA等大厂提供的持久层技术解决方案, ...
- java.lang.ClassNotFoundException: org.apache.commons.dbutils.QueryRunner
七月 28, 2017 11:06:33 下午 org.apache.catalina.core.StandardWrapperValve invoke严重: Servlet.service() fo ...
- 模拟apache commons dbutils 实现自己的BeanListHandler(回调应用)
首先dbcp相关的jar包和MySQL的驱动包导入到项目中. dbcp.properties配置文件如下,并放到项目根目录下. driverClassName=com.mysql.jdbc.Drive ...
随机推荐
- Grails,应该不错
就当学习英文,也慢慢看看啦..
- monitor cursor
客户提出了一个需求,他们改进了自己的程序,想证明程序现在open cursor变少了,也就是说程序运行过程中 open cursor的峰值变小了. 我写了一个脚本来进行这个监控. oracle[aaa ...
- PHP array_diff_assoc()
定义和用法 array_diff_assoc() 函数返回两个数组的差集数组.返回的数组的元素都取自被比较的数组(既第一个数组). 和 array_diff() 函数 不同,本函数要求键名和键值都进行 ...
- CF #327 DIV2 D、E
两题都不难. 对于D题,可以使用相对移动,把吹aircraft移动变成相反方向的待援点的移动.假设此时时间为t,把aircraft的速度设为Vmax,看待援点在飞船最大速度飞行t秒的范围内,注意风向变 ...
- ios 使用Starscream实现websocket简单例子
调试了半天,出现 websocket is disconnected: Invalid HTTP upgrade 的错误 居然是 URL 地址写错了的原因,端口号之后还有一堆地址没有写上. 另外wss ...
- 刚開始学习的人非常有用之chm结尾的參考手冊打开后无法正常显示
从网上下载了struts2的參考手冊.chm(本文适用全部已.chm结尾的文件)不能正常打开使用. 如图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/ ...
- HDU 5538/ 2015长春区域 L.House Building 水题
题意:求给出图的表面积,不包括底面 #include<bits/stdc++.h> using namespace std ; typedef long long ll; #define ...
- 20170322Linux
- bzoj5085: 最大
暴力是4方的,开始我只3方(扫描的时候更新当前最大) 二分+暴力可以做到m^2logMAX 二分答案,暴力枚举可行的两个位置形成一段,对于段,最多只会有m^2种情况. #include<cstd ...
- CockroachDB——类似spanner的开源版,底层使用rocksdb存储,mvcc,支持事务,raft一致性,licence是CockroachDB Community License Agreement
摘自:https://github.com/cockroachdb/cockroach/blob/master/docs/design.md CockroachDB is a distributed ...