MySql && MySQL数据库 连接池(四)

连接池

在这里插入图片描述

市面上在Java领域中成熟的连接池产品有很多,常见的有: C3P0 DBCP (老牌的) Druid HikariCP (新秀)

不论采用哪种连接池,最基本的四要素:

  • 驱动程序
  • URL地址
  • 用户名
  • 密码

C3P0

官网: https://www.mchange.com/projects/c3p0/

在这里插入图片描述

必须记忆: 它的核心类是 ComboPooledDataSource

    public static void main(String[] args) throws SQLException {
        ComboPooledDataSource cpds = new ComboPooledDataSource();
        //池创建好以后,通过getConnection 方法就可以获取 可用的数据库的连接对象。
        Connection connection = cpds.getConnection();

        //中间的代码跟以前一模一样。
        PreparedStatement preparedStatement = connection.prepareStatement("select * from tb1");
        ResultSet resultSet = preparedStatement.executeQuery();
        while(resultSet.next()){
            System.out.println(resultSet.getString("c1"));
        }

        resultSet.close();

        //这里要注意一下,这个拿到的connection对象它的close方法跟以前通过驱动管理器拿到的对象的close方法行为不一致。
        //以前的close方法是方法连接资源。
        //现在的close方法是将连接还给连接池。
        connection.close();

        //程序退出,释放池中的资源
        cpds.close();
    }

DBCP

官网: http://commons.apache.org/proper/commons-dbcp/
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

 public static void main(String[] args) throws Exception {
        //0. 通过配置文件加载 Properties 对象
        Properties properties = new Properties();
        properties.load(Demo.class.getResourceAsStream("dbcp-config.properties"));
        //1. 记住使用核心对象 BasicDataSourceFactory 来进行构建
        BasicDataSource dataSource = BasicDataSourceFactory.createDataSource(properties);

        //2.通过连接池获取可用的连接
        Connection connection = dataSource.getConnection();

        //中间的代码跟以前一模一样。
        PreparedStatement preparedStatement = connection.prepareStatement("select * from tb1");
        ResultSet resultSet = preparedStatement.executeQuery();
        while(resultSet.next()){
            System.out.println(resultSet.getString("c1"));
        }

        resultSet.close();

        //将连接放回池中
        connection.close();

        //关闭连接池
        dataSource.close();
    }

Druid

https://github.com/alibaba/druid

文档:
https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98

在这里插入图片描述

核心类 : DruidDataSource

    public static void main(String[] args) throws IOException, SQLException {
        Properties properties = new Properties();
        //注意:配置项要使用 druid. 前缀
        properties.load(Demo.class.getResourceAsStream("druid-config.properties"));
        //使用核心对象
        DruidDataSource dataSource= new DruidDataSource();
        //通过properties配置文件来进行配置
        dataSource.configFromPropety(properties);

        //2.通过连接池获取可用的连接
        Connection connection = dataSource.getConnection();

        //中间的代码跟以前一模一样。
        PreparedStatement preparedStatement = connection.prepareStatement("select * from tb1");
        ResultSet resultSet = preparedStatement.executeQuery();
        while(resultSet.next()){
            System.out.println(resultSet.getString("c1"));
        }

        resultSet.close();

        //将连接放回池中
        connection.close();


        dataSource.close();
    }

HikariCP

官网: https://github.com/brettwooldridge/HikariCP
在这里插入图片描述

核心对象: HikariDataSource

注意:它还依赖 slf4j-api-1.7.25.jar jar包

 public static void main(String[] args) throws SQLException {
//        Properties properties = new Properties();
//        properties.load(Demo.class.getResourceAsStream("hikaricp-config.properties"));
//        HikariConfig hikariConfig = new HikariConfig(properties);
        HikariConfig hikariConfig = new HikariConfig("/com/example/hikaricp/hikaricp-config.properties");
        HikariDataSource dataSource = new HikariDataSource(hikariConfig);

        //2.通过连接池获取可用的连接
        Connection connection = dataSource.getConnection();

        //中间的代码跟以前一模一样。
        PreparedStatement preparedStatement = connection.prepareStatement("select * from tb1");
        ResultSet resultSet = preparedStatement.executeQuery();
        while(resultSet.next()){
            System.out.println(resultSet.getString("c1"));
        }

        resultSet.close();

        //将连接放回池中
        connection.close();


        dataSource.close();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值