1. 程式人生 > >c3p0連線mysql

c3p0連線mysql

最近在做畢業設計,需要操作資料庫,因為已經厭倦了以前的jdbc啊db啊巴拉巴拉的,所以學了一下這玩意一下是一些經驗經供參考。

使用c3p0 JDBC連線池需要匯入倆jar包

1.c3p0-0.9.1.2.jar

2.commons-dbutils-1.7.jar

然後在src下建立c3p0-config.xml檔案,一下是xml檔案內容

-----------------------------------------------------------------分割線-------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>

    <!-- default-config 預設的配置, -->
    <default-config>
        <property name="driverClass">com.mysql.jdbc.Driver</property>

        <!--連線url 切記處理中文

 -->
        <property name="jdbcUrl">jdbc:mysql://localhost/testdb?useUnicode=true&amp;characterEncoding=utf-8</property>

        <!-- 資料庫連線使用者名稱-->
        <property name="user">username</property>

        <!-- 資料庫密碼-->
        <property name="password">password</property>


        <property name="initialPoolSize">10</property>
        <property name="maxIdleTime">30</property>
        <property name="maxPoolSize">100</property>
        <property name="minPoolSize">10</property>
        <property name="maxStatements">200</property>
    </default-config>

    <!-- This app is massive! -->
    <named-config name="mysql">
        <property name="acquireIncrement">50</property>
        <property name="initialPoolSize">100</property>
        <property name="minPoolSize">50</property>
        <property name="maxPoolSize">1000</property>

        <!-- intergalactoApp adopts a different approach to configuring statement 
            caching -->
        <property name="maxStatements">0</property>
        <property name="maxStatementsPerConnection">5</property>

        <!-- he's important, but there's only one of him -->
        <user-overrides user="master-of-the-universe">
            <property name="acquireIncrement">1</property>
            <property name="initialPoolSize">1</property>
            <property name="minPoolSize">1</property>
            <property name="maxPoolSize">5</property>
            <property name="maxStatementsPerConnection">50</property>
        </user-overrides>
    </named-config>


</c3p0-config>

然後再整一個C3P0Utils.java類,主要就是封裝一些需要用到的方法:

------------------------------------------分割線------------------------------------------------------------------------------------------------------

package util.c3p0;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.sun.crypto.provider.RSACipher;

/**
 * 
 * @author Chauncy
 *    建立C3P0Utils類,定義獲取getDataSource、定義獲取connection、釋放connection的方法
 */

public class C3p0Utils {
    // 這裡有個優點,寫好配置檔案,想換資料庫,簡單
    static ComboPooledDataSource dataSource = null;
    static{
        dataSource = new ComboPooledDataSource();
    }
    
    public static DataSource getDataSource(){
        return dataSource;
    }
    /**
     * 獲得資料庫連線
     * @return  Connection
     */
    public static Connection getConnection(){
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }
    
    public static void close(Connection connection,PreparedStatement pStatement,ResultSet resultSet){
        if(resultSet != null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(pStatement != null){
            try {
                pStatement.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
         if(connection != null){
             try {
                connection.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
         }
    }

    //測試連線
    public static void main(String[] args) {
        Connection connection  = getConnection();
        System.out.println(connection.getClass().getName());
        close(connection, null, null);
    }
}