Skip to main content
added 7 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Loading the jdbcJDBC driver jar dynamically from external location

I have a small project in which I get the URL, username, password, and driver from a text file and I have to fire a query on the database by dynamically reading the jar file in a predefined directory, register the jar, get a connection object. Possibly and possibly de-register it. Each query may talk to a different database schema or even DBMS. The directory consists of all JDBC driver jars. I need to pick one based on the driver class name that I get from the previously mentioned text file.

Loading the jdbc driver jar dynamically from external location

I have a small project in which I get the URL, username, password, driver from a text file and I have to fire a query on the database by dynamically reading the jar file in a predefined directory, register the jar, get a connection object. Possibly de-register it. Each query may talk to a different database schema or even DBMS. The directory consists of all JDBC driver jars. I need to pick one based on the driver class name that I get from the previously mentioned text file.

Loading the JDBC driver jar dynamically from external location

I have a small project in which I get the URL, username, password, and driver from a text file and I have to fire a query on the database by dynamically reading the jar file in a predefined directory, register the jar, get a connection object and possibly de-register it. Each query may talk to a different database schema or even DBMS. The directory consists of all JDBC driver jars. I need to pick one based on the driver class name that I get from the previously mentioned text file.

deleted 181 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Loading the jdbc driver jar dynamically from external location. Any memory issues and performance issues?

I have a small project in which I get the urlURL, username, password, driver from a text file and I have to fire a query on the database by dynamically reading the jar file in a predefined directory, register the jar, get a connection object. Possibly de-register it. Each query may talk to a different database schema or even dbmsDBMS. The directory consists of all jdbcJDBC driver jars. I need to pick one based on the driver class name that I get from the previously mentioned text file.

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String dbUrl = "jdbc:mysql://localhost:3306/test";
            String userName = "foo";
            String password = "bar";
            String driverName = "com.mysql.jdbc.Driver";
    
            final URLClassLoader loader = (URLClassLoader) ClassLoader.getSystemClassLoader();
            String jar = "D:\\Drivers\\mysql-connector-java-5.1.6.jar";
    
            System.out.println(this.getClass().getClassLoader().getClass() + " is the class loader");
            System.out.println("Before loading the drivers in driver manager are ");
            listDrivers();
            try {
                Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
                method.setAccessible(true);
                method.invoke(loader, new File(jar).toURI().toURL());
    
                Class<?> classToLoad = Class.forName(driverName, true, loader);
                Driver driver = (Driver) classToLoad.newInstance();
                DriverManager.registerDriver(new DriverShim(driver));
                Connection connection = DriverManager.getConnection(dbUrl, userName, password);
                System.out.println("is Connection null " + (connection == null) + ". After loading the drivers in driver manager are ");
                //Do business specific action
                listDrivers();
                DriverManager.deregisterDriver(driver);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    
        private void listDrivers() {
            java.util.Enumeration enumeration = java.sql.DriverManager.getDrivers();
            while (enumeration.hasMoreElements()) {
                Driver driverAsObject = (Driver) enumeration.nextElement();
                System.out.println("Driver: " + driverAsObject + ":" + driverAsObject.getMajorVersion() + ":" + driverAsObject.getMinorVersion());
            }
        }

The dependency DriverShimDriverShim is obtained from here http://www.kfu.com/~nsayer/Java/dyn-jdbc.htmlhere.

Loading the jdbc driver jar dynamically from external location. Any memory issues and performance issues?

I have a small project in which I get the url, username, password, driver from a text file and I have to fire a query on the database by dynamically reading the jar file in a predefined directory, register the jar, get a connection object. Possibly de-register it. Each query may talk to a different database schema or even dbms. The directory consists of all jdbc driver jars. I need to pick one based on the driver class name that I get from the previously mentioned text file.

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String dbUrl = "jdbc:mysql://localhost:3306/test";
            String userName = "foo";
            String password = "bar";
            String driverName = "com.mysql.jdbc.Driver";
    
            final URLClassLoader loader = (URLClassLoader) ClassLoader.getSystemClassLoader();
            String jar = "D:\\Drivers\\mysql-connector-java-5.1.6.jar";
    
            System.out.println(this.getClass().getClassLoader().getClass() + " is the class loader");
            System.out.println("Before loading the drivers in driver manager are ");
            listDrivers();
            try {
                Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
                method.setAccessible(true);
                method.invoke(loader, new File(jar).toURI().toURL());
    
                Class<?> classToLoad = Class.forName(driverName, true, loader);
                Driver driver = (Driver) classToLoad.newInstance();
                DriverManager.registerDriver(new DriverShim(driver));
                Connection connection = DriverManager.getConnection(dbUrl, userName, password);
                System.out.println("is Connection null " + (connection == null) + ". After loading the drivers in driver manager are ");
                //Do business specific action
                listDrivers();
                DriverManager.deregisterDriver(driver);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    
        private void listDrivers() {
            java.util.Enumeration enumeration = java.sql.DriverManager.getDrivers();
            while (enumeration.hasMoreElements()) {
                Driver driverAsObject = (Driver) enumeration.nextElement();
                System.out.println("Driver: " + driverAsObject + ":" + driverAsObject.getMajorVersion() + ":" + driverAsObject.getMinorVersion());
            }
        }

The dependency DriverShim is obtained from here http://www.kfu.com/~nsayer/Java/dyn-jdbc.html.

Loading the jdbc driver jar dynamically from external location

I have a small project in which I get the URL, username, password, driver from a text file and I have to fire a query on the database by dynamically reading the jar file in a predefined directory, register the jar, get a connection object. Possibly de-register it. Each query may talk to a different database schema or even DBMS. The directory consists of all JDBC driver jars. I need to pick one based on the driver class name that I get from the previously mentioned text file.

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String dbUrl = "jdbc:mysql://localhost:3306/test";
        String userName = "foo";
        String password = "bar";
        String driverName = "com.mysql.jdbc.Driver";

        final URLClassLoader loader = (URLClassLoader) ClassLoader.getSystemClassLoader();
        String jar = "D:\\Drivers\\mysql-connector-java-5.1.6.jar";

        System.out.println(this.getClass().getClassLoader().getClass() + " is the class loader");
        System.out.println("Before loading the drivers in driver manager are ");
        listDrivers();
        try {
            Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
            method.setAccessible(true);
            method.invoke(loader, new File(jar).toURI().toURL());

            Class<?> classToLoad = Class.forName(driverName, true, loader);
            Driver driver = (Driver) classToLoad.newInstance();
            DriverManager.registerDriver(new DriverShim(driver));
            Connection connection = DriverManager.getConnection(dbUrl, userName, password);
            System.out.println("is Connection null " + (connection == null) + ". After loading the drivers in driver manager are ");
            //Do business specific action
            listDrivers();
            DriverManager.deregisterDriver(driver);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    private void listDrivers() {
        java.util.Enumeration enumeration = java.sql.DriverManager.getDrivers();
        while (enumeration.hasMoreElements()) {
            Driver driverAsObject = (Driver) enumeration.nextElement();
            System.out.println("Driver: " + driverAsObject + ":" + driverAsObject.getMajorVersion() + ":" + driverAsObject.getMinorVersion());
        }
    }

The dependency DriverShim is obtained from here.

Source Link
phoenix
  • 277
  • 1
  • 3
  • 7

Loading the jdbc driver jar dynamically from external location. Any memory issues and performance issues?

I have a small project in which I get the url, username, password, driver from a text file and I have to fire a query on the database by dynamically reading the jar file in a predefined directory, register the jar, get a connection object. Possibly de-register it. Each query may talk to a different database schema or even dbms. The directory consists of all jdbc driver jars. I need to pick one based on the driver class name that I get from the previously mentioned text file.

I have written the following code so far. Does it has any memory issues or performance issues?

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String dbUrl = "jdbc:mysql://localhost:3306/test";
            String userName = "foo";
            String password = "bar";
            String driverName = "com.mysql.jdbc.Driver";
    
            final URLClassLoader loader = (URLClassLoader) ClassLoader.getSystemClassLoader();
            String jar = "D:\\Drivers\\mysql-connector-java-5.1.6.jar";
    
            System.out.println(this.getClass().getClassLoader().getClass() + " is the class loader");
            System.out.println("Before loading the drivers in driver manager are ");
            listDrivers();
            try {
                Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
                method.setAccessible(true);
                method.invoke(loader, new File(jar).toURI().toURL());
    
                Class<?> classToLoad = Class.forName(driverName, true, loader);
                Driver driver = (Driver) classToLoad.newInstance();
                DriverManager.registerDriver(new DriverShim(driver));
                Connection connection = DriverManager.getConnection(dbUrl, userName, password);
                System.out.println("is Connection null " + (connection == null) + ". After loading the drivers in driver manager are ");
                //Do business specific action
                listDrivers();
                DriverManager.deregisterDriver(driver);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    
        private void listDrivers() {
            java.util.Enumeration enumeration = java.sql.DriverManager.getDrivers();
            while (enumeration.hasMoreElements()) {
                Driver driverAsObject = (Driver) enumeration.nextElement();
                System.out.println("Driver: " + driverAsObject + ":" + driverAsObject.getMajorVersion() + ":" + driverAsObject.getMinorVersion());
            }
        }

The dependency DriverShim is obtained from here http://www.kfu.com/~nsayer/Java/dyn-jdbc.html.

How can one do it better?