数据库连接池-druid

2021/6/14 19:21:07

本文主要是介绍数据库连接池-druid,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1  . jar包下载地址

            Central Repository: com/alibaba/druid/1.1.22 (maven.org)

2 .  定义 配置文件

          文件格式必须 为properties,文件不管放在哪个目录 下都可以 

driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/gb1
username=root
password=123456
initialSize=5
#初始化连接数
maxActive=10
#最大连接数
maxWait=3000
#最大等待时间

3  .   导入 jar包

           

 

    4. 简单的使用 

package connection_druid;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;

public class test_1 {
    public static void main(String[] args) throws Exception {
        Properties properties =  new Properties();
        //创建properties类对象
        InputStream resourceAsStream = test_1.class.getClassLoader().getResourceAsStream("druid.properties");
        //定义配置文件
        properties.load(resourceAsStream);
        //加载配置文件
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        Connection connection = dataSource.getConnection();
        //获取连接池对象
        System.out.println(connection);

       

    }
}

 



这篇关于数据库连接池-druid的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程