org.sadun.util.sql
Class DynamicConnectionFactory

java.lang.Object
  extended byorg.sadun.util.sql.DynamicConnectionFactory

public class DynamicConnectionFactory
extends java.lang.Object

This class allows to load JDBC drivers by specifiying their location at runtime, and to successively obtain connections.

Note that method loadDriver(String) must be used instead of Class.forName, and one of the getConnection(String) overloads to obtain a connection.

Using DriverManager directly will result in a "No suitable driver".

Example code follows:

  DynamicConnectionFactory factory = new DynamicConnectionFactory();
  factory.addClassPathEntry(..class path of the jdbc driver(s)...);
  factory.loadDriver(..driver class name...);
  Connection conn = factory.getConnection(..jdbc URL...);
 

Author:
Cristiano Sadun

Constructor Summary
DynamicConnectionFactory()
          Create a factory which uses the same class path as the system.
 
Method Summary
 void addClassPathEntry(java.lang.String entry)
          Add a class path entry.
 java.sql.Connection getConnection(java.lang.String jdbcUrl)
          Return a connection by using the given jdbc URL (as DriverManager)
 java.sql.Connection getConnection(java.lang.String jdbcUrl, java.util.Properties p)
          Return a connection by using the given jdbcURL and properties (as DriverManager)
 java.sql.Connection getConnection(java.lang.String jdbcUrl, java.lang.String user, java.lang.String pwd)
          Return a connection by using the given jdbcURL and username/password (as DriverManager)
 java.lang.Class loadDriver(java.lang.String driverClassName)
          Load a JDBC driver dynamically.
 void setClassPath(java.lang.String cp)
          Set the class path.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DynamicConnectionFactory

public DynamicConnectionFactory()
Create a factory which uses the same class path as the system. addClassPathEntry(String) can be successively used to extend the class path}.

Method Detail

addClassPathEntry

public void addClassPathEntry(java.lang.String entry)
Add a class path entry.

Parameters:
entry - the entry to add

setClassPath

public void setClassPath(java.lang.String cp)
Set the class path.

Parameters:
cp - the class path to set.

loadDriver

public java.lang.Class loadDriver(java.lang.String driverClassName)
                           throws java.lang.ClassNotFoundException
Load a JDBC driver dynamically.

Parameters:
driverClassName - the name of the driver
Returns:
the driver Class object
Throws:
java.lang.ClassNotFoundException - if the driver class does not exist

getConnection

public java.sql.Connection getConnection(java.lang.String jdbcUrl,
                                         java.util.Properties p)
                                  throws java.sql.SQLException
Return a connection by using the given jdbcURL and properties (as DriverManager)

Parameters:
jdbcUrl - the JDBC url to connect to
p - the connection properties
Returns:
a JDBC Connection
Throws:
java.sql.SQLException - if a connection cannot be established

getConnection

public java.sql.Connection getConnection(java.lang.String jdbcUrl)
                                  throws java.sql.SQLException
Return a connection by using the given jdbc URL (as DriverManager)

Parameters:
jdbcUrl - the JDBC url to connect to
Returns:
a JDBC Connection
Throws:
java.sql.SQLException - if a connection cannot be established

getConnection

public java.sql.Connection getConnection(java.lang.String jdbcUrl,
                                         java.lang.String user,
                                         java.lang.String pwd)
                                  throws java.sql.SQLException
Return a connection by using the given jdbcURL and username/password (as DriverManager)

Parameters:
jdbcUrl - the JDBC url to connect to
user - the user
Returns:
a JDBC Connection
Throws:
java.sql.SQLException - if a connection cannot be established