java 数据库连接池HikariCP

HikariCP数据库连接池,代号:光,表示它非常快。github地址:https://github.com/brettwooldridge/HikariCP

jar包下载或maven引用地址:http://maven.outofmemory.cn/com.zaxxer/HikariCP/ 或 https://mvnrepository.com/artifact/com.zaxxer/HikariCP

Java 11+ maven artifact:

<dependency>
   <groupId>com.zaxxer</groupId>
   <artifactId>HikariCP</artifactId>
   <version>5.0.1</version>
</dependency>

Java 8 maven artifact (maintenance mode):

<dependency>
   <groupId>com.zaxxer</groupId>
   <artifactId>HikariCP</artifactId>
   <version>4.0.3</version>
</dependency>

Java 7 maven artifact (maintenance mode):

<dependency>
   <groupId>com.zaxxer</groupId>
   <artifactId>HikariCP-java7</artifactId>
   <version>2.4.13</version>
</dependency>

Java 6 maven artifact (maintenance mode):

<dependency>
   <groupId>com.zaxxer</groupId>
   <artifactId>HikariCP-java6</artifactId>
   <version>2.3.13</version>
</dependency>
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
 
/**
 * hikaricp 连接池示例
 * @author sms.reyo.cn
 * 2022-12-12
 */
public class HikariDemo {
 
    public static void main(String[] args) {
        
        //配置文件
        HikariConfig hikariConfig = new HikariConfig();
//        hikariConfig.setJdbcUrl("jdbc:mysql://localhost:3306/mydata");//mysql
        hikariConfig.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:orcl");//oracle
        hikariConfig.setDriverClassName("oracle.jdbc.driver.OracleDriver");
        hikariConfig.setUsername("whg");
        hikariConfig.setPassword("whg");
        hikariConfig.addDataSourceProperty("cachePrepStmts", "true");
        hikariConfig.addDataSourceProperty("prepStmtCacheSize", "250");
        hikariConfig.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
 
        HikariDataSource ds = new HikariDataSource(hikariConfig);
        Connection conn = null;
        Statement statement = null;
        ResultSet rs = null;
        try{
            
            //创建connection
            conn = ds.getConnection();
            statement = conn.createStatement();
            
            //执行sql
            rs = statement.executeQuery("select 100 s  from dual");
 
            //取数据
            if (rs.next()){
                System.out.println(rs.getString("s"));
            }
            
            //关闭connection
            conn.close();
        }
        catch (SQLException e){
            e.printStackTrace();
        }
 
    }
 
}

 

posted @ 2022-12-13 15:02  锐洋智能  阅读(368)  评论(0编辑  收藏  举报