0
点赞
收藏
分享

微信扫一扫

数据库连接池--Druid

夹胡碰 2022-02-01 阅读 183

常见的数据库连接池:

  • DBCP
  • C3P0
  • Druid

优点:

  • 资源重用
  • 提升系统响应速度
  •  避免数据库连接遗漏
  •         //导入jar包
    
            //定义配置文件
    
            //加载配置文件
            Properties prop = new Properties();
            prop.load(new FileInputStream("src/druid.properties"));
    
            //获取连接池对象
            DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
    
            //获取数据库连接Connection
            Connection connection = dataSource.getConnection();
    
            System.out.println(connection);

    注意:这里定义配置文件往往容易出错,导致程序报错

 

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///db1?useSSL=false&useServerPrepStmts=true
username=root
password=1234
#初始化连接数量
initialSize=5
#最大连接数
maxActive=10
#最大等待时间
maxWait=3000
举报

相关推荐

0 条评论