fedora.utilities
Class DriverShim

java.lang.Object
  extended by fedora.utilities.DriverShim
All Implemented Interfaces:
java.sql.Driver

public class DriverShim
extends java.lang.Object
implements java.sql.Driver

Allows one to load a JDBC driver at runtime. java.sql.DriverManager will refuse to use a driver not loaded by the system ClassLoader. The workaround for this is to use a shim class that implements java.sql.Driver. This shim class will do nothing but call the methods of an instance of a JDBC driver that is loaded dynamically. This works because DriverShim is loaded by the system class loader, and DriverManager doesn't care that it invokes a class that wasn't. Note that we must perform the registration on the instance ourselves. See the utility method, loadAndRegister and the command-line test below. Adapted from http://www.kfu.com/~nsayer/Java/dyn-jdbc.html


Constructor Summary
DriverShim(java.sql.Driver d)
          Constructs a DriverShim over the given driver in order to make it look like it came from this classloader.
 
Method Summary
 boolean acceptsURL(java.lang.String u)
          
 java.sql.Connection connect(java.lang.String u, java.util.Properties p)
          
 int getMajorVersion()
          
 int getMinorVersion()
          
 java.sql.DriverPropertyInfo[] getPropertyInfo(java.lang.String u, java.util.Properties p)
          
 boolean jdbcCompliant()
          
static void loadAndRegister(java.io.File driverJarFile, java.lang.String driverClassName)
          Loads the driver from the given jar file and registers it with the driver manager.
static void loadAndRegister(java.net.URL driverURL, java.lang.String driverClassName)
           
static void main(java.lang.String[] args)
          Command-line test.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DriverShim

public DriverShim(java.sql.Driver d)
Constructs a DriverShim over the given driver in order to make it look like it came from this classloader.

Method Detail

loadAndRegister

public static final void loadAndRegister(java.io.File driverJarFile,
                                         java.lang.String driverClassName)
                                  throws java.lang.Exception
Loads the driver from the given jar file and registers it with the driver manager.

Throws:
java.lang.Exception

loadAndRegister

public static final void loadAndRegister(java.net.URL driverURL,
                                         java.lang.String driverClassName)
                                  throws java.lang.Exception
Throws:
java.lang.Exception

acceptsURL

public boolean acceptsURL(java.lang.String u)
                   throws java.sql.SQLException

Specified by:
acceptsURL in interface java.sql.Driver
Throws:
java.sql.SQLException

connect

public java.sql.Connection connect(java.lang.String u,
                                   java.util.Properties p)
                            throws java.sql.SQLException

Specified by:
connect in interface java.sql.Driver
Throws:
java.sql.SQLException

getMajorVersion

public int getMajorVersion()

Specified by:
getMajorVersion in interface java.sql.Driver

getMinorVersion

public int getMinorVersion()

Specified by:
getMinorVersion in interface java.sql.Driver

getPropertyInfo

public java.sql.DriverPropertyInfo[] getPropertyInfo(java.lang.String u,
                                                     java.util.Properties p)
                                              throws java.sql.SQLException

Specified by:
getPropertyInfo in interface java.sql.Driver
Throws:
java.sql.SQLException

jdbcCompliant

public boolean jdbcCompliant()

Specified by:
jdbcCompliant in interface java.sql.Driver

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Command-line test. Dynamically loads the driver given at the command line, opens a connection, and get the row count of the given table to prove it worked. Usage: java -cp . DriverShim someDriver.jar org.SomeDriver jdbcURL name pwd tbl

Throws:
java.lang.Exception