导入配置
<!--druid依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.20</version>
</dependency>
<!--c3p0依赖-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!--c3p0依赖的依赖-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>mchange-commons-java</artifactId>
<version>0.2.11</version>
</dependency>
配置文件要求及配置
c3p0的配置文件必需命名为c3p0.properties或c3p0-config.xml,druid则没有命名要求,相应的c3p0放在resource下即可自动加载,而druid需手动加载。两者皆放在resource包下即可。
配置c3p0-config.xml
<c3p0-config>
<!--默认配置-->
<default-config>
<!--驱动-->
<property name = "driverClass">com.mysql.cj.jdbc.Driver</property>
<!--url-->
<property name = "jdbcUrl">jdbc:mysql://localhost:3306/test</property>
<!--账号-->
<property name = "user">root</property>
<!--密码-->
<property name = "password">root</property>
<!--初始容量-->
<property name = "initialPoolSize">5</property>
<!--最大容量-->
<property name = "maxPoolSize">10</property>
<!--超时时间-->
<property name = "checkoutTimeout">3000</property>
</default-config>
<!--指定配置-->
<named-config name = "myc3p0config">
<property name = "driverClass">com.mysql.cj.jdbc.Driver</property>
<property name = "jdbcUrl">jdbc:mysql://localhost:3306/test</property>
<property name = "user">root</property>
<property name = "password">root</property>
<property name = "initialPoolSize">1</property>
<property name = "maxPoolSize">5</property>
<property name = "checkoutTimeout">3000</property>
</named-config>
</c3p0-config>
druid配置
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://loacalhost:3306/test
username=root
password=root
initialSize=5
maxActive=10
maxWait=3000
代码实现
c3p0实现
public class Main {
public static void main(String[] args) {
//创建默认配置连接池
ComboPooledDataSource comboPooledDataSource1 = new ComboPooledDataSource();
//创建指定配置连接池
ComboPooledDataSource comboPooledDataSource2 = new ComboPooledDataSource("my-c3p0-config");
//获取连接
Connection connection1 = null;
Connection connection2 = null;
try {
connection1 = comboPooledDataSource1.getConnection();
connection2 = comboPooledDataSource2.getConnection();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//返还给数据库连接池
if (connection1 != null) {
try {
connection1.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection2 != null) {
try {
connection2.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
druid实现
public class Main {
public static void main(String[] args) {
Connection connection = null;
try {
//导入配置文件
Properties properties = new Properties();
//手动加载配置文件
InputStream is = Main.class.getClassLoader().getResourceAsStream("druid.properties");
properties.load(is);
//创建指定配置数据库连接池
DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
//获取连接
connection = dataSource.getConnection();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
//返还给数据库连接池
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}