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();
            }
        }
    }

  • 相关阅读:
    mysql常用命令
    mysql设置外网访问权限
    免费云服务部署项目
    使用虚拟主机部署Php项目总结
    github基本使用命令笔记
    git push -u origin master报错,error: failed to push some refs to 'https://github.com/Youlandawq/Qt.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote c
    centos7安装docker
    java设计模式之单例模式
    java se高级之多线程(一)
    jdbc编程
  • 原文地址:https://www.cnblogs.com/ly-china/p/5420023.html
Copyright © 2011-2022 走看看