public class DBUtil {
private static DataSource ds = null;
static {
try{
InputStream in = DBUtil.class.getClassLoader()
.getResourceAsStream("ds.properties");
Properties props = new Properties();
props.load(in);
ds = BasicDataSourceFactory.createDataSource(props);
}catch(Exception ex){
ex.printStackTrace();
}
}
public static Connection getConnection() throws SQLException{
return ds.getConnection();
}
}
DBCP配置参考
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbc
username=root
password=123456
initialSize=10
maxActive=50
maxIdle=20
minIdle=5
maxWait=60000
connectionProperties=useUnicode=true;characterEncoding=utf-8
defaultAutoCommit=true
defaultReadOnly=
defaultTransactionIsolation=READ_UNCOMMITTED