com.framework.jdto.jdbc.wrapper
Class StatementWrapper

java.lang.Object
  extended bycom.framework.jdto.jdbc.wrapper.JDBCTemplate
      extended bycom.framework.jdto.jdbc.wrapper.StatementWrapper

public class StatementWrapper
extends JDBCTemplate

This class is a Wrapper Class for java.sql.Statement. This class has the ability to take care of the transactions. if you want the current StatementWrapper Object to participate in a Transaction you must call the setSql() method with a boolean parameter as true on the current StatementWrapper Object. This Class takes care of getting and releasing the connection based on the current Objects transaction status.

This class also has the capability to Log the Statement SQL Query.

The Developer is not required to take care of closing the connection. This classes closes the connection after the execution of the query if the current StatementWrapper Object is not participating in any transaction. if the current StatementWrapper Object Participates in a Transaction then the TransactionContextManager will take care of closing the connection once the transaction is either committed or rolledback.

Author:
msatish

Constructor Summary
StatementWrapper()
           
StatementWrapper(javax.sql.DataSource ds)
           
 
Method Summary
 boolean execute()
          Executes the given SQL statement in the current StatementWrapper Object, which returns a boolean value.
 java.lang.Object executeQuery(RowMapper row)
          Executes the given SQL statement in the current StatementWrapper Object, which returns a single java.lang.Object type of object.
 int executeUpdate()
          Executes the given SQL statement in the current StatementWrapper Object, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement.
 java.lang.String getSql()
          Returns the sql query that is associated with the current StatementWrapper object
 void setSql(java.lang.String sql)
          Sets the sql string to the current StatementWrapper Object and associates it with the java.sql.Statement.
 void setSql(java.lang.String sql, boolean addToTransaction)
          Sets the sql string to the current StatementWrapper Object and associates it with the java.sql.Statement.
 
Methods inherited from class com.framework.jdto.jdbc.wrapper.JDBCTemplate
finishTransaction, setDatasource
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StatementWrapper

public StatementWrapper()

StatementWrapper

public StatementWrapper(javax.sql.DataSource ds)
Method Detail

getSql

public java.lang.String getSql()
Returns the sql query that is associated with the current StatementWrapper object

Returns:
sql - an SQL Query as java.lang.String

setSql

public void setSql(java.lang.String sql)
            throws DAOException
Sets the sql string to the current StatementWrapper Object and associates it with the java.sql.Statement. object.

Parameters:
sql - an SQL Query as java.lang.String
Throws:
DAOException - if database error occurs

setSql

public void setSql(java.lang.String sql,
                   boolean addToTransaction)
            throws DAOException
Sets the sql string to the current StatementWrapper Object and associates it with the java.sql.Statement. This method associates current object to the current running thread to run in a Transaction. This method should be explicitly called with boolean parameter as true, if you want the current object to participate in a transaction.

If there is an existing transaction then this method associates the current Object to the currently running Transaction. Otherwise this method creates a new Transaction.

If a new Transaction is started from this method (That is, TransactionContextManager's beginTransaction() is not called before), user needs to call this method on each StatementWrapper object, which needs to run in a transaction.

But if a new Transaction is created by calling the TransactionContextManager's beginTransaction() method then, it is not required to call this method explicitly, instead setSql(String sql) method can be called. The TransactionContextManager will take care of running all the StatementWrapper Objects in the current Transaction, which are executed after beginTransaction() and before endTransaction() method.

Parameters:
sql - an SQL Query as java.lang.String
addToTransaction - adds the current StatementWrapper Object to the current Transaction if true.
Throws:
DAOException - if any database error occurs

executeQuery

public java.lang.Object executeQuery(RowMapper row)
                              throws DAOException
Executes the given SQL statement in the current StatementWrapper Object, which returns a single java.lang.Object type of object. it takes a RowMapper Object as parameter. This method closes the ResultSet, Statement and Connection before returning the Object value.

If the current object is running in a transaction, on successful execution this sets the Current Transaction Status as STATUS_MARKED_COMMIT, otherwise it sets as STATUS_MARKED_ROLLBACK.

Parameters:
row - any class that implements RowMapper Interface.
Returns:
obj - an Object type of object, which can be any user-defined value object.
Throws:
DAOException - if any database error occurs

executeUpdate

public int executeUpdate()
                  throws DAOException
Executes the given SQL statement in the current StatementWrapper Object, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement. This method closes the Statement and Connection before returning the int value.

If the current object is running in a transaction, on successful execution this sets the Current Transaction Status as STATUS_MARKED_COMMIT, otherwise it sets as STATUS_MARKED_ROLLBACK.

Returns:
updateCount - number of rows updated
Throws:
DAOException - if any database error occurs

execute

public boolean execute()
                throws DAOException
Executes the given SQL statement in the current StatementWrapper Object, which returns a boolean value. This method closes the Statement and Connection before returning the boolean value.

If the current object is running in a transaction, on successful execution this sets the Current Transaction Status as STATUS_MARKED_COMMIT, otherwise it sets as STATUS_MARKED_ROLLBACK.

Returns:
bValue - a boolean value that tells whether the query has executed successfully or not
Throws:
DAOException - if any database error occurs