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

    连接池:程序不用自己来创建连接,用池来管理连接,连接池会默认创建n个初始连接,用的时候从里面捞几个上来,用完了再扔回去,可以重复利用,这个池子里的连接是用各种方法创建的,其中就包括用jdbc创建的

    dbcp连接池:

    导入jar包:commons-dbcp-1.4.jar

    public static void main(String[] args) throws Exception {
         //创建连接池对象
         BasicDataSource dataSource = new BasicDataSource();
         //配置连接参数(必写)
         dataSource.setDriverClassName("com.mysql.jdbc.Driver");
         dataSource.setUrl("jdbc:mysql://localhost:3306/demo");
         dataSource.setUsername("root");
         dataSource.setPassword("root");
         
         //配置连接池参数(可选)
         dataSource.setInitialSize(10);//设置连接池的初始大小
         dataSource.setMaxActive(30);//设置连接池的最大连接数
         dataSource.setMaxIdle(5);//设置最大等待连接
         dataSource.setMaxWait(1000);//设置最大等待时间(毫秒),超出这个时间getConnection()方法会抛出异常
              
         //从连接池中得到连接
         Connection con = dataSource.getConnection();
    //测试连接 System.out.println(con); }

     ps:  

    org.apache.commons.dbcp.BasicDataSource implements javax.sql.DataSource 
    //DataSource接口是sun为各大数据库厂商提供的连接池接口,BasicDataSource是mysql公司对这个接口的实现

    c3p0连接池:

    导入jar包:c3p0-0.9.2-pre1.jar, mchange-commons-0.2.jar,c3p0-oracle-thin-extras-0.9.2-pre1.jar(oracle专用jar包,不用oracle可以不导)

    public static void main(String[] args) throws Exception {
            //创建连接池对象
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            //配置连接参数(必须)
            dataSource.setDriverClass("com.mysql.jdbc.Driver");
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/demo");
            dataSource.setUser("root");
            dataSource.setPassword("root");
            
            //配置连接池参数(可选)
            dataSource.setAcquireIncrement(5);//设置连接增量
            dataSource.setInitialPoolSize(20);//设置连接池初始连接数量
            dataSource.setMaxPoolSize(100);//设置最大连接数量
            dataSource.setMinPoolSize(3);//设置最小连接数量,低于这个数量,会按增量值增加连接
            
            
            Connection con = dataSource.getConnection();
            System.out.println(con);
            //把连接归还给连接池
            con.close();
            
        }

    c3p0也可以使用配置文件,文件名必须为c3p0-config.xml,并且放在类路径下

    <?xml version="1.0" encoding="UTF-8"?>
    <c3p0-config>
        <default-config>
    <property name="jdbcUrl">jdbc:mysql://localhost:3306/demo</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="user">root</property> <property name="password">root</property> <property name="acquireIncrement">3</property> <property name="initialPoolSize">10</property> <property name="minPoolSize">2</property> <property name="maxPoolSize">10</property> </default-config> <named-config name="oracle-config">
    <property name="jdbcUrl">jdbc:mysql://localhost:1521/demo</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="user">root</property> <property name="password">root</property> <property name="acquireIncrement">3</property> <property name="initialPoolSize">10</property> <property name="minPoolSize">2</property> <property name="maxPoolSize">10</property> </named-config> </c3p0-config>

    使用默认配置

    public static void main(String args[]) throws Exception {
         ComboPooledDataSource dataSource = new ComboPooledDataSource();//无参,默认使用xml中default-config标签中的配置
         Connection con = dataSource.getConnection();
         System.out.print(con);
         con.close();   
    }

    使用命名配置

    public static void main(String args[]) throws Exception {
         ComboPooledDataSource dataSource = new ComboPooledDataSource("oralce-config");//带参,使用xml中named-config标签的配置 
    Connection con = dataSource.getConnection();
    System.out.print(con);
    con.close();
    }
  • 相关阅读:
    Web打印控件
    excel错误:外部表不是预期的格式 错误
    C#用ado.net访问EXCEL的常见问题及解决方法
    通过反射的方式获取类型中的所有属性
    在64位Windows7上安装64位Oracle11g
    2020&2021的计划
    jQuery_day1
    springboot+mybatis+MySQL(入门级-半小时搞定系列)
    springboot_web开发
    springboot日志
  • 原文地址:https://www.cnblogs.com/sflik/p/4602415.html
Copyright © 2011-2022 走看看