org.javagen.agile.db.util
Class DriverManagerDataSource

java.lang.Object
  extended by org.javagen.agile.db.util.DriverManagerDataSource
All Implemented Interfaces:
DataSource

public class DriverManagerDataSource
extends Object
implements DataSource

A generic way to access database drivers given the driver class name, url, username and password.

Compiled from code found in the Spring Framework.

Author:
Richard Easterling

Constructor Summary
DriverManagerDataSource()
          Constructor for bean-style configuration.
DriverManagerDataSource(String url)
          Create a new DriverManagerDataSource with the given JDBC URL, not specifying a username or password for JDBC access.
DriverManagerDataSource(String url, String username, String password)
          Create a new DriverManagerDataSource with the given standard DriverManager parameters.
DriverManagerDataSource(String driverClassName, String url, String username, String password)
          Create a new DriverManagerDataSource with the given standard DriverManager parameters.
 
Method Summary
 Connection getConnection()
          This implementation delegates to getConnectionFromDriverManager, using the default username and password of this DataSource.
 Connection getConnection(String username, String password)
          This implementation delegates to getConnectionFromDriverManager, using the given username and password.
protected  Connection getConnectionFromDriverManager()
          Get a Connection from the DriverManager, using the default username and password of this DataSource.
protected  Connection getConnectionFromDriverManager(String url, Properties props)
          Getting a connection using the nasty static from DriverManager is extracted into a protected method to allow for easy unit testing.
protected  Connection getConnectionFromDriverManager(String username, String password)
          Build properties for the DriverManager, including the given username and password (if any).
 Properties getConnectionProperties()
          Return the connection properties to be passed to the DriverManager, if any.
static ClassLoader getDefaultClassLoader()
           
 String getDriverClassName()
          Return the JDBC driver class name, if any.
 int getLoginTimeout()
          Returns 0: means use default system timeout.
 PrintWriter getLogWriter()
          LogWriter methods are unsupported.
 String getPassword()
          Return the JDBC password to use for accessing the DriverManager.
 String getUrl()
          Return the JDBC URL to use for accessing the DriverManager.
 String getUsername()
          Return the JDBC username to use for accessing the DriverManager.
 void setConnectionProperties(Properties connectionProperties)
          Specify arbitrary connection properties as key/value pairs, to be passed to the DriverManager.
 void setDriverClassName(String driverClassName)
          Set the JDBC driver class name.
 void setLoginTimeout(int timeout)
           
 void setLogWriter(PrintWriter pw)
          LogWriter methods are unsupported.
 void setPassword(String password)
          Set the JDBC password to use for accessing the DriverManager.
 void setUrl(String url)
          Set the JDBC URL to use for accessing the DriverManager.
 void setUsername(String username)
          Set the JDBC username to use for accessing the DriverManager.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DriverManagerDataSource

public DriverManagerDataSource()
Constructor for bean-style configuration.


DriverManagerDataSource

public DriverManagerDataSource(String driverClassName,
                               String url,
                               String username,
                               String password)
                        throws SQLException
Create a new DriverManagerDataSource with the given standard DriverManager parameters.

Parameters:
driverClassName - the JDBC driver class name
url - the JDBC URL to use for accessing the DriverManager
username - the JDBC username to use for accessing the DriverManager
password - the JDBC password to use for accessing the DriverManager
Throws:
SQLException
See Also:
DriverManager.getConnection(String, String, String)

DriverManagerDataSource

public DriverManagerDataSource(String url,
                               String username,
                               String password)
                        throws SQLException
Create a new DriverManagerDataSource with the given standard DriverManager parameters.

Parameters:
url - the JDBC URL to use for accessing the DriverManager
username - the JDBC username to use for accessing the DriverManager
password - the JDBC password to use for accessing the DriverManager
Throws:
SQLException
See Also:
DriverManager.getConnection(String, String, String)

DriverManagerDataSource

public DriverManagerDataSource(String url)
                        throws SQLException
Create a new DriverManagerDataSource with the given JDBC URL, not specifying a username or password for JDBC access.

Parameters:
url - the JDBC URL to use for accessing the DriverManager
Throws:
SQLException
See Also:
DriverManager.getConnection(String)
Method Detail

setDriverClassName

public void setDriverClassName(String driverClassName)
                        throws SQLException
Set the JDBC driver class name. This driver will get initialized on startup, registering itself with the JDK's DriverManager.

Alternatively, consider initializing the JDBC driver yourself before instantiating this DataSource.

Throws:
SQLException
See Also:
Class.forName(String), DriverManager.registerDriver(java.sql.Driver)

getDriverClassName

public String getDriverClassName()
Return the JDBC driver class name, if any.


setUrl

public void setUrl(String url)
Set the JDBC URL to use for accessing the DriverManager.

See Also:
DriverManager.getConnection(String, String, String)

getUrl

public String getUrl()
Return the JDBC URL to use for accessing the DriverManager.


setUsername

public void setUsername(String username)
Set the JDBC username to use for accessing the DriverManager.

See Also:
DriverManager.getConnection(String, String, String)

getUsername

public String getUsername()
Return the JDBC username to use for accessing the DriverManager.


setPassword

public void setPassword(String password)
Set the JDBC password to use for accessing the DriverManager.

See Also:
DriverManager.getConnection(String, String, String)

getPassword

public String getPassword()
Return the JDBC password to use for accessing the DriverManager.


setConnectionProperties

public void setConnectionProperties(Properties connectionProperties)
Specify arbitrary connection properties as key/value pairs, to be passed to the DriverManager.

Can also contain "user" and "password" properties. However, any "username" and "password" bean properties specified on this DataSource will override the respective connection properties.

See Also:
DriverManager.getConnection(String, java.util.Properties)

getConnectionProperties

public Properties getConnectionProperties()
Return the connection properties to be passed to the DriverManager, if any.


getConnection

public Connection getConnection()
                         throws SQLException
This implementation delegates to getConnectionFromDriverManager, using the default username and password of this DataSource.

Specified by:
getConnection in interface DataSource
Throws:
SQLException
See Also:
getConnectionFromDriverManager()

getConnection

public Connection getConnection(String username,
                                String password)
                         throws SQLException
This implementation delegates to getConnectionFromDriverManager, using the given username and password.

Specified by:
getConnection in interface DataSource
Throws:
SQLException
See Also:
getConnectionFromDriverManager(String, String)

getConnectionFromDriverManager

protected Connection getConnectionFromDriverManager()
                                             throws SQLException
Get a Connection from the DriverManager, using the default username and password of this DataSource.

Throws:
SQLException
See Also:
getConnectionFromDriverManager(String, String)

getConnectionFromDriverManager

protected Connection getConnectionFromDriverManager(String username,
                                                    String password)
                                             throws SQLException
Build properties for the DriverManager, including the given username and password (if any).

Throws:
SQLException
See Also:
getConnectionFromDriverManager(String, java.util.Properties)

getConnectionFromDriverManager

protected Connection getConnectionFromDriverManager(String url,
                                                    Properties props)
                                             throws SQLException
Getting a connection using the nasty static from DriverManager is extracted into a protected method to allow for easy unit testing.

Throws:
SQLException
See Also:
DriverManager.getConnection(String, java.util.Properties)

getLoginTimeout

public int getLoginTimeout()
                    throws SQLException
Returns 0: means use default system timeout.

Specified by:
getLoginTimeout in interface DataSource
Throws:
SQLException

setLoginTimeout

public void setLoginTimeout(int timeout)
                     throws SQLException
Specified by:
setLoginTimeout in interface DataSource
Throws:
SQLException

getLogWriter

public PrintWriter getLogWriter()
LogWriter methods are unsupported.

Specified by:
getLogWriter in interface DataSource

setLogWriter

public void setLogWriter(PrintWriter pw)
                  throws SQLException
LogWriter methods are unsupported.

Specified by:
setLogWriter in interface DataSource
Throws:
SQLException

getDefaultClassLoader

public static ClassLoader getDefaultClassLoader()


Copyright 2006-2006-2007 Outsource Cafe Inc.. All Rights Reserved.