1.依赖
2.配置
3.编程
1.依赖包
dbcp:
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
编程运行时可能会报错
java.lang.ClassNotFoundException: org.apache.juli.logging.LogFactory
只需要导入这个包:
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-juli -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-juli</artifactId>
<version>8.5.0</version>
</dependency>
c3p0:
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mchange/mchange-commons-java -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>mchange-commons-java</artifactId>
<version>0.2.3</version>
</dependency>
2.配置
都在项目的根目录中
dbcp:
dbcp.properties
########DBCP配置文件##########
#驱动名
driverClassName=com.mysql.jdbc.Driver
#url
url=jdbc:mysql://127.0.0.1:3306/mydb
#用户名
username=sa
#密码
password=123456
#初试连接数
initialSize=30
#最大活跃数
maxTotal=30
#最大idle数
maxIdle=10
#最小idle数
minIdle=5
#最长等待时间(毫秒)
maxWaitMillis=1000
#程序中的连接不使用后是否被连接池回收(该版本要使用removeAbandonedOnMaintenance和removeAbandonedOnBorrow)
#removeAbandoned=true
removeAbandonedOnMaintenance=true
removeAbandonedOnBorrow=true
#连接在所指定的秒数内未使用才会被删除(秒)(为配合测试程序才配置为1秒)
removeAbandonedTimeout=1
c3p0:
DriverClass = com.mysql.jdbc.Driver
JdbcUrl = jdbc:mysql://localhost:3306/cloudhospital
User = root
Password = admin
MaxPoolSize = 20
MinPoolSize = 2
InitialPoolSize = 5
MaxStatements = 30
MaxIdleTime = 100
3.编程
dbcp:
创建一个DbcpUtils.java
package com.imooc.utils;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory;
public class DbcpUtils {
private static DataSource DS;
private static final String configFile = "/dbcp.properties";
public DbcpUtils(){
initialDbcp();
}
/**
* 初始化
*/
private static void initialDbcp() {
Properties props = new Properties();
try {
props.load(Object.class.getResourceAsStream(configFile));
//使用DataSourceFactory创建一个DataSource
DS = BasicDataSourceFactory.createDataSource(props);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 从数据源获取一个连接
* @return
*/
public Connection getConn(){
Connection con = null;
if(DS != null){
try {
con= DS.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con.setAutoCommit(false);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
return con;
}
}
c3p0:
创建一个C3p0Utils.java
package com.imooc.utils;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3p0Utils {
private static ComboPooledDataSource ds=new ComboPooledDataSource();
private static final String configFile = "/c3p0.properties";
/**
* 初始化参数
*/
static{
Properties props = new Properties();
try {
props.load(C3p0Utils.class.getResourceAsStream(configFile));
ds.setDriverClass(props.getProperty("DriverClass"));
ds.setJdbcUrl(props.getProperty("JdbcUrl"));
ds.setUser(props.getProperty("User"));
ds.setPassword(props.getProperty("Password"));
ds.setInitialPoolSize(Integer.parseInt(props.getProperty("InitialPoolSize")));
ds.setMinPoolSize(Integer.parseInt(props.getProperty("MinPoolSize")));
ds.setMaxPoolSize(Integer.parseInt(props.getProperty("MaxPoolSize")));
ds.setMaxStatements(Integer.parseInt(props.getProperty("MaxStatements")));
ds.setMaxIdleTime(Integer.parseInt(props.getProperty("MaxIdleTime")));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection(){
try {
return ds.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}