方法一:

package   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/


最新文章

  1. c++ 解包tar
  2. 使用Glyph Designer创建位图字体
  3. [moka同学笔记]yii2 activeForm 表单样式的修改
  4. itellyou MSDN, 我告诉你 win7系统工具等
  5. C# PDF添加水印
  6. POJ 1166 The Clocks (爆搜 || 高斯消元)
  7. Struts1、Struts2和SpringMVC剖析【转载】
  8. 微信开放平台获取component_verify_ticket
  9. 把Orchard部署到Windows Azure Web Sites
  10. ArrayList内元素按照字典排序
  11. hadoop运行作业的脚本解析
  12. TCP/IP笔记(五)IP协议相关技术
  13. 逆向实用干货分享,Hook技术第二讲,之虚表HOOK
  14. Python-数据类型之元组
  15. EF数据迁移
  16. 【php 之获得当前日期以及比较日期大小】
  17. PHP阿里云云解析签名, 通过API 绑定域名到动态 ip
  18. AD添加LOGO的方法
  19. HIVE表保存的路径
  20. virtualbox虚拟机与物理机windows文件共享

热门文章

  1. OJ生成器(一)制作Online Judge前的准备和策划
  2. git 常用命令及解析 由浅入深
  3. 向苹果App Store提交新应用的图文教程(转)
  4. header中Content-Disposition的作用
  5. 我的CSS布局之旅--持续更新
  6. ping命令脚本实现显示网络状态、学生姓名、学号
  7. 一个 div 实现扇形图(锥形渐变)
  8. 第1章 Sass简介
  9. Linux下安装GO语言环境
  10. 【Python】Django