zoukankan      html  css  js  c++  java
  • c3p0数据库连接池

    c3p0数据库连接池连接数据库
    1.导入jar包
    2.进行配置
    c3p0-config.xml文件:注意:xml文件必须放在src目录下

    ```
    <?xml version="1.0" encoding="UTF-8" ?>
    <c3p0-config>
        <default-config> 
            <property name="jdbcUrl">
                <![CDATA[
                    jdbc:mysql://localhost:3306/bookestore(集体连接的database名称)?useUnicode=true&characterEncoding=UTF8&useServerPrepStmts=true&prepStmtCacheSqlLimit=256&cachePrepStmts=true&prepStmtCacheSize=256&rewriteBatchedStatements=true
                ]]>
            </property>
            <property name="driverClass">com.mysql.jdbc.Driver</property>
            <property name="user">root</property>
            <property name="password">123456</property> 
           <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
            <property name="acquireIncrement">3</property>
           <!-- 初始化数据库连接池时连接的数量 -->
            <property name="initialPoolSize">10</property>
            <!-- 数据库连接池中的最小的数据库连接数 -->
            <property name="minPoolSize">2</property>
            <!-- 数据库连接池中的最大的数据库连接数 -->
            <property name="maxPoolSize">10</property>
        </default-config>
    </c3p0-config>
    
    
    
    ```3.代码实现
    
    
    
    
    
    <div class="se-preview-section-delimiter"></div>
    

    这里写代码片
    “`

    package Utils;//获取连接
    import java.sql.SQLException;
    import com.mchange.v2.c3p0.ComboPooledDataSource;
    import com.mysql.jdbc.Connection;
    public class c3p0Util {
        private static ComboPooledDataSource ds=new ComboPooledDataSource();
        public static java.sql.Connection getconnection() throws SQLException{
            return ds.getConnection();
    
        }
    
    }
    
    package test;
    
    
    import java.sql.Connection;
    import java.sql.SQLException;
    import Utils.c3p0Util;
    
    public class test {  //测试代码,可以看出,不再需要手动加载驱动,建立连接等操作
        public static void main(String[] args) throws SQLException {
            Connection conn=c3p0Util.getconnection();
            System.out.println(conn.getCatalog());
        }
    
    }
    
  • 相关阅读:
    uva 11294 Wedding
    uvalive 4452 The Ministers’ Major Mess
    uvalive 3211 Now Or Later
    uvalive 3713 Astronauts
    uvalive 4288 Cat Vs. Dog
    uvalive 3276 The Great Wall Game
    uva 1411 Ants
    uva 11383 Golden Tiger Claw
    uva 11419 SAM I AM
    uvalive 3415 Guardian Of Decency
  • 原文地址:https://www.cnblogs.com/wangxiaopei/p/8551275.html
Copyright © 2011-2022 走看看