Lately, I have worked to develop Java application that should need to be loaded JDBC driver on it's runtime.
By default, Java is providing solution of class loading method with called ClassLoader class.
But JDBC driver of third-party vender is very sensitive and fussy to use.
If you would approach it with general java class loading method, You might be failed about your goal. Especially MySQL driver don't work and do output message "No suitable driver found". As my experience until now, MySQL Driver only worked to do wrong like that, Maybe other drivers could be working wrong like that.
Nevertheless, I will show you about general usage of Java class loading. Generally, JDBC class loading code on runtime is down below.
public static Connection getConnection(File jarFile) throws MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
URL jarUrl = jarFile.toURI().toURL();
URLClassLoader loader = new URLClassLoader(new URL[]{jarUrl}, ClassLoader.getSystemClassLoader());
Class clazz = Class.forName("com.mysql.jdbc.Driver", true, loader);
Properties prop = new Properties();
prop.put("user", "admin");
prop.put("password", "1234");
Connection conn = (Connection)DriverManager.getConnection("jdbc:mysql://192.168.1.157:3306/mysql", prop);
return conn;
}
Upper code snippet will fail with error message on console that is down below.
D:\Projects\chaos-commons> cmd /C "c:\Users\chaos\.vscode\extensions\vscjava.vscode-java-debug-0.31.0\scripts\launcher.bat C:\jdk-11\bin\java.exe -Dfile.encoding=UTF-8 @C:\Users\chaos\AppData\Local\Temp\cp_f58mz4nw79996julgmkyp9ycf.argfile org.chaostocosmos.commons.loader.JobClassLoader "
Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:mysql://192.168.1.157:3306/mysql
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:189)
at org.chaostocosmos.commons.loader.JobClassLoader.getConnection(JobClassLoader.java:75)
at org.chaostocosmos.commons.loader.JobClassLoader.main(JobClassLoader.java:92)
In above code, I create URLClassLoader class from path of jar file, load JDBC driver by Class.forName method with the URLClassLoader object and then have gotten Connection object by DriverManager object.
Now we are going to see the code sample that is working well. It has to create Driver object by URLClassLoader's loadClass method and then getting Connection object with connect method of Driver object.
The code of that be completed is down below.
public static Connection getConnectionByDriver(File jarFile) throws MalformedURLException, InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
URL jarUrl = jarFile.toURI().toURL();
URLClassLoader loader = new URLClassLoader(new URL[]{jarUrl}, ClassLoader.getSystemClassLoader());
Driver driver = (Driver) loader.loadClass("com.mysql.jdbc.Driver").newInstance();
Properties prop = new Properties();
prop.put("user", "admin");
prop.put("password", "1234");
Connection conn = driver.connect("jdbc:mysql://192.168.1.157:3306/mysql", prop);
return conn;
}
As my experience, Oracle driver was working well with general Java class loading method but MySQL was not. Any other DB driver have not been tested yet by me.
If you have been found something about it of other drivers, Let all of DEV know :) Thanks.
Top comments (0)