DbUtil组件及C3P0数据库连接池组件的使用
DbUtils
是 Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装,学习成本极低,并且使用dbutils能极大简化jdbc编码的工作量,同时也不会影响程序的性能。
使用commons-dbutils 的核心工具类:QueryRunner,该类定义了所有操作数据库的方法
如方法:T query(Connection conn ,String sql, ResultSetHandler<T> rsh, Object... params)
DbUtils提供的封装结果的一些对象:
1) BeanHandler 查询返回单个对象(常用)
2) BeanListHandler 查询返回list集合,集合元素是指定的对象(常用)
3) ArrayHandler 查询返回结果记录的第一行,封装对对象数组, 即返回:Object[]
4) ArrayListHandler 把查询的每一行都封装为对象数组,再添加到list集合中
5) ScalarHandler 查询返回结果记录的第一行的第一列 (在聚合函数统计的时候用)
6) MapHandler 查询返回结果的第一条记录封装为map
使用:
前提:实体类必须符合javabean规范,并且实体类中的字段必须与数据库表的字段相同。引入jar文件 : commons-dbutils-1.6.jar
1、简单创建一个工具类JdbcUtil,方便代码调用
public class JdbcUtil {
private static String url = "jdbc:mysql:///test01";
private static String user = "root";
private static String password = "123456";
//获取QueryRunner对象
public static QueryRunner getQueryRunner(){
return new QueryRunner();
}
//获取连接
public static Connection getConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
return DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
2、调用query方法
@Test
public void test1() {
List<Student> list = null;
try {
Connection conn = JdbcUtil.getConnection();
list = JdbcUtil.getQueryRunner().query(conn, "select * from Student"
, new BeanListHandler<Student>(Student.class));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
if(list !=null){
for (Student student : list) {
System.out.println(student);
}
}
}
使用C3P0数据库连接池组件优化程序性能
C3P0核心类:ComboPooledDataSource
使用:
前提 引入jar文件 : c3p0-0.9.1.2.jar
方式一:不使用配置文件
1、简单建立一个jdbc工具类,方便方法调用
import java.beans.PropertyVetoException;
import java.sql.Connection;
import org.apache.commons.dbutils.QueryRunner; import com.mchange.v2.c3p0.ComboPooledDataSource; public class JdbcUtil {
private static String url = "jdbc:mysql:///test01";
private static String user = "root";
private static String password = "123456";
private static ComboPooledDataSource dataSource = null;
private Connection con = null;
static{
//初始化操作
dataSource = new ComboPooledDataSource();// 使用默认的配置
dataSource.setJdbcUrl(url);//设置连接字符串
try {
dataSource.setDriverClass("com.mysql.jdbc.Driver");//获取驱动
} catch (PropertyVetoException e) {
e.printStackTrace();
}
dataSource.setUser(user);//用户名
dataSource.setPassword(password);//密码
dataSource.setInitialPoolSize(3);//初始化时获取三个连接
dataSource.setMaxPoolSize(6);//连接池中保留的最大连接数
dataSource.setMaxIdleTime(60); //最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃
} //获取QueryRunner对象
public static QueryRunner getQueryRunner(){
return new QueryRunner(dataSource);
}
//获取连接纯 通过c3p0核心类对象获取(此例子没用到该方法)
public static Connection getConnection(){
try {
return dataSource.getConnection();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
2、执行测试
@Test
public void test1() {
List<Student> list = null;
try {
Connection conn = JdbcUtil.getConnection();
list = JdbcUtil.getQueryRunner().query("select * from Student"
, new BeanListHandler<Student>(Student.class));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
if(list !=null){
for (Student student : list) {
System.out.println(student);
}
}
}
方式二:使用配置文件来初始化
1、将C3P0配置文件c3p0-config.xml放置在工程src目录下
c3p0-config.xml
<c3p0-config>
<!-- 默认加载配置 -->
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test01</property>
<property name="user">root</property>
<property name="password">123456</property>
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">10</property>
</default-config>
<!-- 指定名称加载配置 -->
<named-config name="C3P0TestName">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test01</property>
<property name="user">root</property>
<property name="password">123456</property>
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">10</property>
</named-config> </c3p0-config>
2、简单编写一个工具类,方便代码调用
import java.sql.Connection;
import org.apache.commons.dbutils.QueryRunner; import com.mchange.v2.c3p0.ComboPooledDataSource; public class JdbcUtil {
private static ComboPooledDataSource dataSource = null;
static{
//初始化操作
// 自动加载src目录下c3p0的配置文件【c3p0-config.xml】
dataSource = new ComboPooledDataSource();// 使用默认的配置
//使用c3p0-config.xml配置文件中named-config的name属性为C3P0TestName的配置
//dataSource = new ComboPooledDataSource("C3P0TestName");
} //获取QueryRunner对象
public static QueryRunner getQueryRunner(){
return new QueryRunner(dataSource);
}
//获取连接纯 通过c3p0核心类对象获取(此例子没用到该方法)
public static Connection getConnection(){
try {
return dataSource.getConnection();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
3、执行测试
@Test
public void test1() {
List<Student> list = null;
try {
Connection conn = JdbcUtil.getConnection();
list = JdbcUtil.getQueryRunner().query("select * from Student"
, new BeanListHandler<Student>(Student.class));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
if(list !=null){
for (Student student : list) {
System.out.println(student);
}
}
}
完毕.
DbUtil组件及C3P0数据库连接池组件的使用的更多相关文章
- python Mysql数据库连接池组件封装(转载)
以前一直在用Java来开发,数据库连接池等都是有组件封装好的,直接使用即可,最近在尝试Python的学习,碰到了和数据库打交道的问题,和数据库打交道我们都知道,数据库连接池必不可少,不然要么就是程序异 ...
- Hibernate第十一篇【配置C3P0数据库连接池、线程Session】
Hibernate连接池 Hibernate自带了连接池,但是呢,该连接池比较简单..而Hibernate又对C3P0这个连接池支持-因此我们来更换Hibernate连接池为C3P0 查看Hibern ...
- [原创]java WEB学习笔记80:Hibernate学习之路--- hibernate配置文件:JDBC 连接属性,C3P0 数据库连接池属性等
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- c3p0数据库连接池无法连接数据库—错误使用了username关键字
一.问题描述 上篇博客说到了关于maven无法下载依赖jar包的问题,这篇博客再说一下关于在本个项目中遇到的关于使用C3P0连接池连接数据库的问题,真心很奇葩,在此,也请大家引起注意.首先看我的项目基 ...
- 【Java EE 学习 16 上】【dbcp数据库连接池】【c3p0数据库连接池】
一.回顾之前使用的动态代理的方式实现的数据库连接池: 代码: package day16.utils; import java.io.IOException; import java.lang.ref ...
- paip.c3p0 数据库连接池 NullPointerException 的解决...
paip.c3p0 数据库连接池 NullPointerException 的解决... 程序ide里面运行正常..外面bat运行错误.. 作者Attilax 艾龙, EMAIL:14665198 ...
- paip.提升稳定性---c3p0数据库连接池不能取到连接An attempt by a client to checkout a Connection has timed out
paip.提升稳定性---c3p0数据库连接池不能取到连接An attempt by a client to checkout a Connection has timed out 作者Attilax ...
- c3p0数据库连接池(作用不重复)
/* * c3p0数据库连接池: * 只被初始化一次 * connection对象进行close时,不是正的关闭,而是将该数据连接归还给数据库连接池 * * */ 四个架包 mysql-connect ...
- 第32讲 UI组件之 时间日期控件DatePicker和TimePicker
第32讲 UI组件之 时间日期控件DatePicker和TimePicker 在Android中,时间日期控件相对来说还是比较丰富的.其中, DatePicker用来实现日期输入设置, Time ...
随机推荐
- HDU 4195 Regular Convex Polygon
思路:三角形的圆心角可以整除(2*pi)/n #include<cstdio> #include<cstring> #include<iostream> #incl ...
- Casio普通计算器编程
用xelatex写了个奇怪的东西……欢乐向 PDF http://files.cnblogs.com/htfy/calc.pdf TEX http://files.cnblogs.com/htfy/ ...
- hadoop-2.6.0.tar.gz + spark-1.5.2-bin-hadoop2.6.tgz 的集群搭建(3节点和5节点皆适用)
本人呕心沥血所写,经过好一段时间反复锤炼和整理修改.感谢所参考的博友们!同时,欢迎前来查阅赏脸的博友们收藏和转载,附上本人的链接.http://www.cnblogs.com/zlslch/p/584 ...
- 8-14-Exercise
8-14-小练 这次是我这组出题......我出的是B.C.D[虽然本来是想出的很难......╮(╯▽╰)╭但是,没找到AC1000+同时又让我想出的难题......SO...我出的真的不难= =] ...
- C#中异步和多线程的区别
C#中异步和多线程的区别是什么呢?异步和多线程两者都可以达到避免调用线程阻塞的目的,从而提高软件的可响应性.甚至有些时候我们就认为异步和多线程是等同的概念.但是,异步和多线程还是有一些区别的.而这些区 ...
- Java 动态生成 复杂 .doc文件
阅读目录 1.word 里面调整好排版,包括你想生成的动态部分,还有一些不用生成的规则性的文字 2. 将 word 文档保存为 xml 3.用 Firstobject free XML edito 打 ...
- Hadoop权威指南(中文版,第2版)【分享】
下载地址 Hadoop权威指南(中文版,第2版) http://download.csdn.net/download/u011000529/5726789 (友情提示:请点击右下的 “联通下载” 或者 ...
- cocos2d-x学习笔记
转自:http://blog.csdn.net/we000636/article/details/8263503 接受触屏事件的优先级是值越小,响应触屏事件的优先级越高 Z值越大,越外面 JNI:允许 ...
- Ubuntu 12.04 安装搜狗输入法
安装指南 Ubuntu / Ubuntu Kylin 14.04 LTS 版本 只需双击下载的 deb 软件包,即可直接安装搜狗输入法. Ubuntu 12.04 LTS 版本 由于 Ubuntu 1 ...
- [Android]Fragment源代码分析(二) 状态
我们上一讲,抛出来一个问题,就是当Activity的onCreateView的时候,是怎样构造Fragment中的View參数.要回答这个问题我们先要了解Fragment的状态,这是Fragment管 ...