我想在我的应用程序加载时加载 h2 数据库驱动程序。我使用 URLClassLoader 将外部库加载到类路径中,但出现以下错误:
19:25:56 [SEVERE] java.sql.SQLException: No suitable driver found for jdbc:h2:x\y\store.h2db
19:25:56 [SEVERE] at java.sql.DriverManager.getConnection(Unknown Source)
19:25:56 [SEVERE] at java.sql.DriverManager.getConnection(Unknown Source)
我使用此代码加载我的库:
File myJar = new File("./lib/h2.jar");
URL url = myJar.toURL();
URL[] urls = new URL[]{url};
URLClassLoader cl = new URLClassLoader(urls, this.getClass().getClassLoader());
Driver drv = (Driver) Class.forName("org.h2.Driver", true, cl).newInstance();
DriverManager.registerDriver(drv);
提前感谢您的帮助。