2020-08-17C3P0數(shù)據(jù)庫連接池與druid數(shù)據(jù)庫連接池

C3P0下載地址
https://mvnrepository.com/artifact/com.mchange/c3p0

C3P0數(shù)據(jù)庫連接池

  • 步驟
    ①導(dǎo)入jar包c3p0和mchange-commons-java不要忘記導(dǎo)入數(shù)據(jù)庫驅(qū)動jar包
    ②定義配置文件c3p0.properties或者c3p0-config.xml,路徑為src目錄下
    ③創(chuàng)建核心對象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?&amp;useSSL=false&amp;serverTimezone=GMT</property>
        <property name="user">root</property>
        <property name="password">545267257zzt</property>
<!--        連接池參數(shù)-->
<!--        初始化申請的連接數(shù)-->
        <property name="initialPoolSize">10</property>
<!--        創(chuàng)建過的鏈接并使用后閑置下來的閑置時間,如果超過這個時間就會自動斷開這個連接-->
        <property name="maxIdleTime">30</property>
<!--        最大連接數(shù)-->
        <property name="maxPoolSize">20</property>
<!--        最小連接數(shù)-->
        <property name="minPoolSize">5</property>
<!--        最多可以創(chuàng)建Statements對象的個數(shù). . 就是可以執(zhí)行baiSQL語句的對象的個數(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?&amp;useSSL=false&amp;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ù)庫
        //DataSource dataSource1 = new ComboPooledDataSource("mysql");
        //2.獲取連接
        Connection connection = dataSource.getConnection();
        //sql操作
        //釋放連接
        connection.close();
    }
}

Druid數(shù)據(jù)庫連接池

  • 步驟
  1. 導(dǎo)入druid-x.x.x.jar包
  2. 定義配置文件,是properties形式,可以叫任意名稱,可以放置在任意路徑下,手動加載
  3. 加載配置文件
    ClassLoader classLoader = druid.class.getClassLoader();
    URL resource = classLoader.getResource("druid.properties");
    String string = resource.getPath();
    properties.load(new FileReader(string));
  4. 獲取數(shù)據(jù)源連接池對象:通過工廠類來獲取DruidDataSourceFactory
  5. 獲取連接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
#最大等待時間
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的工具類

  1. 定義一個類JDBCUtils
  2. 提供靜態(tài)代碼塊加載配置文件,初始化連接池對象
  3. 提供方法
    ①獲取連接的方法:通過數(shù)據(jù)庫連接池獲取連接
    ②釋放資源
    ③獲取連接池的方法
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();
        }
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容