项目场景:
使用DriverManager动态加载JDBC驱动时,报:java.sql.SQLException: No suitable driver found for xxxx 错误
问题描述:
DriverManager 能够根据 JDBC 连接字符串匹配到驱动类,所以一般来说都不需要显式调用 DriverManager.registerDriver() 方法。
因此直接使用Class.forName进行加载
// 代码摘自[参考文章](https://yanbin.blog/custom-classload-dynamic-load-jdbc-driver/#more-8187)
package cc.unmi;
public class JdbcDriverLoader {
public static void main(String[] args) {
notWork();
}
public static void notWork() throws Exception {
URL url = new URL("file:~/drivers/mysql-connector-java-5.1.43.jar");
String driverClass = "com.mysql.jdbc.Driver";
URLClassLoader classLoader = new URLClassLoader(new URL[] {url});
Class.forName(driverClass, true, classLoader);
DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useSSL=false", "root", "");
}
}
执行最后一行获取数据库连接时报出的异常:java.sql.SQLException: No suitable driver found for xxxx
这里表明能正确用自定义的类加载器加载到驱动类 com.mysql.jdbc.Driver, 否则会报出 ClassNotFound 的异常。
那么直接使用显式调用 DriverManager.registerDriver() 方法
// 代码摘自[参考文章](https://yanbin.blog/custom-classload-dynamic-load-jdbc-driver/#more-8187)
package cc.unmi;
public class JdbcDriverLoader {
public static void main(String[] args) {
notWork();
}
public static void notWork() throws Exception {
URL url = new URL("file:~/drivers/mysql-connector-java-5.1.43.jar");
String driverClass = "com.mysql.jdbc.Driver";
URLClassLoader classLoader = new URLClassLoader(new URL[] {url});
Driver driver = (Driver) Class.forName(driverClass, true, classLoader).newInstance();
DriverManager.registerDriver(driver);
DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useSSL=false", "root", "");
}
}
还是报异常:java.sql.SQLException: No suitable driver found for xxxx
原因分析:
类加载是正常的,那么问题就出现在DriverManager
DriverManager在获取连接时会用加载 cc.unmi.JdbcDriverLoader 类的加载器(Launcher$AppClassLoader)检验一下是否能加载到数据库驱动类,显然这里要卡壳了。com.mysql.jdbc.Driver 对于应用程序类加载器是不可能见的,所以报出驱动找不到的异常。
解决方案:
- 参考文章作者:
使用了 DriverShim 包装类后,在 getConnection() 时 DriverManager 同样要验证驱动是否对应用程序类加载器可见,只是这时候要验证的是这个 DriverShim 而非通过自定义类加载器弄进来的 “com.mysql.jdbc.Driver” 了。而后在使用 DriverShim 桥接到实际的 com.mysql.jdbc.Driver 时 DriverManager 就管不着了。
大致意思就是:你 DriverManager 不是想验证数据库驱动是否是应用程序类加载器加载的吗?给个壳逗你玩一下,我在壳里面呢,你管我是由哪个类加载器加载的。
所以我们建一个 DriverShim 包装类,对 driver 进行包装,代码如下:
// 代码摘自[参考文章](https://yanbin.blog/custom-classload-dynamic-load-jdbc-driver/#more-8187)
import java.sql.*;
class DriverShim implements Driver {
private Driver driver;
DriverShim(Driver d) {
this.driver = d;
}
public boolean acceptsURL(String u) throws SQLException {
return this.driver.acceptsURL(u);
}
public Connection connect(String u, Properties p) throws SQLException {
return this.driver.connect(u, p);
}
public int getMajorVersion() {
return this.driver.getMajorVersion();
}
public int getMinorVersion() {
return this.driver.getMinorVersion();
}
public DriverPropertyInfo[] getPropertyInfo(String u, Properties p) throws SQLException {
return this.driver.getPropertyInfo(u, p);
}
public boolean jdbcCompliant() {
return this.driver.jdbcCompliant();
}
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return null;
}
}
// 代码摘自[参考文章](https://yanbin.blog/custom-classload-dynamic-load-jdbc-driver/#more-8187)
public static void notWork() throws Exception {
URL url = new URL("file:~/drivers/mysql-connector-java-5.1.43.jar");
String driverClass = "com.mysql.jdbc.Driver";
URLClassLoader classLoader = new URLClassLoader(new URL[] {url});
Driver driver = (Driver) Class.forName(driverClass, true, classLoader).newInstance();
DriverManager.registerDriver(new DriverShim(driver));
DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useSSL=false", "root", "");
}