在这个 Java 项目中,我想尝试使用 HikariCP,看看我是否愿意。
乍一看似乎很简单,但当我实际尝试运行我的应用程序时,它出错了。以下代码是我的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;
由于某种原因,当它运行时,我收到以下错误:
[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
我检查了该类是否存在于我的项目中,确实存在。我实在想不出这是什么原因。
我对 Java 还很陌生,所以我不知道我是否在问题中提供了足够的信息。如果不是这样,请说明。
最佳答案
数据源
不是驱动程序
。使用 setDataSourceClassName() 代替(我似乎记得它比 setDriverClassName()
更推荐)。
关于java - 无法将 MySQLDataSource 转换为 java.sql.driver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35459930/