Druid(德鲁伊)连接池是阿里提供的数据库连接池,集DBCP,C3P0,Proxool的优点于一身的数据库连接池,功能强大,速度快,稳定性好,具有强大的监控功能,也可以防止SQL的注入。
1、在使用Druid(德鲁伊)连接池时,需要先加入Druid的jar包和配置文件
#key=value #驱动 driverClassName=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/books?rewriteBatchedStatements=true&serverTimezone=UTC #url=jdbc:mysql://localhost:3306/books #要连接的数据库的用户名和密码 username=root password=123456 #initial connection Size 初始连接个数 initialSize=10 #min idle connecton size 空闲时期的最小连接数 minIdle=5 #max active connection size 最大连接数 maxActive=20 #max wait time (5000 mil seconds) 进程的最大等待时间 maxWait=5000
2、加载配置文件
Properties properties = new Properties();
properties.load(new FileReader("src\druid.properties"));
3、创建一个指定参数的数据库连接池
DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
4、建立连接
Connection connection = dataSource.getConnection();



