This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JDBC and the fly likes loading JDBC driver from a Jar (not in classpath) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "loading JDBC driver from a Jar (not in classpath)" Watch "loading JDBC driver from a Jar (not in classpath)" New topic
Author

loading JDBC driver from a Jar (not in classpath)

Andris Jekabsons
Ranch Hand

Joined: Jan 20, 2004
Posts: 82
How can one load a JDBC driver from a Jar file that is not in the classpath?
Currently the Jar's (with the db driver) location is indicated in the run script. However, the application should be deployed as a Jar file, so that the user could start it just by executing that Jar file. The problem is that the user could use any jdbc driver that could be located anywhere on the machine, so I cannot hardcode it, and have to ask the user to provide this information.
How can I load the driver after the user has given the Jar location and driver name (it is a Swing application).
Right now the driver is loaded and connection established by:

I was trying to use a URLClassLoader, but aparently am missing some step:

Thanks for any hints.
Andris Jekabsons
Ranch Hand

Joined: Jan 20, 2004
Posts: 82
Should have googled more precisely, as the solution was not far away.
As explained here:
http://forum.java.sun.com/thread.jspa?threadID=300557&start=15&tstart=0
all I was missing was creating the driver instance:

Now I just have to look more into the Driver.getPropertyInfo() for more ways to gathering connection information for various kinds of drivers.
[ April 12, 2006: Message edited by: Andris Jekabsons ]
stu derby
Ranch Hand

Joined: Dec 15, 2005
Posts: 333
Thanks for posting the followup, I was curious...
 
Consider Paul's rocket mass heater.
 
subject: loading JDBC driver from a Jar (not in classpath)
 
Similar Threads
dynamically loading a class file from a jar file
where to put image jar files
URLClassLoader for loading JDBC driver...
Regarding ClassLoader concept
Class.forName()