zoukankan      html  css  js  c++  java
  • C3p0连接池

    1.需要导入两个jar包

      c3p0-0.9.2-pre5.jar

           mchange-commons-java-0.2.3.jar

    2.将c3p0-config.xml导入到src根目录下(jar包可以自动加载里面的属性内容)

    3.创建数据源ComboPooledDataSource

    4.获取连接 getConnection

    代码如下:

    package c3p0;
    
    
    
    import java.sql.Connection;
    import java.sql.SQLException;
    
    import javax.sql.DataSource;
    
    
    
    import com.mchange.v2.c3p0.ComboPooledDataSource;
    
    
    public class C3p0Util {
        
        private ComboPooledDataSource dataSource = new ComboPooledDataSource();
        
        //获取数据源
        public ComboPooledDataSource getDataSource(){
            return dataSource;
        }
        
        //获取连接
        public Connection getConnection() throws SQLException{
            return dataSource.getConnection();
        }
        
        public static void main(String[] args) {
            
            try {
                //1.创建数据源
                /**
                 * 如果将c3p0-config.xml文件拷贝到src文件夹下
                 * 当创建dataSource对象的时候自动加载c3p0-config.xml文件中的<default-config>内容
                 * 如果打算使用<named-config name="aa">这个的话,直接将aa作为参数传入
                 * ComboPooledDataSource dataSource = new ComboPooledDataSource("aa");
                 */
                ComboPooledDataSource dataSource = new ComboPooledDataSource();
                
                //2获取链接
                Connection conn = dataSource.getConnection();
                
                System.out.println(conn);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    <c3p0-config>
        <!-- 默认配置,如果没有指定则使用这个配置 -->
        <default-config>
            <property name="driverClass">com.mysql.jdbc.Driver</property>
            <property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/shop</property>
            <property name="user">root</property>
            <property name="password">1234</property>
            
            <property name="checkoutTimeout">30000</property>
            <property name="idleConnectionTestPeriod">30</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>
            <user-overrides user="test-user">
                <property name="maxPoolSize">10</property>
                <property name="minPoolSize">1</property>
                <property name="maxStatements">0</property>
            </user-overrides>
        </default-config> 
        <!-- 命名的配置 -->
        <named-config name="cc">
            <property name="driverClass">com.mysql.jdbc.Driver</property>
            <property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/shop</property>
            <property name="user">root</property>
            <property name="password">1234</property>
            <!-- 如果池中数据连接不够时一次增长多少个 -->
            <property name="acquireIncrement">5</property>
            <property name="initialPoolSize">20</property>
            <property name="minPoolSize">10</property>
            <property name="maxPoolSize">40</property>
            <property name="maxStatements">0</property>
            <property name="maxStatementsPerConnection">5</property>
        </named-config>
    </c3p0-config> 
    
    
    
     
  • 相关阅读:
    标准输入输出流
    打印流
    数据输入输出流
    对象操作流
    随机流
    内存输出流
    序列流
    转换流示例代码
    字符流的示例代码
    字符流
  • 原文地址:https://www.cnblogs.com/itcx1213/p/8124174.html
Copyright © 2011-2022 走看看