本文介绍 HiKariCP 连接池配置依赖、配置文件和示例代码。
配置依赖
pom.xml
文件内容如下:
<dependency>
<groupId>com.oceanbase</groupId>
<artifactId>oceanbase-client</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.3.1</version>
</dependency>
配置文件
jdbc.properties
文件内容如下:
jdbcUrl=jdbc:oceanbase://xxx.xxx.xxx.xxx:1521/?useSSL=false&useServerPrepStmts=true&serverTimezone=UTC
username=a****
password=******
dataSource.cachePrepStmts=true
dataSource.prepStmtCacheSize=250
dataSource.prepStmtCacheSqlLimit=2048
dataSource.useServerPrepStmts=true
dataSource.useLocalSessionState=true
dataSource.rewriteBatchedStatements=true
dataSource.cacheResultSetMetadata=true
dataSource.cacheServerConfiguration=true
dataSource.elideSetAutoCommits=true
dataSource.maintainTimeStats=false
示例代码
@Test
public void hikariTest() throws SQLException {
HikariConfig config = new HikariConfig("/jdbc.properties");
Connection conn = new HikariDataSource(config).getConnection();
PreparedStatement drop_table_data_test = conn.prepareStatement("drop table data_test");
Statement stmt = conn.createStatement();
try {
drop_table_data_test.execute();
} catch (SQLException e) {
}
conn.prepareStatement("create table data_test(test1 int)").execute();
for (int i = 0; i <= 10; i++) {
stmt.executeUpdate("insert into data_test values("+i+")");
}
ResultSet rs = conn.prepareStatement("select * from data_test;").executeQuery();
while (rs.next()){
System.out.print(rs.getObject(1)+"\t");
}
System.out.println();
System.out.println("------------------------");
stmt.executeUpdate("update data_test set test1 = 100;");
ResultSet rs1 = conn.prepareStatement("select * from data_test;").executeQuery();
while (rs1.next()){
System.out.print(rs1.getObject(1)+"\t");
}
System.out.println();
System.out.println("------------------------");
stmt.executeUpdate("delete from data_test");
ResultSet rs2 = stmt.executeQuery("select * from data_test;");
if (!rs2.next()){
System.out.println("数据删除成功");
}
stmt.close();
drop_table_data_test.execute();
drop_table_data_test.close();
rs.close();
conn.close();
}
文档内容是否对您有帮助?