c3p0数据库连接池+mysql数据库基本使用方法

       一般我们在项目中操作数据库时,都是每次需要操作数据库就建立一个连接,操作完成后释放连接。因为jdbc没有保持连接的能力,一旦超过一定时间没有使用(大约几百毫秒),

连接就会被自动释放掉。而每次新建连接都需要140毫秒左右的时间,所以耗费时间比较多。若使用C3P0连接池来池化连接,随时取用,则平均每次取用只需要10-20毫秒。

这在高并发随机访问数据库的时候对效率的提升有很大帮助。

      C3P0连接池会根据你的配置来初始化N个数据库连接,空闲T时间后连接过期又会自动新建K个连接使得连接池总有空闲的数据库连接等待被取用。我们只需通过dataSourse.getConnection()

即可从线程池中取用一个已经连接好的空闲连接,执行数据库操作。然后“断开”(放回)这个连接,把这个连接的使用权放回连接池。真正的数据库连接的创建与释放是由C3P0在后台自动完成的

,我们花的只是取用与释放占用权的时间。全程耗时10+毫秒,比原来提高了几十倍。

下面我使用idea举例

首先需要三个jar包:

整体的项目结构如下;

这里的xml文件默认只能放在src文件下才能识别,且名字不能改

c3p0-config.xml代码如下:

<?xml version="1.0" encoding="UTF-8" ?>
<c3p0-config>
    <!-- This is default config! -->
    <default-config>
        <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 is my config for mysql-->
    <named-config name="mysql">
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/customer</property>
        <property name="user">root</property>
        <property name="password">1234</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>
    </named-config>
</c3p0-config>

在Test.java中测试

package main;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.sql.*;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * Created by Administrator on 2017/8/4.
 */
public class Test {
    static ComboPooledDataSource cpds=null;
    static{
        //这里有个优点,写好配置文件,想换数据库,简单
        //cpds = new ComboPooledDataSource("oracle");//这是oracle数据库
        cpds = new ComboPooledDataSource("mysql");//这是mysql数据库
    }
    /**
     * 获得数据库连接
     * @return   Connection
     */
    public static Connection getConnection(){
        try {
            return cpds.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 数据库关闭操作
     * @param conn
     * @param pst
     * @param rs
     */
    public static void close(Connection conn, PreparedStatement pst, ResultSet rs){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(pst!=null){
            try {
                pst.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 测试DBUtil类
     * @param args
     */
    public static void main(String[] args) {
        Connection conn=getConnection();
        try {
            PreparedStatement pst=conn.prepareStatement("SELECT * FROM t_customer");
            ResultSet rs=pst.executeQuery();
            while(rs.next()){
                System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }


        close(conn,null,null);
    }



}
原文地址:https://www.cnblogs.com/liushuncheng/p/7283671.html