net.sf.xbus.technical.database
Class DBConnection

java.lang.Object
  extended by net.sf.xbus.technical.database.DBConnection
All Implemented Interfaces:
TAResource

public class DBConnection
extends java.lang.Object
implements TAResource

DBConnection manages connections to databases.

It implements two Design-Patterns:

  1. Singleton: An instance of DBConnection is created for every thread. This instance can be fetched with getInstance().
  2. Facade: The complexity of creating and managing a connection is capsuled.


Field Summary
static java.lang.String UNNAMED
           
 
Method Summary
 void bind(java.lang.Object name, int pos, int value)
          Binds an integer value to a parameter in a stored prepared statement.
 void bind(java.lang.Object name, int pos, java.lang.String value)
          Binds a string value to a parameter in a stored prepared statement.
 void bindNull(java.lang.Object name, int pos, int type)
          Binds a null value to a parameter in a stored prepared statement.
static void clear()
           
 void close()
          Closes a resource.
 void commit()
          Commits all actions on the database.
 java.sql.ResultSet executeRead(java.lang.String statement)
          Executes a SQL statement to read data.
 java.sql.ResultSet executeReadPrepared(java.lang.Object name)
          Executes a stored prepared statement.
 int executeUpdate(java.lang.String statement)
          Executes an update, insert or delete statement.
 int executeUpdatePrepared(java.lang.Object name)
          Executes a stored prepared statement.
 boolean existsPrepared(java.lang.Object name)
          Tests if a prepared statement is stored.
static DBConnection getInstance(java.lang.String name)
          Delivers an instance of DBConnection.
 java.lang.String getUrl()
          Returns the complete URL of the DBConnection
 void open()
          Opens the connection to the database.
 void prepareStatement(java.lang.Object name, java.lang.String statement)
          Stores the given SQL statement as a prepared statement under the given name.
 void reopen()
          Reopens a database connection after an error has been detected.
 void rollback()
          Performs a rollback for all actions on the database.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

UNNAMED

public static final java.lang.String UNNAMED
See Also:
Constant Field Values
Method Detail

getInstance

public static DBConnection getInstance(java.lang.String name)
                                throws XException
Delivers an instance of DBConnection.

If it is the first call for the actual thread, a new DBConnection -object gets created. Subsequent calls in this thread will deliver the object, that has been created by the first call.

Throws:
XException

open

public void open()
          throws XException
Opens the connection to the database.

Specified by:
open in interface TAResource
Throws:
XException - if something goes wrong

close

public void close()
           throws XException
Description copied from interface: TAResource
Closes a resource.

Specified by:
close in interface TAResource
Throws:
XException - if something goes wrong

commit

public void commit()
            throws XException
Commits all actions on the database.

Specified by:
commit in interface TAResource
Throws:
XException - if something goes wrong

rollback

public void rollback()
              throws XException
Performs a rollback for all actions on the database.

Specified by:
rollback in interface TAResource
Throws:
XException - if something goes wrong

reopen

public void reopen()
            throws XException
Reopens a database connection after an error has been detected. All internal variables are initialized.

Throws:
XException

executeRead

public java.sql.ResultSet executeRead(java.lang.String statement)
                               throws XException
Executes a SQL statement to read data.

Parameters:
statement - SQL statement to read data
Returns:
set of read rows
Throws:
XException - if something goes wrong

executeUpdate

public int executeUpdate(java.lang.String statement)
                  throws XException
Executes an update, insert or delete statement.

Parameters:
statement - SQL statement to update, insert or delete rows
Returns:
number of rows affected
Throws:
XException - if something goes wrong

prepareStatement

public void prepareStatement(java.lang.Object name,
                             java.lang.String statement)
                      throws XException
Stores the given SQL statement as a prepared statement under the given name.

Throws:
XException

bind

public void bind(java.lang.Object name,
                 int pos,
                 java.lang.String value)
          throws XException
Binds a string value to a parameter in a stored prepared statement.

Parameters:
name - the name under which the prepared statement is stored
pos - position of the parameter in the prepared statement
value - value that will be bound to the parameter
Throws:
XException

bind

public void bind(java.lang.Object name,
                 int pos,
                 int value)
          throws XException
Binds an integer value to a parameter in a stored prepared statement.

Parameters:
name - the name under which the prepared statement is stored
pos - position of the parameter in the prepared statement
value - value that will be bound to the parameter
Throws:
XException

bindNull

public void bindNull(java.lang.Object name,
                     int pos,
                     int type)
              throws XException
Binds a null value to a parameter in a stored prepared statement.

Parameters:
name - the name under which the prepared statement is stored
pos - position of the parameter in the prepared statement
type - the type of the column (see java.sql.Types)
Throws:
XException

executeUpdatePrepared

public int executeUpdatePrepared(java.lang.Object name)
                          throws XException
Executes a stored prepared statement.

Parameters:
name - the name under which the prepared statement is stored
Returns:
number of affected rows
Throws:
XException

executeReadPrepared

public java.sql.ResultSet executeReadPrepared(java.lang.Object name)
                                       throws XException
Executes a stored prepared statement.

Parameters:
name - the name under which the prepared statement is stored
Returns:
the ResultSet for the query
Throws:
XException

existsPrepared

public boolean existsPrepared(java.lang.Object name)
Tests if a prepared statement is stored.

Parameters:
name - the name under which the prepared statement is stored
Returns:
true if the prepared statement exists

clear

public static void clear()

getUrl

public java.lang.String getUrl()
Returns the complete URL of the DBConnection