简体   繁体   中英

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?

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.

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.

You need to add a Classpath reference in the manifest. Follow these simple steps:

add a folder "lib" to your application

place "mysql-connector-java-5.1.18-bin" in lib

now open your "MANIFEST.MF" and go to tab "RUNTIME"

on bottom right, you would see "classpath" ; click "Add"

now add the folder lib [created in step 1] along with the jar file

in this way, whenever a runtime EclipseApplication /OSGi Application is started this jar file is exported along too. So the connectivity would then be available there too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM