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());
        }
    
    }
    
  • 相关阅读:
    终于有人把 Docker 讲清楚了,万字详解!
    Java 14 可能带来什么新特性?
    我 45 岁还写代码,怎么了?
    傻傻分不清?Integer、new Integer() 和 int 的面试题
    showModalDialog打开页面有缓存,不走action
    ajax,下拉框级联
    jsp中<!DOCTYPE>标签
    struts配置文件中如何从一个package的action跳到另一个package中的某个action
    jsp通过s:hidden传值给后台,后台数据出现了若干逗号问题
    struts2.0整合json
  • 原文地址:https://www.cnblogs.com/wangxiaopei/p/8551275.html
Copyright © 2011-2022 走看看