使用 hikaricp 的 Oracle 数据库连接 java

Oracle db connection using hikaricp java

我正在尝试使用 hikaricp 和 java..

为 Oracle 数据库创建连接池

下面是我的代码..

public class OracleCon {

    public static void main(String[] args) {
        try {

            Connection con = dataSource().getConnection();
            Statement stmt = con.createStatement();

        } catch (Exception e) {
            System.out.println(e);
        }

    }

    private static DataSource dataSource() {
        final HikariDataSource ds = new HikariDataSource();
        ds.setMaximumPoolSize(100);
        ds.setDataSourceClassName("oracle.jdbc.driver.OracleDriver");
        ds.addDataSourceProperty("serverName", "localhost");
        ds.addDataSourceProperty("port", "5432");
        ds.addDataSourceProperty("databaseName", "test");
        ds.addDataSourceProperty("user", "user");
        ds.addDataSourceProperty("password", "password");
        return ds;
    }
}

我遇到了以下错误。

09:15:10.627 [main] DEBUG com.zaxxer.hikari.HikariConfig - schema................................none
09:15:10.627 [main] DEBUG com.zaxxer.hikari.HikariConfig - threadFactory................................internal
09:15:10.627 [main] DEBUG com.zaxxer.hikari.HikariConfig - transactionIsolation................................default
09:15:10.627 [main] DEBUG com.zaxxer.hikari.HikariConfig - username................................none
09:15:10.627 [main] DEBUG com.zaxxer.hikari.HikariConfig - validationTimeout................................5000
09:15:10.627 [main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
java.lang.RuntimeException: java.lang.ClassCastException: Cannot cast oracle.jdbc.driver.OracleDriver to javax.sql.DataSource

任何建议也会有所帮助..谢谢..

dataSource() 方法中,您试图将 驱动程序 class 分配给 数据源 class 属性。不要使用 setDataSourceClassName(),而是使用 setDriverClassName() 方法。

您的最终配置如下所示 -

private static DataSource dataSource() {
    final HikariDataSource ds = new HikariDataSource();
    ds.setMaximumPoolSize(100);
    ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
    ds.addDataSourceProperty("serverName", "localhost");
    ds.addDataSourceProperty("port", "5432");
    ds.addDataSourceProperty("databaseName", "test");
    ds.addDataSourceProperty("user", "user");
    ds.addDataSourceProperty("password", "password");
    return ds;
}