Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading JDBC Driver at Runtime

I'm using the following code to load a driver class:

public class DriverLoader extends URLClassLoader {

    private DriverLoader(URL[] urls) {
        super(urls);
        File driverFolder = new File("driver");
        File[] files = driverFolder.listFiles();
        for (File file : files) {
            try {
                addURL(file.toURI().toURL());
            } catch (MalformedURLException e) {
            }
        }
    }


    private static DriverLoader driverLoader;


    public static void load(String driverClassName) throws ClassNotFoundException {
        try {
            Class.forName(driverClassName);
        } catch (ClassNotFoundException ex) {
            if (driverLoader == null) {
                URL urls[] = {};
                driverLoader = new DriverLoader(urls);
            }
            driverLoader.loadClass(driverClassName);
        }
    }
}

Although the class loads fine I can't establish a Database connection (No suitable driver found for ...) no matter which driver I try.

I assume this is because I'm not loading the driver class using Class.forName (which wouldn't work since I'm using my own ClassLoader). How can I fix this?

like image 954
jhasse Avatar asked Apr 15 '11 09:04

jhasse


People also ask

What is the efficient way of loading the driver?

forName() The most common approach to register a driver is to use Java's Class. forName() method, to dynamically load the driver's class file into memory, which automatically registers it.

Which one is correct function for loading driver in JDBC?

Explanation: There are two ways to load a database driver in JDBC: By using the registerDriver() Method: To access the database through a Java application, we must register the installed driver in our program. We can do this with the registerDriver() method that belongs to the DriverManager class.


2 Answers

You need to create an instance of the driver class before you can connect:

Class drvClass = driverLoader.loadClass(driverClassName);
Driver driver = drvClass.newInstance();

Once you have the instance you can either use that instance to connect:

Properties props = new Properties();
props.put("user", "your_db_username");
props.put("password", "your_db_password");
Connection con = driver.connect("jdbc:postgresql:...", props);

As an alternative, if you want to keep using DriverManager you must register the driver with the DriverManager manually:

DriverManager.registerDriver(driver);

Then you should be able to use the DriverManager to establis a connection.

If I recall it correctly there was a problem with the DriverManager refusing to connect if the driver itself was not loaded by the same classloader as the DriverManager. If that (still) is the case, you need to use Driver.connect() directly.

like image 156
a_horse_with_no_name Avatar answered Oct 06 '22 11:10

a_horse_with_no_name


You should establish connection in a class loaded by your DriverLoader. So, load the connection establishment code using DriverLoader and then call JDBC from it.

like image 45
Dmitry Negoda Avatar answered Oct 06 '22 11:10

Dmitry Negoda