B4J Code Snippet Load A JDBC Driver Dynamically At Run Time Using Inline Java

I am currently working on a project that has to support multiple types of databases. So that meant many additional Jars added to the project. I use SQuirrel SQL Client quite often which is written in Java and that loads JDBC drivers dynamically. So some Googling on how this is done and I came up with this:

B4X:
#Region Project Attributes 
    #MainFormWidth: 600
    #MainFormHeight: 600 
    'No Addition Jar for MYSQL JDBC Driver!
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim oSQL As SQL
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'Location of JDBC Driver to be Dynamically Loaded
    Dim JDBCDRiverFile As String = "C:\b4j\mysql-connector-java-5.1.39-bin.jar"
    Dim cityResult As ResultSet
    If LoadJAR(JDBCDRiverFile) = True Then
        If oSQL.IsInitialized Then
            Log ("JDBC Driver Loaded")
            cityResult =oSQL.ExecQuery("select * from city")
            Do  while cityResult.NextRow
                Log(cityResult.GetString("city"))
            Loop
       
        End If
       
    End If
    'Test Connection
   
    MainForm.Show
End Sub


'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub LoadJAR (DriverFile As String) As Boolean
    Dim JO As JavaObject = GetJO(Me)
    JO.RunMethod("loadJDBCLibrary",Array As String (DriverFile))
    Try
        oSQL.Initialize2("com.mysql.jdbc.Driver","jdbc:mysql://localhost/sakila?autoReconnect=true&useSSL=false","KeirS","MyPassword")
        Return True

    Catch
        Log(LastException)
        Return False
    End Try
   
   
   
   
End Sub

Sub GetJO(O As JavaObject)  As JavaObject
    Return O
End Sub
    #if Java
     import java.lang.reflect.*;
     import java.io.File;
     import java.net.URLClassLoader;
     import java.net.URL;
      import java.util.Arrays;
     
    public static synchronized void loadJDBCLibrary(String  jarFile) 
     
    {
     
        try {
            java.io.File driverJDBC  = new  java.io.File(jarFile);
            java.net.URLClassLoader systemLoader = (java.net.URLClassLoader)ClassLoader.getSystemClassLoader();
            java.net.URL driverJDBCURL =  driverJDBC.toURI().toURL();
            for (java.net.URL loadedJars : java.util.Arrays.asList(systemLoader.getURLs())){
                if (loadedJars.equals(driverJDBCURL)){
                    return;
                }
            }
            java.lang.reflect.Method methodAddURL = java.net.URLClassLoader.class.getDeclaredMethod("addURL", new Class[]{java.net.URL.class});
            methodAddURL.setAccessible(true); 
            methodAddURL.invoke(systemLoader, new Object[]{driverJDBCURL});
        } catch (Exception e){
          throw new RuntimeException(e);
           
        }
    }
   

   
    #End If
 
Top