Java

JDBC DBCP数据库连接池

2019-12-23  本文已影响0人  一亩三分甜

配置数据库连接池DBCP两种方式

1.手动配置

    //方式一:不推荐
    @Test
    public void testGetConnection() throws SQLException{
        //创建了DBCP的数据库连接池
        BasicDataSource source = new BasicDataSource();

        //设置基本信息
        source.setDriverClassName("com.mysql.jdbc.Driver");
        source.setUrl("jdbc:mysql:///firstDB?rewriteBatchedStatements=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8");
        source.setUsername("root");
        source.setPassword("123");

        //还可以设置其他涉及数据库连接池管理的相关属性
        source.setInitialSize(10);
        source.setMaxActive(10);
        //。。。

        Connection conn = source.getConnection();
        System.out.println(conn);
    }
//输出
jdbc:mysql:///firstDB?rewriteBatchedStatements=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8, UserName=root@localhost, MySQL Connector Java

2.通过dbcp.properties文件配置

    //方式二:推荐:使用配置文件
    @Test
    public void testGetConnection1() throws Exception{
        Properties pros = new Properties();
        //方式1:
//        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("dbcp.properties");
        //方式2:
        FileInputStream is = new FileInputStream(new File("src/dbcp.properties"));
        pros.load(is);
        DataSource source = BasicDataSourceFactory.createDataSource(pros);

        Connection conn = source.getConnection();
        System.out.println(conn);
    }
//输出
jdbc:mysql:///firstDB?rewriteBatchedStatements=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8, UserName=root@localhost, MySQL Connector Java

使用DBCP数据库连接池技术

public class JDBCUtils {
   /**
    * @Description:使用DBCP的数据库连接池技术
    * @Param:
    * @return:
    * @Author: fish
    * @Date: 11:47 AM 2019/12/23
    */
    //创建一个DBCP数据库连接池
    private static DataSource  source;
    static {
        try {
            Properties pros = new Properties();
            //方式1:
//        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("dbcp.properties");
            //方式2:
            FileInputStream is = new FileInputStream(new File("src/dbcp.properties"));
            pros.load(is);
            source = BasicDataSourceFactory.createDataSource(pros);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static Connection getConnection2() throws Exception{
        Connection conn = source.getConnection();
        return conn;
    }
}
    @Test
    public void getCustomerById(){
        Connection conn = null;
        try{
            conn = JDBCUtils.getConnection2();
            Customer customer = dao.getCustomerById(conn,8);
            System.out.println(customer);
            System.out.println("查询成功");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(conn,null);
        }
    }
//输出
十二月 23, 2019 2:06:19 下午 com.mchange.v2.log.MLog <clinit>
信息: MLog clients using java 1.4+ standard logging.
十二月 23, 2019 2:06:19 下午 com.mchange.v2.c3p0.C3P0Registry banner
信息: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
Customer{id=8, name='陈道明', email='bdf@126.com', birth=1963-09-16 00:00:00.0}
查询成功
上一篇下一篇

猜你喜欢

热点阅读