Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Dynamically loading JDBC drivers from a file path: No suitable driver found

843859Nov 12 2007 — edited Nov 20 2014
An application I'm working on allows the user to specify database drivers to use within the GUI. How do I accomplish this without adding the driver's JAR to the classpath?

The sample code below appears to be able to create a new instance of the driver class (in this case org.h2.Driver) but when I attempt to create a connection it bombs out with the SQLException: "No suitable driver found for jdbc:h2:file:D:/h2db/db"

Here's the output:
It's a JDBC driver
We loaded it properly...
java.sql.SQLException: No suitable driver found for jdbc:h2:file:D:/h2db/db
        at java.sql.DriverManager.getConnection(DriverManager.java:602)
        at java.sql.DriverManager.getConnection(DriverManager.java:185)
        at h2test.H2Testing.<init>(H2Testing.java:33)
        at h2test.H2Testing.main(H2Testing.java:52)
Here's the sample application..
package h2test;

import java.io.File;
import java.net.*;
import java.sql.*;

public class H2Testing {

    public H2Testing() {
        try {
            URL[] urls = {new File("d:\\h2.jar").toURI().toURL()};

            URLClassLoader jarloader = new URLClassLoader(urls, this.getClass().getClassLoader()); // parent*/
            Class clazz = jarloader.loadClass("org.h2.Driver");

            // Create a new instance to load the driver and register it with JDBC
            if (java.sql.Driver.class.isAssignableFrom(clazz)) {
                System.out.println("It's a JDBC driver");
                
                // Create a new instance to load the driver and register it with JDBC
                // This is equivalent to Class.forName
                clazz.newInstance();
                System.out.println("We loaded it properly...");
                
                Connection conn = DriverManager.getConnection("jdbc:h2:file:D:/h2db/db", "sa", "");
                System.out.println("We made a connection to the db");
                
                // Close the connection
                try { conn.close(); } catch (SQLException e) {}
            } else {
                System.out.println("Class " + clazz + " is not a valid JDBC driver class");
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new H2Testing();
    }
}
Thank you!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 10 2007
Added on Nov 12 2007
1 comment
799 views