I have a plugin that connects to database. I don't want to bundle the jar for the driver in my plugin. Instead i want to take it from the users project and load it in run-time. Below is the code I am using to achieve the same. I get the error "No suitable driver found......" Please point out what could be wrong.
jPrj -- is the IJavaProject object,of the project from where i want to load the driver class. databaseType -- "mysql"
final String[] classPathEntries = JavaRuntime.computeDefaultRuntimeClassPath(jPrj);
for (int i = 0; i < classPathEntries.length; i++) {
final String entry = classPathEntries[i];
if (entry.contains(databaseType)) {
final IPath path = new Path(entry);
final URL url = path.toFile().toURI().toURL();
urlList.add(url); //the value of url at this point is-- file:/C:/.../mysql-connector-java-5.1.15-bin.jar -- the path on my disk
final ClassLoader parentClassLoader = jPrj.getClass().getClassLoader();
final URL[] urls = urlList.toArray(new URL[urlList.size()]);
final URLClassLoader classLoader = new URLClassLoader(urls, parentClassLoader);
try {
final ClassLoader loader = new URLClassLoader(urls);
loader.loadClass("com.mysql.jdbc.Driver");
Class<?> clazz = loader.loadClass("java.sql.DriverManager");
} catch (final ClassNotFoundException ex) {
ex.printStackTrace();
}
break;
}
}
DriverManager.getConnection("jdbc:mysql://localhost:port/myDB", myuser, mypaswrd); //-- get exception from here
I m not sure how to use clazz for the DriverManager.getConnection