com.framework.jdto.jdbc.wrapper
Class PreparedStatementWrapper

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

public class PreparedStatementWrapper
extends JDBCTemplate

This class is a Wrapper Class for java.sql.PreparedStatement. This class has the ability to take care of the transactions. if you want the current PrepareStatementWrapper Object to participate in a Transaction you must call the setSql() method with a boolean parameter as true on the current PrepareStatementWrapper 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 PreparedStatement 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 PrepareStatementWrapper Object is not participating in any transaction. if the current PrepareStatementWrapper 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
PreparedStatementWrapper()
          Default Constructor
PreparedStatementWrapper(DataSource ds)
          Constructor that sets the DataSource Object to the current PreparedStatementWrapper Object.
 
Method Summary
 boolean execute()
          Calls the execute() method of java.sql.PreparedStatement and closes the PreparedStatement and connection before returning the boolean value.
 Object executeQuery(RowMapper row)
          Executes the SQL query in this PreparedStatementWrapper object and returns an Object type of object generated by the query.
 int executeUpdate()
          Executes the SQL statement in this PreparedStatementWrapper object, which must be an SQL INSERT, UPDATE or DELETE statement or an SQL statement that returns nothing, such as a DDL statement.
 String getSql()
          Returns the sql query that is associated with the current PreparedStatementWrapper object
 void setBigDecimal(int parameterIndex, BigDecimal x)
          Sets the designated parameter to BigDecimal value.
 void setBoolean(int parameterIndex, boolean x)
          Sets the designated parameter to boolean value.
 void setByte(int parameterIndex, byte x)
          Sets the designated parameter to byte value.
 void setBytes(int parameterIndex, byte[] x)
          Sets the designated parameter to array of bytes.
 void setDate(int parameterIndex, Date x)
          Sets the designated parameter to Date value.
 void setDouble(int parameterIndex, double x)
          Sets the designated parameter to double value.
 void setFloat(int parameterIndex, float x)
          Sets the designated parameter to float value.
 void setInt(int parameterIndex, int x)
          Sets the designated parameter to int value.
 void setLong(int parameterIndex, long x)
          Sets the designated parameter to long value.
 void setNull(int parameterIndex, int sqlType)
          Sets the designated parameter to SQL NULL.
 void setShort(int parameterIndex, short x)
          Sets the designated parameter to short value.
 void setSql(String sql)
          Sets the sql string to the current PreparedStatementWrapper Object and associates it with the java.sql.PreparedStatement.
 void setSql(String sql, boolean avoidTransaction)
          Sets the sql string to the current PreparedStatementWrapper Object and associates it with the java.sql.PreparedStatement.
 void setString(int parameterIndex, String x)
          Sets the designated parameter to String value.
 void setTime(int parameterIndex, Time x)
          Sets the designated parameter to SQL Time value.
 void setTimestamp(int parameterIndex, Timestamp x)
          Sets the designated parameter to SQL TimeStamp value.
 
Methods inherited from class com.framework.jdto.jdbc.wrapper.JDBCTemplate
setDatasource
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PreparedStatementWrapper

public PreparedStatementWrapper()
Default Constructor


PreparedStatementWrapper

public PreparedStatementWrapper(DataSource ds)
Constructor that sets the DataSource Object to the current PreparedStatementWrapper Object.

Parameters:
ds - javax.sql.DataSource
Method Detail

getSql

public String getSql()
Returns the sql query that is associated with the current PreparedStatementWrapper object

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

setSql

public void setSql(String sql)
            throws DAOException
Sets the sql string to the current PreparedStatementWrapper Object and associates it with the java.sql.PreparedStatement.

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

setSql

public void setSql(String sql,
                   boolean avoidTransaction)
            throws DAOException
Sets the sql string to the current PreparedStatementWrapper Object and associates it with the java.sql.PreparedStatement. 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 avoid running in an existing transaction.

If there is an existing transaction, which is started by TransactionContextManager's beginTransaction() method then this method dis associates the current Object from the currently running Transaction if the boolean parameter is true. But if the boolean parameter is false then this method associates the current Object to the currently running Transaction.

If there is no transaction running, this method doesn't associate the current object to any transaction irrespective the boolean value.

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

executeQuery

public Object executeQuery(RowMapper row)
                    throws DAOException
Executes the SQL query in this PreparedStatementWrapper object and returns an Object type of object generated by the query. it takes a RowMapper Object as parameter.This method closes the ResultSet, PreparedStatement 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 SQL statement in this PreparedStatementWrapper object, which must be an SQL INSERT, UPDATE or DELETE statement or an SQL statement that returns nothing, such as a DDL statement. This method closes the PreparedStatement 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
Calls the execute() method of java.sql.PreparedStatement and closes the PreparedStatement and connection before returning the boolean value. This method closes the PreparedStatement 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

setNull

public void setNull(int parameterIndex,
                    int sqlType)
             throws DAOException
Sets the designated parameter to SQL NULL.

Parameters:
parameterIndex - the first parameter is 1, the second is 2, ...
sqlType - the SQL type code defined in java.sql.Types
Throws:
DAOException - if a database access error occurs

setBoolean

public void setBoolean(int parameterIndex,
                       boolean x)
                throws DAOException
Sets the designated parameter to boolean value.

Parameters:
parameterIndex - the first parameter is 1, the second is 2, ...
x - the parameter value
Throws:
DAOException - if a database access error occurs

setByte

public void setByte(int parameterIndex,
                    byte x)
             throws DAOException
Sets the designated parameter to byte value.

Parameters:
parameterIndex - the first parameter is 1, the second is 2, ...
x - the parameter value
Throws:
DAOException - if a database access error occurs

setShort

public void setShort(int parameterIndex,
                     short x)
              throws DAOException
Sets the designated parameter to short value.

Parameters:
parameterIndex - the first parameter is 1, the second is 2, ...
x - the parameter value
Throws:
DAOException - if a database access error occurs

setInt

public void setInt(int parameterIndex,
                   int x)
            throws DAOException
Sets the designated parameter to int value.

Parameters:
parameterIndex - the first parameter is 1, the second is 2, ...
x - the parameter value
Throws:
DAOException - if a database access error occurs

setLong

public void setLong(int parameterIndex,
                    long x)
             throws DAOException
Sets the designated parameter to long value.

Parameters:
parameterIndex - the first parameter is 1, the second is 2, ...
x - the parameter value
Throws:
DAOException - if a database access error occurs

setFloat

public void setFloat(int parameterIndex,
                     float x)
              throws DAOException
Sets the designated parameter to float value.

Parameters:
parameterIndex - the first parameter is 1, the second is 2, ...
x - the parameter value
Throws:
DAOException - if a database access error occurs

setDouble

public void setDouble(int parameterIndex,
                      double x)
               throws DAOException
Sets the designated parameter to double value.

Parameters:
parameterIndex - the first parameter is 1, the second is 2, ...
x - the parameter value
Throws:
DAOException - if a database access error occurs

setBigDecimal

public void setBigDecimal(int parameterIndex,
                          BigDecimal x)
                   throws DAOException
Sets the designated parameter to BigDecimal value.

Parameters:
parameterIndex - the first parameter is 1, the second is 2, ...
x - the parameter value
Throws:
DAOException - if a database access error occurs

setString

public void setString(int parameterIndex,
                      String x)
               throws DAOException
Sets the designated parameter to String value.

Parameters:
parameterIndex - the first parameter is 1, the second is 2, ...
x - the parameter value
Throws:
DAOException - if a database access error occurs

setBytes

public void setBytes(int parameterIndex,
                     byte[] x)
              throws DAOException
Sets the designated parameter to array of bytes.

Parameters:
parameterIndex - the first parameter is 1, the second is 2, ...
x - the parameter value
Throws:
DAOException - if a database access error occurs

setDate

public void setDate(int parameterIndex,
                    Date x)
             throws DAOException
Sets the designated parameter to Date value.

Parameters:
parameterIndex - the first parameter is 1, the second is 2, ...
x - the parameter value
Throws:
DAOException - if a database access error occurs

setTime

public void setTime(int parameterIndex,
                    Time x)
             throws DAOException
Sets the designated parameter to SQL Time value.

Parameters:
parameterIndex - the first parameter is 1, the second is 2, ...
x - the parameter value
Throws:
DAOException - if a database access error occurs

setTimestamp

public void setTimestamp(int parameterIndex,
                         Timestamp x)
                  throws DAOException
Sets the designated parameter to SQL TimeStamp value.

Parameters:
parameterIndex - the first parameter is 1, the second is 2, ...
x - the parameter value
Throws:
DAOException - if a database access error occurs