DBCP是Apache的一個開源項目:
commons.dbcp.jar
http://jakarta.apache.org/commons/dbcp/index.html
DBCP依賴Apache的另外2個開源項目
commons.collections.jar和commons.pool.jar
下載這些包并將這些包的路徑添加到classpath中就可以使用dbcp做為項目中的數據庫連接池使用了。
以下是我的連接池
java 代碼
-
package
??selfservice;????
???
import
??java.io.FileNotFoundException;????
import
??java.io.IOException;????
import
??java.sql.Connection;????
import
??java.sql.DriverManager;????
import
??java.sql.ResultSet;????
import
??java.sql.SQLException;????
import
??java.sql.Statement;????
???
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
??PoolManager?
{??????
?????
private
???
static
??String?????
????????????driver
=
?
"
oracle.jdbc.driver.OracleDriver
"
?,?
//
驅動????
????????????url?
=
??
"
jdbc:oracle:thin:@192.168.0.40:1521:drcom
"
?,?
//
URL????
????????????Name
=
?
"
drcom
"
?,?
//
用戶名????
????????????Password
=
?
"
drcom
"
?;?
//
密碼????
????????
?????
private
???
static
??Class?driverClass?
=
??
null
?;????
?????
private
???
static
??ObjectPool?connectionPool?
=
??
null
?;????
???

?????
public
??PoolManager()
{???????????
????}
???????
????????

?????
/**?*/
/**
???
?????*?裝配配置文件???
?????*?initProperties???????
?????
*/
????

?????
private
???
static
???
void
??loadProperties()
{????

?????????
try
??
{????
????????????java.io.InputStream?stream?
=
??
new
??java.io.FileInputStream(?
"
config.properties
"
?);????
????????????java.util.Properties?props?
=
??
new
??java.util.Properties();????
????????????props.load(stream);????
????????????????
????????????driver?
=
?props.getProperty(?
"
ORACLE_DRIVER
"
?);????
????????????url?
=
?props.getProperty(?
"
ORACLE_URL
"
?);????
????????????Name?
=
?props.getProperty(?
"
ORACLE_LOGIN_NAME
"
?);????
????????????Password?
=
?props.getProperty(?
"
ORACLE_LOGIN_PASSWORD
"
?);????
????????????????

????????}
??
catch
??(FileNotFoundException?e)?
{????
????????????System.out.println(?
"
讀取配置文件異常
"
?);?????????????

????????}
??
catch
?(IOException?ie)
{????
????????????System.out.println(?
"
讀取配置文件時IO異常
"
?);????
????????}
????
????}
????
????????

?????
/**?*/
/**
????
?????*?初始化數據源????
?????
*/
??????

?????
private
???
static
???
synchronized
???
void
??initDataSource()?
{?????????????

?????????
if
??(driverClass?
==
??
null
?)?
{?????

?????????????
try
??
{?????
????????????????driverClass?
=
?Class.forName(driver);?????

????????????}
??
catch
??(ClassNotFoundException?e)?
{?????
????????????????e.printStackTrace();????
????????????}
?????
????????}
?????
????}
?????
?????

?????
/**?*/
/**
????
?????*?連接池啟動????
?????*?
@throws
?Exception????
?????
*/
??????

?????
public
???
static
???
void
??StartPool()?
{????
????????loadProperties();????
????????initDataSource();?????

?????????
if
??(connectionPool?
!=
??
null
?)?
{?????
????????????ShutdownPool();?????
????????}
??????

?????????
try
??
{?????
????????????connectionPool?
=
??
new
??GenericObjectPool(?
null
?);?????
????????????ConnectionFactory?connectionFactory?
=
??
new
??DriverManagerConnectionFactory(url,?Name,?Password);?????
????????????PoolableConnectionFactory?poolableConnectionFactory?
=
??
new
??PoolableConnectionFactory(connectionFactory,?connectionPool,??
null
?,??
null
?,??
false
?,??
true
?);?????
????????????Class.forName(?
"
org.apache.commons.dbcp.PoolingDriver
"
?);?????
????????????PoolingDriver?driver?
=
?(PoolingDriver)?DriverManager.getDriver(?
"
jdbc:apache:commons:dbcp:
"
?);?????
????????????driver.registerPool(?
"
dbpool
"
?,?connectionPool);??????????????????
????????????System.out.println(?
"
裝配連接池OK
"
?);?????

????????}
??
catch
??(Exception?e)?
{?????
????????????e.printStackTrace();????
????????}
?????
????}
?????
?????

?????
/**?*/
/**
????
?????*?釋放連接池????
?????
*/
??????

?????
public
???
static
???
void
??ShutdownPool()?
{?????

?????????
try
??
{?????
????????????PoolingDriver?driver?
=
?(PoolingDriver)?DriverManager.getDriver(?
"
jdbc:apache:commons:dbcp:
"
?);?????
????????????driver.closePool(?
"
dbpool
"
?);????

????????}
??
catch
??(SQLException?e)?
{?????
????????????e.printStackTrace();????
????????}
?????
????}
?????????
?????

?????
/**?*/
/**
????
?????*?取得連接池中的連接????
?????*?
@return
????
?????
*/
??????

?????
public
???
static
??Connection?getConnection()?
{?????
????????Connection?conn?
=
??
null
?;?????
?????????
if
?(connectionPool?
==
??
null
?)?????
????????????StartPool();?????

?????????
try
??
{?????
????????????conn?
=
?DriverManager.getConnection(?
"
jdbc:apache:commons:dbcp:dbpool
"
?);?????

????????}
??
catch
??(SQLException?e)?
{?????
????????????e.printStackTrace();????
????????}
?????
?????????
return
??conn;?????
????}
?????
????????

?????
/**?*/
/**
???
?????*?獲取連接???
?????*?getConnection???
?????*?
@param
?name???
?????*?
@return
???
?????
*/
????

?????
public
???
static
??Connection?getConnection(String?name)
{????
?????????
return
??getConnection();????
????}
????

?????
/**?*/
/**
???
?????*?釋放連接???
?????*?freeConnection???
?????*?
@param
?conn???
?????
*/
????

?????
public
???
static
???
void
??freeConnection(Connection?conn)
{????

?????????
if
?(conn?
!=
??
null
?)
{????

?????????????
try
??
{????
????????????????conn.close();????

????????????}
??
catch
??(SQLException?e)?
{??????????????????
????????????????e.printStackTrace();????
????????????}
????
????????}
????
????}
????

?????
/**?*/
/**
???
?????*?釋放連接???
?????*?freeConnection???
?????*?
@param
?name???
?????*?
@param
?con???
?????
*/
????

?????
public
???
static
???
void
??freeConnection?(String?name,Connection?con)
{????
????????freeConnection(con);????
????}
????
????????

?????
/**?*/
/**
???
?????*?例子???
?????*?main???
?????*?
@param
?args???
?????
*/
????

?????
public
???
static
???
void
??main(String[]?args)?
{????????????

?????????
try
??
{????
????????????Connection?conn?
=
?PoolManager.getConnection();????

?????????????
if
?(conn?
!=
??
null
?)
{????
????????????????Statement?statement?
=
?conn.createStatement();????
????????????????ResultSet?rs?
=
?statement.executeQuery(?
"
select?*?from?tblgxinterface
"
?);????
?????????????????
int
??c?
=
?rs.getMetaData().getColumnCount();????

?????????????????
while
?(rs.next())
{???????????????????????
????????????????????System.out.println();????

?????????????????????
for
?(?
int
??i
=
?
1
?;i
<=
c;i
++
)
{????
????????????????????????System.out.print(rs.getObject(i));????
????????????????????}
????
????????????????}
????
????????????????rs.close();????
????????????}
????
????????????PoolManager.freeConnection(conn);????

????????}
??
catch
??(SQLException?e)?
{??????????????
????????????e.printStackTrace();????
????????}
????
???
????}
????
???
}
???
?