Reputation: 1173
I tried switching to Hikari-CP for java 1.6/1.7
but i m getting the below error:
Caused by: java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(DriverManager.java:278) ~[na:1.7.0_45]
at com.zaxxer.hikari.util.DriverDataSource.<init>(DriverDataSource.java:52) ~[HikariCP-java6-2.3.5.jar:na]
... 82 common frames omitted
I m using Oracle DB.
Upvotes: 5
Views: 11142
Reputation: 3357
If you sure that driver jar and other dependencies are in place and just you have received this error then try to build a connection by this manner.
config.setJdbcUrl(JDBC_URL);
config.setUsername(JDBC_USERNAME);
config.setPassword(JDBC_PASSWORD);
config.setDriverClassName(JDBC_DRIVERCLASSNAME);
HikariCp looks these properties like this.
If you try to set the driver class via property it won't work and you will receive the error related to driver.
props.addDataSourceProperty("dataSourceClassName", "com.mysql.cj.jdbc.Driver");
Upvotes: 3
Reputation: 31212
JdbcDriver
is availableIf you are running a jar
artifact you can verify with following command in Linux/MacOS, (example shows mysql jdbc Driver)
$ jar -tvf your-lovely.jar | grep "com/mysql/cj/jdbc/Driver"
733 Sun Mar 25 07:00:36 PDT 2018 com/mysql/cj/jdbc/Driver.class
JdbcDriver
is available and you still get No suitable driver
error, register the JdbcDriver
.Example using HicariConfig#setDriverClassName,
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import java.sql.Connection;
import java.sql.SQLException;
class ConnectionPool {
private HikariDataSource ds = new HikariDataSource(getConfig());
private HikariConfig getConfig() {
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:mysql://localhost:3306/lovely_database_name");
config.setUsername("root");
config.setPassword("root");
config.setDriverClassName("com.mysql.cj.jdbc.Driver"); //alternative is Class.forName("com.mysql.cj.jdbc.Driver")
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
return config;
}
Connection getConnection() throws SQLException {
return ds.getConnection();
}
}
Upvotes: 4
Reputation: 1173
This line from documentation was the reason.
When using this property with "old" drivers, you may also need to set the driverClassName property, but try it first without.
The minute i did setup the driverClassName explicitly it started working.
Upvotes: 5
Reputation: 11114
I sounds like the driver jar is not visible in the classpath, or the driver is not a self-registering driver. What database are you using? Please add more detail to your question.
Upvotes: 2