I'm having an issue with some of my code, I've searched and tried everything I know of, without any luck.
Scenario:
If it doesn't exist, the application downloads the JDBC driver and adds it to a ClassLoader like so: (note: storageDataManager is a class of mine I use for SQL methods)
File h2Driver = new File(directory.toString() + File.separator + "lib" + File.separator + "h2.jar");
if (h2Driver.exists()) {
URL[] url = new URL[0];
try {
url = new URL[]{h2Driver.toURI().toURL()};
storageDataManager.setClassLoader(new URLClassLoader(url));
} catch (MalformedURLException ignore) {}
}
When the storageDataManager runs the first query, it tries to connect with the specified driver, and if it does have a ClassLoader, it uses ClassLoader instead:
if (getClassLoader() != null) {
getLogging().debug("Loading custom class loader for H2 driver: " + getClassLoader().toString());
Driver driver = (Driver) Class.forName("org.h2.Driver", true, getClassLoader()).newInstance();
getLogging().debug("Loaded H2 driver: " + driver.toString() + " - " + driver.getMinorVersion() + " - " + driver.getMajorVersion());
DriverManager.registerDriver(driver);
} else {
getLogging().debug("Loading H2 driver.");
Class.forName("org.h2.Driver");
}
outputDrivers();
this.con = DriverManager.getConnection(this.url, this.username, this.password);
break;
When I run the application I get this error:
"*No suitable driver found for jdbc:h2:plugins\Odin\data\OdinStorage;AUTO_RECONNECT=TRUE*"
Here's the full log:
[Debug] Loading custom class loader for H2 driver: java.net.URLClassLoader@3bf3d5f4
[Debug] Loaded H2 driver: org.h2.Driver@67257ce8 - 3 - 1
[Debug] Checking DriverManager drivers.
[Debug] Found driver #1: sun.jdbc.odbc.JdbcOdbcDriver
[Debug] Found driver #2: com.mysql.jdbc.Driver
[Debug] Found 2 drivers in DriverManager.
--------------------------- STACKTRACE ERROR ---------------------------
Class name: java.sql.DriverManager
Error message: No suitable driver found for jdbc:h2:plugins\Odin\data\OdinStorage;AUTO_RECONNECT=TRUE
Error cause: null
File name: null
Function name: getConnection
Error line: -1
--------------------------- STACKTRACE START ---------------------------
java.sql.DriverManager.getConnection(Unknown Source)
java.sql.DriverManager.getConnection(Unknown Source)
com.craftfire.commons.managers.DataManager.connect(DataManager.java:756)
com.craftfire.commons.managers.DataManager.executeQuery(DataManager.java:526)
com.craftfire.odin.managers.StorageManager.checkInventoryDatabase(StorageManager.java:65)
com.craftfire.odin.managers.StorageManager.checkDatabases(StorageManager.java:56)
com.craftfire.odin.managers.StorageManager.<init>(StorageManager.java:34)
com.craftfire.odin.managers.OdinManager.loadDatabases(OdinManager.java:206)
com.craftfire.odin.managers.OdinManager.init(OdinManager.java:75)
com.craftfire.odin.layer.bukkit.Odin.onEnable(Odin.java:63)
org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:374)
org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:381)
org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:266)
org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:248)
org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:200)
net.minecraft.server.ServerConfigurationManagerAbstract.<init>(ServerConfigurationManagerAbstract.java:50)
net.minecraft.server.ServerConfigurationManager.<init>(SourceFile:11)
net.minecraft.server.DedicatedServer.init(DedicatedServer.java:105)
net.minecraft.server.MinecraftServer.run(MinecraftServer.java:377)
net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
---------------------------- STACKTRACE END ----------------------------
My question is then, why doesn't the driver show up in the DriverManager.getDrivers()?
Note: I do not wish to add the library to the CLASSPATH, which is why I need to find a solution to the issue I described above.
And how can I solve this issue? I simply need to load the H2 JDB driver from a jar.
I have also tried this:
Could anyone provide me with a solution to this?
Thanks!
In brief, we can say that such an error occurs when no JDBC JAR file is added to the classpath of Java. Just we need to add the JAR file to the classpath and then execute the code. The code will hopefully get executed with success.
Click Windows → type H2 Console → Click H2 console icon. Connect to the URL http://localhost:8082. At the time of connecting, the H2 database will ask for database registration as shown in the following screenshot.
Connecting to a Database using JDBC This code opens a connection (using DriverManager. getConnection() ). The driver name is "org. h2.
This error occurs if JDBC is not able to find a suitable driver for the URL format passed to the getConnection() method e.g. "jdbc:mysql://" in our case. In order to solve this error, you need the MySQL JDBC driver like mysql-connector-java-5.1. 36. jar in your classpath.
In my case the error was in the maven scope
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
<scope>test</scope>
</dependency>
Once you remove it or change it to a visible scope it worked
I had the same problem. The h2 driver was configured in the pom.xml with
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.193</version>
</dependency>
Because I'm using Java 6 in my project (don't ask why ;-)) but the h2-1.4.193.jar from the Maven Repository depends on Java 7, this driver version could not be used.
Changing the pom.xml to use h2-1.4.190.jar solved the problem for me.
See also issue #300 in the h2database git project.
According to the Oracle docs: http://docs.oracle.com/cd/E19501-01/819-3659/beadf/index.html
Classloaders delegate classloading to child classloaders, searching for the class on the classpath. However, the URLClassloader you used to load your library is not visible to the system or bootstrap hierarchy, so it can not find the class (despite it being loaded, albeit in another castl...classloader).
The easiest solution is to replace your system classloader with a URLClassloader and use addUrl(...path...) to load your library, as this answer suggests: How should I load Jars dynamically at runtime?
Hibernate properties set url value to: jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
driverClassName:
<property name="driverClassName">
<value>org.hibernate.dialect.H2Dialect</value>
Add H2 driver to pom:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.166</version>
</dependency>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With