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

    l  DBCP 是 Apache 软件基金组织下的开源连接池实现,使用DBCP数据源,应用程序应在系统中增加如下两个 jar 文件:

    • Commons-dbcp.jar:连接池的实现
    • Commons-pool.jar:连接池实现的依赖库

    l  Tomcat 的连接池正是采用该连接池来实现的。该数据库连接池既可以与应用服务器整合使用,也可由应用程序独立使用。

    l  核心类:BasicDataSource

    l  使用步骤

    • 引入jar文件
     commons-dbcp-1.4.jar
     commons-pool-1.5.6.jar
    package com.loaderman.demo.c_dbcp;
    
    import java.io.InputStream;
    import java.sql.Connection;
    import java.util.Properties;
    
    import javax.sql.DataSource;
    
    import org.apache.commons.dbcp.BasicDataSource;
    import org.apache.commons.dbcp.BasicDataSourceFactory;
    import org.junit.Test;
    
    public class App_DBCP {
    
        // 1. 硬编码方式实现连接池
        @Test
        public void testDbcp() throws Exception {
            // DBCP连接池核心类
            BasicDataSource dataSouce = new BasicDataSource();
            // 连接池参数配置:初始化连接数、最大连接数 / 连接字符串、驱动、用户、密码
            dataSouce.setUrl("jdbc:mysql:///jdbc_demo");            //数据库连接字符串
            dataSouce.setDriverClassName("com.mysql.jdbc.Driver");  //数据库驱动
            dataSouce.setUsername("root");                            //数据库连接用户
            dataSouce.setPassword("root");                             //数据库连接密码
            dataSouce.setInitialSize(3);  // 初始化连接
            dataSouce.setMaxActive(6);      // 最大连接
            dataSouce.setMaxIdle(3000);   // 最大空闲时间
    
            // 获取连接
            Connection con = dataSouce.getConnection();
            con.prepareStatement("delete from admin where id=3").executeUpdate();
            // 关闭
            con.close();
        }
    
        @Test
        // 2. 【推荐】配置方式实现连接池  ,  便于维护
        public void testProp() throws Exception {
            // 加载prop配置文件
            Properties prop = new Properties();
            // 获取文件流
            InputStream inStream = App_DBCP.class.getResourceAsStream("db.properties");
            // 加载属性配置文件
            prop.load(inStream);
            // 根据prop配置,直接创建数据源对象
            DataSource dataSouce = BasicDataSourceFactory.createDataSource(prop);
    
            // 获取连接
            Connection con = dataSouce.getConnection();
            con.prepareStatement("delete from admin where id=4").executeUpdate();
            // 关闭
            con.close();
        }
    }

    db.propertes配置文件

    url=jdbc:mysql:///jdbc_demo
    driverClassName=com.mysql.jdbc.Driver
    username=root
    password=root
    initialSize=3
    maxActive=6
    maxIdle=3000
  • 相关阅读:
    Ubuntu配置sublime text 3的c编译环境
    ORA-01078错误举例:SID的大写和小写错误
    linux下多进程的文件拷贝与进程相关的一些基础知识
    ASM(四) 利用Method 组件动态注入方法逻辑
    基于Redis的三种分布式爬虫策略
    Go语言并发编程总结
    POJ2406 Power Strings 【KMP】
    nyoj 会场安排问题
    Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds. If the server requires more time, try increasing the timeout in the server editor.
    Java的String、StringBuffer和StringBuilder的区别
  • 原文地址:https://www.cnblogs.com/loaderman/p/10018725.html
Copyright © 2011-2022 走看看