how to register jdbc driver into DriverManager?
I want to connect mysql via jdbc. I've appended mysql connector to classpath and import com.mysql.cj.jdbc.Driver, then import java.sql.DriverManager and register mysql Driver with method DriverManager.registerDriverSync. but I can't find any driver by using DriverManager.getDeiversSync,and can't find suitable driver for jdbc url either. how can I use jdbc with node - java-bridge?
I just tried connecting to a mysql database and ran into the same issue. I'll try to figure out what's wrong and get back to you once I know why this isn't working.
I've looked further into this and found the following:
-
DriverManager.getDriver(String url)usesReflection.getCallerClass()to get the caller class and check if the class loader of the caller class is able to load the driver - Since we are calling java from node, there is no caller class,
Reflection.getCallerClass()will returnnull - If no caller class is passed,
Class.forNameSync("com.mysql.cj.jdbc.Driver", true, null)will be called to check if the driver exists - If
nullis passed as a third argument toClass.forNameSync, the bootstrap class loader will be used to search for the driver and won't find anything since the bootstrap class loader only contains the java runtime classes - This will cause
DriverManager.getDriverto not find an appropriate driver
The easiest fix for this would be to encapsulate your java logic in a java class, load this class and call the methods from it using java-bridge. This will ensure Reflection.getCallerClass() to return a proper java class and since java-bridge sets the class loader properly, the driver will be found.
Closing due to inactivity