java 数据库连接池

/**
* 说明:数据库连接池
* @author see_you_again
* @date 2015.8.17
*
*/
public class DBManager {

private BasicDataSource ds = null;
private XmlDbImp xmldb = null ;

/**
* 说明:构造函数
* @param xmldb
*/
public DBManager( XmlDbImp xmldb ) {
this.xmldb = xmldb ;

try {
DataSource datasource = this.xmldb.getDatasource() ;
ds = new BasicDataSource();
ds.setInitialSize(Integer.parseInt(xmldb.getParamMap().get("initialSize"))) ;
ds.setMaxIdle(Integer.parseInt(xmldb.getParamMap().get("maxIdle"))) ;//最大空闲连接
ds.setMaxActive(Integer.parseInt(xmldb.getParamMap().get("maxActive"))) ;//最大连接数量
ds.setMaxWait(Integer.parseInt(xmldb.getParamMap().get("maxWait"))) ;//时等待时间以毫秒为单位 1000等于60秒
ds.setPassword(datasource.getPassword());
ds.setUrl(datasource.getURL());
ds.setUsername(datasource.getUser());
ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");

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

}

/**
* 获取链接,用完后记得关闭
*
* @see {@link DBManager#closeConn(Connection)}
* @return
*/
public Connection getConn() {
Connection conn = null;
try {
conn = ds.getConnection();
} catch (SQLException e) {
}
return conn;
}

/**
* 关闭连接
*
* @param conn
* 需要关闭的连接
*/
public void closeConn(Connection conn) {
try {
if (conn != null && !conn.isClosed()) {
conn.setAutoCommit(true);
conn.close();
}
} catch (SQLException e) {
}
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值