一个简单适用的DBCP连接池类

 

package com.dbcp;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.PoolingDriver;
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool;

public class DBConnectionManager {

 /** */
 /**
  * 数据库连接池
  *
  * @see http://jakarta.apache.org/commons/dbcp/index.html
  */
 private static PoolingDriver driver = null;
 
 private final static String POOL_NAME = "pool";
 
 private final static String DBCP_DRIVER = "jdbc:apache:commons:dbcp:";

 /** */
 /**
  * 设置一个数据库连接池
  *
  * @param name
  *            连接池的名称
  * @param url
  *            数据源
  * @throws SQLException
  */
 private static void setUpDriverPool(String url,String user,String password)
   throws SQLException {
  if ((driver == null)/* || driver.getPoolNames().length < 2*/) {
   try {
    /**
     * 首先创建一个对象池来保存数据库连接
     * 使用 commons.pool 的 GenericObjectPool对象
     */
    ObjectPool connectionPool = new GenericObjectPool();//默认值:this(null, 8, (byte)1, -1L, 8, 0, false, false, -1L, 3, 0x1b7740L, false);

           
    /**
     * 创建一个 DriverManagerConnectionFactory对象 连接池将用它来获取一个连接
     */
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
      url, user, password);
      //url, props);       
    
    /**
     * 创建一个PoolableConnectionFactory 对象。
     */
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
      connectionFactory, connectionPool, null, null, false,
      true);

    /**
     * 注册PoolingDriver。
     */
    Class.forName("org.apache.commons.dbcp.PoolingDriver");

    driver = (PoolingDriver) DriverManager
      .getDriver(DBCP_DRIVER);

    driver.registerPool(POOL_NAME, connectionPool);

   } catch (ClassNotFoundException e) {
    throw new RuntimeException(e);
   }
  }
 }

 /** */
 /**
  * 关闭所有数据库连接池
  *
  */
 public static void shutDownDriver() {
  try {   
   driver.getPool(POOL_NAME).close();
   
  } catch (SQLException sqle) {
   throw new RuntimeException(sqle);
  } catch (Exception e) {
   throw new RuntimeException(e);
  }
 }

 /** */
 /**
  * 取得一个数据库连接对象。
  *
  *
  * @param report
  * @return
  */
 public static Connection getConnection() {
  Connection con = null;
  try {
   //读配置
   String driver = "oracle.jdbc.driver.OracleDriver";
   String url = "jdbc:oracle:oci:@192.168.0.86:1522:orcl";
   String user = "user";
   String password = "pw";
   
   Class.forName(driver);
   setUpDriverPool(url,user,password);
   con = DriverManager.getConnection(DBCP_DRIVER
     + POOL_NAME);
   return con;
  } catch (ClassNotFoundException cnfe) {
   throw new RuntimeException("无法装入数据库引擎");
  } catch (SQLException sqle) {
   throw new RuntimeException("无法打开数据库连接");
  }
 }
  /** */
 /**
  * 执行清理过程
  *
  * <li>关闭数据库连接</li>
  * <li>关闭语句对象</li>
  * <li>关闭结果集</li>
  *
  * @param con
  * @param s
  * @param rs
  */
 public static void closeAll(Connection con, Statement s, ResultSet rs) {
  try {
   if (rs != null) {
    rs.close();
    rs = null;
   }

   if (s != null) {
    s.close();
    s = null;
   }

   if (con != null) {
    con.close();
    con = null;
   }
  } catch (SQLException sqle) {
   // nothing to do, forget it;
  }
 }
 
 /**
  * @see com.creawor.meip.sms.server.Daemon#stop()
  */
 public void onTerminal() {
  try {
   shutDownDriver();
  } catch (Exception e) {
  }
 }
 
 
 public static void main(String[] args){
  Connection conn = null;
  Statement st = null;
  ResultSet rs = null;
  
  try{
   String sql = "select * from appsystem";
   conn = getConnection();
   st = conn.createStatement();
   rs = st.executeQuery(sql);
   int numcols = rs.getMetaData().getColumnCount();
   while (rs.next()) {
    for (int i = 1; i <= numcols; i++) {
     System.out.print(" " + rs.getString(i));
    }
    System.out.println("");
   }
  }catch(Exception e){
   e.printStackTrace();
  }finally{
   closeAll(conn,st,rs);
  }
 }

}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值