Search code examples
javamysqlhikaricp

Can't cast MySQLDataSource to java.sql.driver


In this Java project, I wanted to try working with HikariCP for one, see if I would like it.

It seems very simple at first, but when I actually try to run my application, it goes wrong. The following code is a function in my DatabasePool.java

    HikariConfig configuration = new HikariConfig();
    configuration.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/testdb");
    configuration.addDataSourceProperty("serverName","127.0.0.1");
    configuration.addDataSourceProperty("port", 3306);
    configuration.addDataSourceProperty("databaseName", "testdb"));
    configuration.addDataSourceProperty("user", "root");
    configuration.addDataSourceProperty("password", "123");
    configuration.setDriverClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
    this.database = new HikariDataSource(configuration);
    return true;

And for some reason, when this gets ran, I get the following error back:

[main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-0 - is starting.
[main] WARN com.zaxxer.hikari.util.DriverDataSource - Registered driver with driverClassName=org.mariadb.jdbc.MySQLDataSource was not found, trying direct instantiation.
[main] WARN com.zaxxer.hikari.util.DriverDataSource - Could not instantiate instance of driver class org.mariadb.jdbc.MySQLDataSource, trying JDBC URL resolution
java.lang.ClassCastException: org.mariadb.jdbc.MySQLDataSource cannot be cast to java.sql.Driver

I checked if the class exists in my project, it does. I can't really think of a cause for this.

I'm new to pretty new to Java, so I have no idea if I supplied enough information in the question. Please state it if that's not the case.


Solution

  • A DataSource is not a Driver. Use setDataSourceClassName() instead (I seem to remember it being recommended over setDriverClassName() anyway).