1. 程式人生 > 其它 >[藍橋杯] 第39級臺階 python解法

[藍橋杯] 第39級臺階 python解法

技術標籤:菜雞從頭開始javamysqlsql連線池

資料庫連線池

當前市面上有很多資料庫連線池,如c3p0,druid,光連線池等,下面對於前兩種連線池進行簡單使用

一:老牌連線池C3p0

1、使用步驟:

1、匯入jar包:c3p0-0.9.5.2jar以及mchange-commons-java-0.2.12jar(版本自動忽略)
在這裡插入圖片描述

2、資料資料庫連線池,這邊有兩種方式,一種是使用程式碼set直接將需要的驅動,url,使用者名稱等資訊設定好,另一種方式是使用配置檔案,使用配置檔案也有兩種,但這裡我們主要使用xml配置檔案。具體步驟見下面的程式碼和執行步驟。
3、獲取資料庫連線

2、具體實現步驟

(1)、匯入jar包

這邊就不詳細解釋匯入jar包的流程,可自行尋找jar包以及匯入jar包的流程。

(2)、建立連線池物件並使用程式碼設定相應配置
 /*
        使用set方法來直接設定連線池資訊
     */

    @Test
    public void testC3p0() throws Exception{
        //建立連線池
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
        //設定資料庫驅動
        comboPooledDataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
        //設定資料庫連線url
        comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/demo01?serverTimezone = GMT%2B8");
        //密碼和使用者名稱
        comboPooledDataSource.setUser("root");
        comboPooledDataSource.setPassword("root");
        //獲取連線
        Connection connection = comboPooledDataSource.getConnection();

		//執行sql語句
        String sql = "select * from user where id = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1,1);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()){
            System.out.println(resultSet.getString("name"));
        }
    }

(3)、建立連線池物件並使用xml配置檔案設定相關配置

使用配置檔案建立連線池物件及執行程式碼:

/*
        使用xml配置檔案來設定連線池資訊
            注意配置檔案的存放
     */

    @Test
    public void testC3p0Xml() throws Exception{
        //建立連線池
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
        //獲取連線
        Connection connection = comboPooledDataSource.getConnection();
        
        //執行sql語句
        String sql = "select * from user where id = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1,1);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()){
            System.out.println(resultSet.getString("name"));
        }
    }

xml配置檔案:

<c3p0-config>
  <!-- 使用預設的配置讀取連線池物件 -->
  <default-config>
  	<!--  連線引數 -->
    <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql://localhost:3306/demo01?serverTimezone = GMT%2B8</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="otherc3p0"> 
    <!--  連線引數 -->
    <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql://localhost:3306/demo01?serverTimezone = GMT%2B8</property>
    <property name="user">root</property>
    <property name="password">root</property>
    
    <!-- 連線池引數 -->
    <property name="initialPoolSize">5</property>
    <property name="maxPoolSize">8</property>
    <property name="checkoutTimeout">1000</property>
  </named-config>
</c3p0-config>

通過上面xml檔案可以得到:
1、c3p0的xml配置檔名稱是:c3p0-config,注意這邊xml配置檔案的名稱不能改變!不能改變!不能改變!!
2、當代碼中建立資料庫連線池時如果沒有指定引數,則使用預設的配置來進行資料庫連線即如下:

 //建立連線池
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();

在建立資料庫連線時並沒有指定引數,那麼使用xml配置檔案中的

 <default-config>

下的配置資訊進行配置,當指定引數時:

ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource("otherc3p0"); 

則使用配置檔案中othere3p0下的配置資訊
3、xml配置檔案在工程中的位置要存放在src目錄下
在這裡插入圖片描述

二:druid(德魯伊)連線池

druid連線池是由阿里巴巴提供的一種資料庫連線池,該資料庫連線池的效能還是很強悍。

1、使用步驟:

1、匯入jar包:druid-XX.jar(自行尋找jar包以及匯入工程步驟)
2、將配置檔案放入工程中,相對於c3p0,druid連線池的配置檔案可以放在工程中的任意目錄中,並且可以取任意名字。.
3.建立資料庫連線池物件,通過工廠來獲取物件DruidDataSourceFactory();
4.獲取連線getConnection();

2、具體實現步驟

1、將配置檔案放入到工程中

driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/demo01?serverTimezone = GMT%2B8
username=root
password=root
initialSize=5
maxActive=10
maxWait=3000

druid的配置檔案時properties檔案型別的,在配置檔案中同樣配置相應的url地址,驅動,使用者名稱,使用者密碼,以及初始化連線的數量,最大等待時間。

2、程式碼中使用

  @Test
    public void druidTest() throws Exception {
        //反射載入配置檔案
        Properties properties = new Properties();
        properties.load(this.getClass().getClassLoader().getResourceAsStream("source/druid.properties"));
        //通過工程建立資料庫連線物件
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);

        //執行sql語句
        Connection connection = dataSource.getConnection();
        String sql = "select * from user where id = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1,1);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()){
            System.out.println(resultSet.getString("name"));
        }

    }

首先是載入properties型別的檔案,這裡使用的是反射進行載入。
然後建立druid連線池物件
之後就可以直接獲取資料庫連線了。

3、使用druid建立一個JDBCUtils

/**
 * 使用druid來寫一個數據庫連線池
 */
public class JDBCUtils {
    private static DataSource dataSource;

    static {
        try {
            //載入配置檔案
            Properties properties = new Properties();
            properties.load(JDBCUtils.class.getClassLoader().getResourceAsStream("source/druid.properties"));
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //獲取連線
    public static Connection getConnect(){
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    //關閉連線
    public static void close(Connection connection, Statement statement, ResultSet resultSet){
        if (resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}