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);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_37707251/article/details/82905318