首页 > 代码库 > c3p0连接数据库

c3p0连接数据库

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
    <default-config>
        <!-- 最多可以创建多少个statement -->
        <!-- 最多可以创建多少个statement -->
        <property name="maxStatements">50</property>
        <!-- 最多有多少个连接 -->
        <property name="maxPoolSize">4</property>
        <property name="initialPoolSize">2</property>
        <property name="acquireIncrement">2</property>
        <property name="minPoolSize">2</property>
        <!-- 连接的超时间 -->
        <property name="checkoutTimeout">3000</property>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql:///abc
        </property>
        <property name="user">root</property>
        <property name="password">1234</property>
    </default-config>

    <!-- 配置其他的连接,用户的其他配置可以有多个 -->
    
    
    <named-config name="jtedu">
    
    <property name="maxStatements">50</property>
        <!-- 最多有多少个连接 -->
        <property name="maxPoolSize">4</property>
        <property name="initialPoolSize">2</property>
        <property name="acquireIncrement">2</property>
        <property name="minPoolSize">2</property>
        <!-- 连接的超时间 -->
        <property name="checkoutTimeout">3000</property>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql:///abc
        </property>
        <property name="user">root</property>
        <property name="password">1234</property>
     </named-config> 
</c3p0-config>
package c3p0连接池;

import java.sql.Connection;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class c3p0Utiles {
    
//    public static void main(String[] args) {
//        System.err.println(c3p0Utiles.getCon());
//        
//    }

    private static DataSource ds;
    static {
//默认配置  
ds
= new ComboPooledDataSource();
//指定配置
//ds=new ComboPooledDataSource("jteud"); }
public static DataSource getDs() { return ds; } public static Connection getCon() { Connection con = null; try { con = ds.getConnection(); } catch (Exception e) { e.printStackTrace(); } return con; } }