C3P0下載地址
https://mvnrepository.com/artifact/com.mchange/c3p0
C3P0數(shù)據(jù)庫(kù)連接池
- 步驟
①導(dǎo)入jar包c(diǎn)3p0和mchange-commons-java不要忘記導(dǎo)入數(shù)據(jù)庫(kù)驅(qū)動(dòng)jar包
②定義配置文件c3p0.properties或者c3p0-config.xml,路徑為src目錄下
③創(chuàng)建核心對(duì)象CombopooledDataSource对人; DataSource dataSource = new ComboPooledDataSource();
④獲取連接getDataSource - 配置文件c3p0-config.xml
<?xml version="1.0" encoding="utf-8"?>
<c3p0-config>
<default-config>
<!-- 連接參數(shù)-->
<property name="driverClass">com.mysql.cj.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/db4?&useSSL=false&serverTimezone=GMT</property>
<property name="user">root</property>
<property name="password">545267257zzt</property>
<!-- 連接池參數(shù)-->
<!-- 初始化申請(qǐng)的連接數(shù)-->
<property name="initialPoolSize">10</property>
<!-- 創(chuàng)建過的鏈接并使用后閑置下來的閑置時(shí)間,如果超過這個(gè)時(shí)間就會(huì)自動(dòng)斷開這個(gè)連接-->
<property name="maxIdleTime">30</property>
<!-- 最大連接數(shù)-->
<property name="maxPoolSize">20</property>
<!-- 最小連接數(shù)-->
<property name="minPoolSize">5</property>
<!-- 最多可以創(chuàng)建Statements對(duì)象的個(gè)數(shù). . 就是可以執(zhí)行baiSQL語(yǔ)句的對(duì)象的個(gè)數(shù)-->
<property name="maxStatements">200</property>
</default-config>
<named-config name="mysql">
<property name="driverClass">com.mysql.cj.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/db4?&useSSL=false&serverTimezone=GMT</property>
<property name="user">root</property>
<property name="password">545267257zzt</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">20</property>
<property name="minPoolSize">5</property>
<property name="maxStatements">200</property>
</named-config>
</c3p0-config>
import com.mchange.v2.c3p0.ComboPooledDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
public class c3p0 {
public static void main(String[] args) throws SQLException {
//1.獲取DataSource,使用默認(rèn)配置
DataSource dataSource = new ComboPooledDataSource();
//使用配置文件中指定的配置信息谣殊,連接不同的數(shù)據(jù)庫(kù)
//DataSource dataSource1 = new ComboPooledDataSource("mysql");
//2.獲取連接
Connection connection = dataSource.getConnection();
//sql操作
//釋放連接
connection.close();
}
}
Druid數(shù)據(jù)庫(kù)連接池
- 步驟
- 導(dǎo)入druid-x.x.x.jar包
- 定義配置文件,是properties形式牺弄,可以叫任意名稱姻几,可以放置在任意路徑下,手動(dòng)加載
- 加載配置文件
ClassLoader classLoader = druid.class.getClassLoader();
URL resource = classLoader.getResource("druid.properties");
String string = resource.getPath();
properties.load(new FileReader(string)); - 獲取數(shù)據(jù)源連接池對(duì)象:通過工廠類來獲取DruidDataSourceFactory
- 獲取連接getConnection势告;
- 配置文件druid.properties
driverClassname=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/db4?useSSL=false&serverTimezone=UTC
username=root
password=545267257zzt
#初始化連接數(shù)
initialSize=5
#最大連接數(shù)
maxActive=10
#最大等待時(shí)間
maxWait=3000
import javax.sql.DataSource;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.util.Properties;
public class druid {
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
ClassLoader classLoader = druid.class.getClassLoader();
URL resource = classLoader.getResource("druid.properties");
String string = resource.getPath();
properties.load(new FileReader(string));
DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
Connection connection = dataSource.getConnection();
System.out.println(connection);
}
}
定義druid的工具類
- 定義一個(gè)類JDBCUtils
- 提供靜態(tài)代碼塊加載配置文件蛇捌,初始化連接池對(duì)象
- 提供方法
①獲取連接的方法:通過數(shù)據(jù)庫(kù)連接池獲取連接
②釋放資源
③獲取連接池的方法
package it.heima.util;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class jdbcUtil {
// 1. 定義成員變量DataSource
private static DataSource ds;
static {
Properties properties = new Properties();
ClassLoader classLoader = jdbcUtil.class.getClassLoader();
URL resource = classLoader.getResource("druid.properties");
String path = resource.getPath();
try {
properties.load(new FileReader(path));
ds= DruidDataSourceFactory.createDataSource(properties);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/*
*獲取連接的方法
*/
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
/*
*釋放資源
*/
public static void close(Statement statement,Connection connection){
if (statement != null) {
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public static void close(Statement statement, Connection connection, ResultSet resultSet){
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
/*
獲取連接池的方法
*/
public static DataSource getDataSource(){
return ds;
}
}
public class jdbcUtilTest {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = jdbcUtil.getConnection();
String sql = "insert into student (id,name,cid) values (3, ?, 2)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,"zhangzetao");
preparedStatement.executeUpdate();
jdbcUtil.close(preparedStatement,connection);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}