C3P0的两种使用方法
import java.sql.Connection;
import java.sql.SQLException;
import java.beans.PropertyVetoException;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class DBPool{
private static DBPool dbPool;
private ComboPooledDataSource dataSource;
static {
dbPool=new DBPool();
}
public DBPool(){
try {
dataSource=new ComboPooledDataSource();
dataSource.setUser( "id ");
dataSource.setPassword( "pw ");
dataSource.setJdbcUrl( "jdbc:mysql://127.0.0.1:3306/test?
autoReconnect=true&useUnicode=true&characterEncoding=GB2312 ");
dataSource.setDriverClass( "com.mysql.jdbc.Driver ");
dataSource.setInitialPoolSize(2);
dataSource.setMinPoolSize(1);
dataSource.setMaxPoolSize(10);
dataSource.setMaxStatements(50);
dataSource.setMaxIdleTime(60);
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
}
public final static DBPool getInstance(){
return dbPool;
}
public final Connection getConnection() {
try {
return dataSource.getConnection();
} catch (SQLException e) {
throw new RuntimeException( "无法从数据源获取连接 ",e);
}
}
public static void main(String[] args) throws SQLException {
Connection con = null;
try {
con = DBPool.getInstance().getConnection();
} catch (Exception e){
} finally {
if (con != null)
con.close();
}
}
}
原来不知道使用c3p0 是如此的简单,我一直使用properties 文件去配置c3p0,但总是连接不上数据库,后来调试才发现ComboPooledDataSource 这个对象的属性没有被设置成功,我是先获取了properties文件的内容,封装在一个 Properties对象里面,然后直接调用 ComboPooledDataSource 的 setProperties(Properties p) 方法来配置c3p0,程序是没有报错,但连不上数据库,调试发现属性都没有设置成功,只是properties这个属性被设置了而已,结果我对每个属性调用set方法后就连接上了。。。
public final class ConnectionManager {
private static ConnectionManager instance;
public ComboPooledDataSource ds;
private static String c3p0Properties = "c3p0.properties";
private ConnectionManager() throws Exception {
Properties p = new Properties();
p.load(this.getClass().getResourceAsStream(c3p0Properties));
ds = new ComboPooledDataSource();
ds.setUser(p.getProperty("user"));
ds.setPassword(p.getProperty("user"));
ds.setJdbcUrl(p.getProperty("user"));
ds.setDriverClass(p.getProperty("user"));
ds.setInitialPoolSize(Integer.parseInt(p.getProperty("initialPoolSize")));
ds.setMinPoolSize(Integer.parseInt(p.getProperty("minPoolSize")));
ds.setMaxPoolSize(Integer.parseInt(p.getProperty("maxPoolSize")));
ds.setMaxStatements(Integer.parseInt(p.getProperty("maxStatements")));
ds.setMaxIdleTime(Integer.parseInt(p.getProperty("maxIdleTime")));
}
public static final ConnectionManager getInstance() {
if (instance == null) {
try {
instance = new ConnectionManager();
} catch (Exception e) {
e.printStackTrace();
}
}
return instance;
}
public synchronized final Connection getConnection() {
try {
return ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
protected void finalize() throws Throwable {
DataSources.destroy(ds); // 关闭datasource
super.finalize();
}
}
如此就可以获取connection来做jdbc操作了:
Connection conn=ConnectionManager.getInstance().getConnection();
记得使用完后调用close方法:
conn.close();
c3p0 的某些参数的配置以及意义见另外一篇文章http://kangzye.blog.163.com/blog/static/368192232010442162576/
C3P0的两种使用方法的更多相关文章
- angular2系列教程(十)两种启动方法、两个路由服务、引用类型和单例模式的妙用
今天我们要讲的是ng2的路由系统. 例子
- git两种合并方法 比较merge和rebase
18:01 2015/11/18git两种合并方法 比较merge和rebase其实很简单,就是合并后每个commit提交的id记录的顺序而已注意:重要的是如果公司用了grrit,grrit不允许用m ...
- 两种Ajax方法
两种Ajax方法 Ajax是一种用于快速创建动态网页的技术,他通过在后台与服务器进行少量的数据交换,可以实现网页的异步更新,不需要像传统网页那样重新加载页面也可以做到对网页的某部分作出更新,现在这项技 ...
- mysql in 的两种使用方法
简述MySQL 的in 的两种使用方法: 他们各自是在 in keyword后跟一张表(记录集).以及在in后面加上字符串集. 先讲后面跟着一张表的. 首先阐述三张表的结构: s(sno,sname. ...
- C#中的两种debug方法
这篇文章主要介绍了C#中的两种debug方法介绍,本文讲解了代码用 #if DEBUG 包裹.利用宏定义两种方法,需要的朋友可以参考下 第一种:需要把调试方法改成debug代码用 #if DEBU ...
- Service的两种启动方法
刚才看到一个ppt,介绍service的两种启动方法以及两者之间的区别. startService 和 bindService startService被形容为我行我素,而bindService被形容 ...
- jQuery ajax调用后台aspx后台文件的两种常见方法(不是ashx)
在asp.net webForm开发中,用Jquery ajax调用aspx页面的方法常用的有两种:下面我来简单介绍一下. [WebMethod] public static string SayHe ...
- android studio gradle 两种更新方法更新
android studio gradle 两种更新方法更新 第一种.Android studio更新 第一步:在你所在项目文件夹下:你项目根目录gradlewrappergradle-wrapper ...
- iOS学习——UITableViewCell两种重用方法的区别
今天在开发过程中用到了UITableView,在对cell进行设置的时候,我发现对UITableViewCell的重用设置的方法有如下两种,刚开始我也不太清楚这两种之间有什么区别.直到我在使用方法二进 ...
随机推荐
- FC400A与400B的区别
FC400B就比400A多了一个功能,那就是联动,也就是说主机关了后,电子净化箱也会被关掉,这样就不需要去手动关闭电源,更加方便,估计是很多工业企业上需要这样的功能,所以就升级了这么个版本.
- win64位安装python-mysqldb1.2.5
在其他版本的mysqldb里面时间查询有问题最后确定还是在 1.2.5 版本下来解决,需要解决的问题就是这个:"Cannot open include file: 'config-win.h ...
- ios 使用AFN上传图片到服务器
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSe ...
- Linux命令的返回码列表
转自:http://blog.chinaunix.net/uid-10347480-id-3263127.html 在 Linux 下,不管你是启动一个桌面程序也好,还是在控制台下运行命令,所有的程序 ...
- logstash输出到influxdb
用了这个logstash扩展 https://github.com/PeterPaulH/logstash-influxdb/blob/master/src/influxdb.rb 把这个文件放到 l ...
- Python—redis
一.redis redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sor ...
- Asp.Net运行于32/64模式下的性能差异
项目使用的是Oracle数据库,.Net使用Odac对数据库进行访问.在官方下载Odac时会让你选择是下载32位的还是64位的.这会直接影响到网站的运行模式.目前我们使用的是32位的,那么布署时,需要 ...
- 《利用python进行数据分析》读书笔记--第七章 数据规整化:清理、转换、合并、重塑(三)
http://www.cnblogs.com/batteryhp/p/5046433.html 5.示例:usda食品数据库 下面是一个具体的例子,书中最重要的就是例子. #-*- encoding: ...
- DDS转换工具
本工具用于将(长宽均为 2 的指数次幂的)图像文件如 PNG.TGA.BMP.JPG 等转换为 DDS 图像(DXT 压缩类型可选,DXT1~DXT5),也可将 DDS 图像转换回此几种格式图片. 截 ...
- boost asio sync
Service: #include<boost/asio.hpp> #include<boost/thread.hpp> #include<iostream> #i ...