vivek
vivek

Reputation: 145

Oracle db connection using hikaricp java

I'm trying to create a connection pool for Oracle database using hikaricp and java..

Here is my below code..

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;
    }
}

and I'm getting below error.

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

Any suggestions would also be helpful.. Thanks..

Upvotes: 2

Views: 4976

Answers (1)

Tanuj
Tanuj

Reputation: 2260

In the dataSource() method you are trying to assign the driver class to the data source class property. Instead of using setDataSourceClassName() use the setDriverClassName() method.

Here's how your final configuration will looks like -

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;
}

Upvotes: 3

Related Questions