DBCP类的方法
发布网友
发布时间:2022-04-07 23:43
我来回答
共1个回答
热心网友
时间:2022-04-08 01:12
Eclipse里面?
删除,再打点试试。
Properties dbProps=null;
//下面的读取配置文件可以根据实际的不同修改
dbProps = ConfigProperties.getInstance().getProperties("jdbc.properties");
try {
String driveClassName = dbProps.getProperty("jdbc.driverClassName");
String url = dbProps.getProperty("jdbc.url");
String username = dbProps.getProperty("jdbc.username");
String password = dbProps.getProperty("jdbc.password");
String initialSize = dbProps.getProperty("dataSource.initialSize");
String minIdle = dbProps.getProperty("dataSource.minIdle");
String maxIdle = dbProps.getProperty("dataSource.maxIdle");
String maxWait = dbProps.getProperty("dataSource.maxWait");
String maxActive = dbProps.getProperty("dataSource.maxActive");
//是否在自动回收超时连接的时候打印连接的超时错误
boolean logAbandoned = (Boolean.valueOf(dbProps.getProperty("dataSource.logAbandoned","false"))).booleanValue();
//是否自动回收超时连接
boolean removeAbandoned = (Boolean.valueOf(dbProps.getProperty("dataSource.removeAbandoned","false"))).booleanValue();
//超时时间(以秒数为单位)
int removeAbandonedTimeout = Integer.parseInt(dbProps.getProperty("dataSource.removeAbandonedTimeout","300"));
dataSource = new BasicDataSource();
dataSource.setDriverClassName(driveClassName);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
//初始化连接数
if(initialSize!=null)
dataSource.setInitialSize(Integer.parseInt(initialSize));
//最小空闲连接
if(minIdle!=null)
dataSource.setMinIdle(Integer.parseInt(minIdle));
//最大空闲连接
if(maxIdle!=null)
dataSource.setMaxIdle(Integer.parseInt(maxIdle));
//超时回收时间(以毫秒为单位)
if(maxWait!=null)
dataSource.setMaxWait(Long.parseLong(maxWait));
//最大连接数
if(maxActive!=null){
if(!maxActive.trim().equals("0"))
dataSource.setMaxActive(Integer.parseInt(maxActive));
}
System.out.println("logAbandoned="+logAbandoned);
dataSource.setLogAbandoned(logAbandoned);
dataSource.setRemoveAbandoned(removeAbandoned);
dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout);
Connection conn = dataSource.getConnection();
if(conn==null){
log("创建连接池时,没有取得连接!检查设置!!!");
}else{
conn.close();
}
System.out.println("连接池创建成功!!!");
}catch (Exception e) {
e.printStackTrace();
System.out.println("创建连接池失败!请检查设置!!!");
}