• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

URLCLassLoader

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am using a URlCLassLoader's loadClass() method to load a driver and create an instance.AS far as the loadingn is concerned , it is without an error , but once i used the DriverManager's getConnection function , i get an error no suitable driver...
and i am pretty much sure that the connectionString which i am using is correct , no problem in that...
Why is DriverManager unable to recognize the driver?..
i have the code here..
// To load the driver
URL driverUrl = new URL("file:"+driverJarPath);
URL[] urlArray = new URL[] {driverUrl};
ClassLoader driverLoader = new URLClassLoader(urlArray);
Object driverClass = driverLoader.loadClass(dbDriver).newInstance();
// To get the connection
and..
dbConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jumpman","supervisor","supervisor");
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would you please show us the error message?
 
Natraj Gudla
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please find the error Message:...
java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getConnection(DriverManager.java:537)
at java.sql.DriverManager.getConnection(DriverManager.java:177)
at com.geindustrials.jmdboperations.SqlOperations.checkConnection(SqlOperations.java:163)
at com.geindustrials.jmdboperations.SqlOperations.start(SqlOperations.java:97)
do you need any more information?..please inform me?..
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not certain this is the problem, but it could be....
When the DriverManager loads classes, it effectively loads them against a specific ClassLoader. If you have your delegation incorrect in your loadClass method, you could have two versions of the DriverManager, one against the URLClassLoader and the other in the bootloader.
If this is the case, the driver gets registered against the DriverManager from the URLClassLoader, but then the call to find a valid driver occurs against the DriverManager in the bootloader - hence 'No suitable driver'
Dave
 
Natraj Gudla
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dave,
You are exactly right....I was actually looking into the code
of java.sql.DriverManager since yesterday , even i figured out the same
thing as you have stated..
In this case what is that i can do to solve the problem...
can you help me?..I have an urgent assignment...
I am also giving my code...

URL driverUrl = new URL("file:"+driverJarPath);
URL[] urlArray = new URL[] {driverUrl};
ClassLoader driverLoader = new URLClassLoader(urlArray);
Class driverClass = driverLoader.loadClass(dbDriver);
DriverManager.registerDriver((Driver)driverClass.newInstance());

regards
Natraj
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is not the recommended way to register Drivers, you should just be able to call Class.forName() or equivelent and allow the Driver to register itself as the Class is loaded.
The thing that confuses me is that the constructor you are using for URLClassLoader is supposed to delegate correctly by default. From the JavaDocs:

Constructs a new URLClassLoader for the specified URLs using the default delegation parent ClassLoader.


Sounds like what we want, doesn't it? ie when Classes loaded by the URLClassLoader need access to the DriverManager, it delegates to the defalt, which should be the bootloader.
Maybe try to explicity specify the parent ClassLoader as the one that manages the DriverManager
ie

Failing this, in your original code, I'd try DriverManager.getDrivers and find out for sure if the correct DriverManager is loading the Drivers specified by the URLClassLoader.
There isn't a programatic way to find the Classes loaded by a ClassLoader, but running it with java -verbose:class may help, except that it doesn't say which ClassLoader loads the class. The DriverManager may show up as being loaded twice though.
Hope this helps.
Dave.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, found it.
See this link:
http://www.kfu.com/~nsayer/Java/dyn-jdbc.html
Basically (quoting from the link)

"But why not use something like URLClassLoader and the overload of class.forName() that lets you specify the ClassLoader?" Because the DriverManager will refuse to use a driver not loaded by the system ClassLoader.


The article also has a work-around for you.
Dave
 
Natraj Gudla
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dav,
Got it...the URL which you gave helped me..that was a good one indeed.
a nice little conceptual things..
thanks for all your help,
Bye
 
Montana has cold dark nights. Perfect for the heat from incandescent light. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic