com.mortbay.JDBC
Class Database

java.lang.Object
  |
  +--com.mortbay.JDBC.Database

public class Database
extends java.lang.Object

JDBC Database wrapper This class wraps the detail of creating JDBC drivers and connections to a particular database.

This class takes a DbAdaptor instance, a JDBC database URL and an optional properties object and manages a pool of JDBC connections to that database.

The DbAdaptor instance is used to handle table creation from meta data and formatting values for the particular underlying database.

Notes

The connection pool is managed by wrapper classes for ResultSet and Connection. (Really needs weak references :-)

Version:
$Revision: 2.4 $ $Date: 2000/06/25 21:22:58 $
Author:
Greg Wilkins (gregw)
See Also:

Constructor Summary
Database()
          Default Constructor.
Database(java.util.Hashtable properties)
          Database constructor
Database(java.lang.String dbAdaptor, java.lang.String dbUrl)
          Database constructor
Database(java.lang.String dbAdaptor, java.lang.String dbUrl, java.util.Hashtable properties)
          Database constructor
 
Method Summary
 DbAdaptor getAdaptor()
          Get the db adaptor
 java.sql.Connection getConnection()
          Get a connection Get either a new or recycled connection.
 java.sql.Connection getConnection(Transaction t)
          Get Transaction connection.
 Transaction newTransaction()
          Create a new transaction instance
 java.sql.ResultSet query(java.lang.String query)
          Send an SQL query to the database.
 java.sql.ResultSet query(Transaction tx, java.lang.String query)
          Send an SQL query to the database in a transaction.
 java.lang.String quote(java.lang.String s)
          Quote a string for an SQL statement Prepare a string for inclusion in a SQL clause with the quoting conventions of the underlying database.
 int update(java.lang.String update)
          Send an update SQL request to the database
 int update(Transaction tx, java.lang.String update)
          Send an update SQL request to the database in transaction
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Database

public Database()
Default Constructor. Creates testing DB for MSQL adaptor with null URL

Database

public Database(java.util.Hashtable properties)
         throws java.sql.SQLException,
                java.lang.ClassNotFoundException,
                java.lang.InstantiationException,
                java.lang.IllegalAccessException
Database constructor
Parameters:
properties - A hashtable of connection properties, which must at least contain DbAdaptor and DbUrl.
Throws:
java.lang.ClassNotFoundException - Could not find DbAdaptor or the JDBC driver specified by the adaptor
java.lang.InstantiationException - Could not create DbAdaptor or the JDBC driver specified by the adaptor
java.lang.IllegalAccessException - Other problem with DbAdaptor or the JDBC driver specified by the adaptor
java.sql.SQLException - JDBC driver manager could not open connection

Database

public Database(java.lang.String dbAdaptor,
                java.lang.String dbUrl,
                java.util.Hashtable properties)
         throws java.sql.SQLException,
                java.lang.ClassNotFoundException,
                java.lang.InstantiationException,
                java.lang.IllegalAccessException
Database constructor
Parameters:
dbAdaptor - The full package and class name of the JDBC DbAdaptor type to use for this database.
dbUrl - The url of this database
properties - A hashtable of connection properties (or null)
Throws:
java.lang.ClassNotFoundException - Could not find DbAdaptor or the JDBC driver specified by the adaptor
java.lang.InstantiationException - Could not create DbAdaptor or the JDBC driver specified by the adaptor
java.lang.IllegalAccessException - Other problem with DbAdaptor or the JDBC driver specified by the adaptor
java.sql.SQLException - JDBC driver manager could not open connection

Database

public Database(java.lang.String dbAdaptor,
                java.lang.String dbUrl)
         throws java.sql.SQLException,
                java.lang.ClassNotFoundException,
                java.lang.InstantiationException,
                java.lang.IllegalAccessException
Database constructor
Parameters:
dbAdaptor - The full package and class name of the JDBC DbAdaptor type to use for this database.
dbUrl - The url of this database
Throws:
java.lang.ClassNotFoundException - Could not find DbAdaptor or the JDBC driver specified by the adaptor
java.lang.InstantiationException - Could not create DbAdaptor or the JDBC driver specified by the adaptor
java.lang.IllegalAccessException - Other problem with DbAdaptor or the JDBC driver specified by the adaptor
java.sql.SQLException - JDBC driver manager could not open connection
Method Detail

getAdaptor

public DbAdaptor getAdaptor()
Get the db adaptor
Returns:
DbAdaptor

newTransaction

public Transaction newTransaction()
                           throws java.sql.SQLException
Create a new transaction instance
Returns:
The transaction instance.
Throws:
java.sql.SQLException -  

getConnection

public java.sql.Connection getConnection()
                                  throws java.sql.SQLException
Get a connection Get either a new or recycled connection. The real JDBC connection is wrapped in a com.mortbay.JDBC.Connection instance, which returns the connection when it is finalized or an attempt is made to close the connection.
Returns:
The connection instance
Throws:
java.sql.SQLException -  

getConnection

public java.sql.Connection getConnection(Transaction t)
                                  throws java.sql.SQLException
Get Transaction connection. Get connection from a transaction. If the connection is null, this call acts as getConnection().
Parameters:
t - Transaction or null.
Returns:
Connection for the transaction or a pool connection the transaction is null.

query

public java.sql.ResultSet query(java.lang.String query)
                         throws java.sql.SQLException
Send an SQL query to the database. When the returned ResultSet is emptied, closed or finalized, the underlying connection is recycled.
Parameters:
update - SQL query clause
Returns:
ResultSet containing results of the query.

query

public java.sql.ResultSet query(Transaction tx,
                                java.lang.String query)
                         throws java.sql.SQLException
Send an SQL query to the database in a transaction.
Parameters:
tx - The transaction object or null if no transaction
query - SQL query clause
Returns:
ResultSet containing results of the query.
Throws:
java.sql.SQLException -  

update

public int update(java.lang.String update)
           throws java.sql.SQLException
Send an update SQL request to the database
Parameters:
update - SQL update clause
Returns:
int number of rows updated

update

public int update(Transaction tx,
                  java.lang.String update)
           throws java.sql.SQLException
Send an update SQL request to the database in transaction
Parameters:
tx - The transaction object or null if no transaction
update - SQL update string
Returns:
number of rows updated
Throws:
java.sql.SQLException -  

quote

public java.lang.String quote(java.lang.String s)
Quote a string for an SQL statement Prepare a string for inclusion in a SQL clause with the quoting conventions of the underlying database.