使用了Oracle连接池包
import java.util.*;
import java.sql.*;
import java.io.*;
import oracle.jdbc.pool.*;
public class DBPoolConnection {
private String url = new String();
private String user = new String();
private String password = new String();
private int imin = 0;
private int imax = 0;
private OracleConnectionPoolDataSource ocpds;
private Connection conn = null;
private OracleConnectionCacheImpl dbpool;
//使用 private static final 属性只产生一个连接池
private static final DBPoolConnection dbpoolconn = new DBPoolConnection();
//使用private属性的构造函数保证构造函数不被调用
private DBPoolConnection() {
if (dbpool == null) {
getConnectionInfo();
setOraConnPoolDataSource();
setOraConnCacheImpl(ocpds);
setConn(dbpool);
}
}
public static DBPoolConnection getdbpool() {
return dbpoolconn;
}
private boolean getConnectionInfo() {
Properties prop = new Properties();
try {
InputStream is = getClass().getResourceAsStream(
"/database-conf.properties");
prop.load(is);
if (is != null) {
is.close();
}
}
catch (Exception ex) {
System.out.println(ex.toString());
}
url = prop.getProperty("url");
user = prop.getProperty("user");
password = prop.getProperty("password");
imin = Integer.parseInt(prop.getProperty("min"));
imax = Integer.parseInt(prop.getProperty("max"));
return true;
}
private OracleConnectionPoolDataSource setOraConnPoolDataSource() {
Properties prop = new Properties();
try {
ocpds = new OracleConnectionPoolDataSource();
ocpds.setURL(url);
ocpds.setUser(user);
ocpds.setPassword(password);
}
catch (Exception ex) {
}
return ocpds;
}
private OracleConnectionCacheImpl setOraConnCacheImpl(
OracleConnectionPoolDataSource ds) {
try {
dbpool = new OracleConnectionCacheImpl(ds);
dbpool.setCacheScheme(OracleConnectionCacheImpl.FIXED_WAIT_SCHEME);
dbpool.setMinLimit(imin);
dbpool.setMaxLimit(imax);
}
catch (Exception ex) {
}
return dbpool;
}
private void setConn(OracleConnectionCacheImpl oraconnImpl) {
try {
conn = oraconnImpl.getConnection();
}
catch (Exception ex) {
}
}
public Connection getConn() {
return conn;
}
}
该博客展示了使用Java实现Oracle连接池的代码。通过导入相关包,定义连接信息和连接池属性,利用构造函数初始化连接池,实现了获取数据库连接的功能。代码中涉及属性读取、异常处理等操作。
1319

被折叠的 条评论
为什么被折叠?



