DriverManager.registerDriver not working for me
I want to create a java application where i want to connect to a mysql database and supply the driver class/jar at runtime.
I tried this:
DriverManager.setLogStream(System.out);
String strJar = "G:/mysqlConnectors/mysql-connector-java-5.1.14-bin.jar";
File file = new File(strJar);
if(file.exists()) {
URL jarfile = new URL("jar", "","file:" + file.getAbsolutePath()+"!/");
System.out.println(jarfile);
ClassLoader cl = URLClassLoader.newInstance(new URL[] {jarfile });
clas = cl.loadClass("com.mysql.jdbc.Driver");
Class<? extends Driver> driverClass = clas.asSubclass(Driver.class);
System.out.println(driverClass.getName());
DriverManager.registerDriver(driverClass.newInstance());
System.out.println("********"+System.getProperty("jdbc.drivers"));
DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "pranay"); //this line gives exception.
} else {
System.out.println("Jar File not found in path: "+strJar);
}
the last line DriverManager.getConnection gives me exception.
Output of the code is:
jar:file:G:\mysqlConnectors\mysql-connector-java-5.1.14-bin.jar!/
com.mysql.jdbc.Driver
JdbcOdbcDriver class loaded
registerDriver: driver[className=sun.jdbc.odbc.JdbcOdbcDriver,sun.jdbc.odbc.JdbcOdbcDriver@60aeb0]
DriverManager.initialize: jdbc.drivers = null
JDBC DriverManager initialized
registerDriver: driver[className=com.mysql.jdbc.Driver,com.mysql.jdbc.Driver@16caf43]
registerDriver: driver[className=com.mysql.jdbc.Driver,com.mysql.jdbc.Driver@66848c]
********null
DriverManager.getConnection("jdbc:mysql://localhost:3306/test")
trying driver[className=sun.jdbc.odbc.JdbcOdbcDriver,sun.jdbc.odbc.JdbcOdbcDriver@60aeb0]
*Driver.connect (jdbc:mysql://localhost:3306/test)
skipping: driver[className=com.mysql.jdbc.Driver,com.mysql.jdbc.Driver@16caf43]
skipping: driver[className=com.mysql.jdbc.Driver,com.mysql.jdbc.Driver@66848c]
getConnection: no suitable driver found for jdbc:mysql://localhost:3306/test
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.global.pranay.multijar.MultiJar.testConnection(MultiJar.java:35)
at com.global.pranay.multijar.MultiJar.testSingleClassLoader(MultiJar.java:63)
at com.global.pranay.multijar.MultiJar.main(MultiJar.java:48)
SQLException: SQLState(08001)
Can you please help what is the problem?
Thanks in advance.
Subject
Written By
Posted
DriverManager.registerDriver not working for me
December 22, 2010 06:48PM
Sorry, you can't reply to this topic. It has been closed.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion
of Oracle or any other party.