0

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......" from line 3. The same line works fine when i use Class.forName("com.mysql.jdbc.Driver"); and include the jar in my plugin. Please point out what could be wrong.

/*urls = [file:/C:/veni/code/TestFC/lib/mysql-connector-java-5.1.15-bin.jar] */
final ClassLoader loader = new URLClassLoader(urls);  //1
loader.loadClass("com.mysql.jdbc.Driver");            //2
con = DriverManager.getConnection("jdbc:mysql://localhost/testfc",username,paswd); //3
Krishnaveni
  • 799
  • 2
  • 11
  • 32
  • 2
    Have a look at [How to use a JDBC driver from an arbitrary location](http://stackoverflow.com/questions/288828/how-to-use-a-jdbc-driver-from-an-arbitrary-location) – MadProgrammer Jun 24 '14 at 03:39
  • 1
    The issue is, the `DriverManager` is loaded in a different class loader from the `Driver`, but class loaders don't scan child class loaders for classes, they only scan parent class loaders – MadProgrammer Jun 24 '14 at 03:40
  • final ClassLoader loader = new URLClassLoader(urls); final Driver driver = (Driver) loader.loadClass(MYSQL_JDBC_DRIVER).newInstance(); DriverManager.registerDriver(driver); DriverManager.getDriver("jdbc:mysql://localhost/TestFc"); the last line gives error that driver not found....does that mean that the driver was not loaded? – Krishnaveni Jun 24 '14 at 10:09
  • Properties props = new Properties(); props.put("user", "your_db_username"); props.put("password", "your_db_password"); Connection con = driver.connect("jdbc:mysql:...", props); instead of using DriverManager, i tried the above code and it works. refer:http://stackoverflow.com/a/5674762/1081017 – Krishnaveni Jun 24 '14 at 10:23

0 Answers0