如果您正在运行jar
工件,您可以在 Linux/MacOS 中使用以下命令进行验证,(示例显示mysql jdbc 驱动程序)
$ 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
可用并且您仍然收到No suitable driver
错误,请注册JdbcDriver
.
使用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();
}
}