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);
}
}
}