C3p0连接池配置

package DB;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * Created by Administrator on 2018\11\23 0023.
 */
public class C3p0Utils {


    private static DataSource dataSource=null;
    static{
        dataSource=new ComboPooledDataSource("mysql");
    }

    /*
     c3p0数据库连接
     */

    public static Connection getConnection(){
       Connection connection = null;
       try{
           connection = dataSource.getConnection();
       }catch (Exception E){
           E.printStackTrace();
       }
        return connection;
    }

    /**
     * 关闭数据库连接
     *
     */
    public static void closeConn(Connection connection){
        try {
            //如果连接不等于空  并且连接已经关闭 选择关闭连接connection

            if(connection!=null && connection.isClosed()){
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }



}

 

 

配置 




    
        10
        30
        100
        10
        200
    

    
        com.mysql.jdbc.Driver
        jdbc:mysql://localhost:3306/test
        root
        123456
        10
        30
        100
        10
        200
    

转载于:https://my.oschina.net/u/3554714/blog/2942335

你可能感兴趣的:(C3p0连接池配置)