[Jakarta Commons] 簡單使用DCBP连接池

[Jakarta Commons] 簡單使用DCBP连接池

import  java.sql.Connection;
import  java.sql.SQLException;

import  org.apache.commons.dbcp.BasicDataSource;

/** */ /**
 * 
@author  zJun
 * 
 
*/

public   class  dbcpDemo  implements  ConnFactory  {

    
static  BasicDataSource dataSource  =   null ;

    
static   {
        
try   {
            Class.forName(
" org.gjt.mm.mysql.Driver " );
            dataSource 
=  setupDataSource( " org.gjt.mm.mysql.Driver " " root " ,
                    
" 111111 " " jdbc:mysql://127.0.0.1:3306/test " );
        }
  catch  (Exception e)  {
            System.out.println(
" DBConnect.java => Unable to load driver. "
                    
+  e.getMessage());
        }

    }


    
/** */ /**
     * 初始化数据源
     * 
     * 
@param  sDrvName
     * 
@param  sUserName
     * 
@param  sPwd
     * 
@param  connectURI
     * 
@return
     
*/

    
private   static  BasicDataSource setupDataSource(String sDrvName,
            String sUserName, String sPwd, String connectURI) 
{
        BasicDataSource ds 
=   new  BasicDataSource();
        ds.setDriverClassName(sDrvName);
        ds.setUsername(sUserName);
        ds.setPassword(sPwd);
        ds.setUrl(connectURI);
        ds.setMaxActive(
50 );
        ds.setMaxIdle(
10 );
        ds.setMaxWait(
10000 );  //  10 seconds
         return  ds;
    }


    
/** */ /**
     * 取得数据库连接
     * 
     * 
@return
     * 
@throws  SQLException
     * 
@throws  ClassNotFoundException
     
*/

    
public  Connection getConnection()  throws  SQLException,
            ClassNotFoundException 
{
        System.out.println(
" NumActive:  "   +  dataSource.getNumActive()  +   " , "
                
+   " NumIdle:  "   +  dataSource.getNumIdle());
        
return  dataSource.getConnection();
    }


}

你可能感兴趣的:([Jakarta Commons] 簡單使用DCBP连接池)