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

    -package cn.gdpe.pool;

    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    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 DataPool {
        //硬编码方式  设置数据源各种参数
        public void test1(){
            try {
                BasicDataSource dataSource=new BasicDataSource();//核心类  dbcp  pool 核心类
                dataSource.setInitialSize(3);    //初始化连接数
                dataSource.setMaxActive(6);        //最大连接数
                dataSource.setMaxIdle(3000);    //最大空闲时间
                dataSource.setDriverClassName("com.mysql.jdbd.Driver");//驱动
                dataSource.setUrl("jdbc:mysql://localhost:3306/day15");//数据库连接地址
                dataSource.setUsername("root");//用户名
                dataSource.setPassword("root");//用户密码
                Connection conn=dataSource.getConnection();
                PreparedStatement psm=conn.prepareStatement("delete from user where id=4");
                psm.executeUpdate();
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    ------------------------分割线-------------------------------------------
        //配置文件方式  设置数据源各种参数 建议这种方法
        @Test
        public void test2(){
            try {
                InputStream is=DataPool.class.getResourceAsStream("/db.properties");
                Properties p=new Properties();
                p.load(is);
                DataSource dataSource=BasicDataSourceFactory.createDataSource(p);//核心类  dbcp  pool 核心类
                Connection conn=dataSource.getConnection();
                PreparedStatement psm=conn.prepareStatement("delete from user where id=6");
                psm.executeUpdate();
                conn.close();
                is.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

  • 相关阅读:
    centos7下安装docker
    java中获取两个时间中的每一天
    Linq中string转int的方法
    logstash 主题综合篇
    Windows环境下ELK(5.X)平台的搭建
    本地没问题 服务器 提示 Server Error in '/' Application
    错误 未能找到类型或命名空间名称"xxxxxx"的真正原因
    System.web和System.WebServer
    Chrome Adobe Flash Player 因过期而 阻止
    请求WebApi的几种方式
  • 原文地址:https://www.cnblogs.com/ly-china/p/5420023.html
Copyright © 2011-2022 走看看