时间:2021-07-01 10:21:17 帮助过:5人阅读
方式2:通过代码配置
BasicDataSource bds = new BasicDataSource(); bds.setUrl(url); bds.setDriverClassName(driverClassName); bds.setUsername(username); bds.setPassword(password); bds.setInitialSize(initialSize); bds.setMaxActive(maxActive); bds.setMinIdle(minIdle); bds.setMaxIdle(maxIdle); bds.setMaxWait(maxWait);
3. 获取连接
下面是获取连接的类DBCPUtil
private static DataSource ds; private static final String configFile = "/dbcp.properties"; public DBCPUtil() { initDBCP(); } private void initDBCP() { Properties prop = new Properties(); try { prop.load(Object.class.getResourceAsStream(configFile)); ds = BasicDataSourceFactory.createDataSource(prop); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } public Connection getConn() { Connection conn = null; if(ds != null) { try { conn = ds.getConnection(); conn.setAutoCommit(false); } catch (SQLException e) { e.printStackTrace(); } } return conn; } public static void main(String[] args) { DBCPUtil db = new DBCPUtil(); System.out.println(db.getConn()); } }
JDBC数据库连接池之dbcp
标签: