HikariCP 是几个常见数据库连接池中出现的最晚的一个。它口号是 “快速、简单、可靠” ,官方宣传是性能最快的数据库连接池(貌似也没有其它方对此表示异议)。
<dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>3.3.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.17</version> </dependency>
Properties properties = new Properties(); properties.setProperty("driverClassName", "com.mysql.cj.jdbc.Driver"); properties.setProperty("jdbcUrl", "jdbc:mysql://127.0.0.1:3306/........?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false"); properties.setProperty("username", "........"); properties.setProperty("password", "........"); DataSource dataSource = new HikariDataSource(new HikariConfig(properties)); Connection connection = dataSource.getConnection(); System.out.println(connection == null ? "not connected" : "connected");
注意
在配置数据库连接四大属性时,HikariCP 和其它数据库连接池用到了不同的单词。它使用到的是 driverClassName 和 jdbcUrl 。其它数据库连接池通常用的是 driver 和 url 。
driverClassName=com.mysql.cj.jdbc.Driver jdbcUrl=jdbc:mysql://127.0.0.1:3306/scott?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false username=root password=123456
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("jdbc.properties"); Properties properties = new Properties(); properties.load(is); DataSource dataSource = new HikariDataSource(new HikariConfig(properties)); Connection connection = dataSource.getConnection(); System.out.println(connection == null ? "not connected" : "connected");
← slf4j 日志库 Jackson →