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.
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.
How can one do it better?