com.boylesoftware.cb2.util.sql
Class PreparedStatementWrapper

java.lang.Object
  extended bycom.boylesoftware.cb2.util.sql.StatementWrapper
      extended bycom.boylesoftware.cb2.util.sql.PreparedStatementWrapper
All Implemented Interfaces:
java.sql.PreparedStatement, java.sql.Statement
Direct Known Subclasses:
CallableStatementWrapper

public class PreparedStatementWrapper
extends StatementWrapper
implements java.sql.PreparedStatement

Wrapper around a JDBC's PreparedStatement interface implementation. You can derive your statement wrapper classes from this one and override some of its methods, while all the others will be calling appropriate methods on the wrapped, "real" statement.

This class also provides some "hook" methods called in certain situations. Note that if your redefine interface methods in your subclass and you use hooks you will have to call the base method from your redefined one in order to make the hooks invoked. Alternatively you call them yourself directly from the redefined interface method.

Version:
$Id: PreparedStatementWrapper.java,v 1.6 2003/11/30 15:21:55 levahim Exp $
Author:
Lev Himmelfarb

Field Summary
protected  java.sql.PreparedStatement pstmt
          Reference to the wrapped statement as a prepared statement so we don't have to do class casting all the time.
 
Fields inherited from class com.boylesoftware.cb2.util.sql.StatementWrapper
con, EXECUTE_TYPE_BATCH, EXECUTE_TYPE_GENERIC, EXECUTE_TYPE_QUERY, EXECUTE_TYPE_UPDATE, stmt
 
Fields inherited from interface java.sql.Statement
CLOSE_ALL_RESULTS, CLOSE_CURRENT_RESULT, EXECUTE_FAILED, KEEP_CURRENT_RESULT, NO_GENERATED_KEYS, RETURN_GENERATED_KEYS, SUCCESS_NO_INFO
 
Constructor Summary
PreparedStatementWrapper()
          Creates a new wrapper.
PreparedStatementWrapper(java.sql.PreparedStatement pstmt)
          Creates a new wrapper and sets the wrapped statement to the specified one.
 
Method Summary
 void addBatch()
          Interface method wrapper.
 void clearParameters()
          Interface method wrapper.
 boolean execute()
          Interface method wrapper.
 java.sql.ResultSet executeQuery()
          Interface method wrapper.
 int executeUpdate()
          Interface method wrapper.
 java.sql.ResultSetMetaData getMetaData()
          Interface method wrapper.
 java.sql.ParameterMetaData getParameterMetaData()
          Interface method wrapper.
 java.sql.PreparedStatement getWrappedPreparedStatement()
          Returns the wrapped prepared statement.
 void setArray(int i, java.sql.Array x)
          Interface method wrapper.
 void setAsciiStream(int parameterIndex, java.io.InputStream x, int length)
          Interface method wrapper.
 void setBigDecimal(int parameterIndex, java.math.BigDecimal x)
          Interface method wrapper.
 void setBinaryStream(int parameterIndex, java.io.InputStream x, int length)
          Interface method wrapper.
 void setBlob(int i, java.sql.Blob x)
          Interface method wrapper.
 void setBoolean(int parameterIndex, boolean x)
          Interface method wrapper.
 void setByte(int parameterIndex, byte x)
          Interface method wrapper.
 void setBytes(int parameterIndex, byte[] x)
          Interface method wrapper.
 void setCharacterStream(int parameterIndex, java.io.Reader reader, int length)
          Interface method wrapper.
 void setClob(int i, java.sql.Clob x)
          Interface method wrapper.
 void setDate(int parameterIndex, java.sql.Date x)
          Interface method wrapper.
 void setDate(int parameterIndex, java.sql.Date x, java.util.Calendar cal)
          Interface method wrapper.
 void setDouble(int parameterIndex, double x)
          Interface method wrapper.
 void setFloat(int parameterIndex, float x)
          Interface method wrapper.
 void setInt(int parameterIndex, int x)
          Interface method wrapper.
 void setLong(int parameterIndex, long x)
          Interface method wrapper.
 void setNull(int parameterIndex, int sqlType)
          Interface method wrapper.
 void setNull(int paramIndex, int sqlType, java.lang.String typeName)
          Interface method wrapper.
 void setObject(int parameterIndex, java.lang.Object x)
          Interface method wrapper.
 void setObject(int parameterIndex, java.lang.Object x, int targetSqlType)
          Interface method wrapper.
 void setObject(int parameterIndex, java.lang.Object x, int targetSqlType, int scale)
          Interface method wrapper.
protected  java.lang.Object setParameterHook(int index, java.lang.Object param)
          This hook is called by all methods that set query parameters.
 void setRef(int i, java.sql.Ref x)
          Interface method wrapper.
 void setShort(int parameterIndex, short x)
          Interface method wrapper.
 void setString(int parameterIndex, java.lang.String x)
          Interface method wrapper.
 void setTime(int parameterIndex, java.sql.Time x)
          Interface method wrapper.
 void setTime(int parameterIndex, java.sql.Time x, java.util.Calendar cal)
          Interface method wrapper.
 void setTimestamp(int parameterIndex, java.sql.Timestamp x)
          Interface method wrapper.
 void setTimestamp(int parameterIndex, java.sql.Timestamp x, java.util.Calendar cal)
          Interface method wrapper.
 void setUnicodeStream(int parameterIndex, java.io.InputStream x, int length)
          Deprecated.  
 void setURL(int parameterIndex, java.net.URL x)
          Interface method wrapper.
 void setWrappedStatement(java.sql.Statement stmt)
          Sets the wrapped statement.
 
Methods inherited from class com.boylesoftware.cb2.util.sql.StatementWrapper
addBatch, cancel, clearBatch, clearWarnings, close, execute, execute, execute, execute, executeBatch, executeQuery, executeUpdate, executeUpdate, executeUpdate, executeUpdate, getConnection, getFetchDirection, getFetchSize, getGeneratedKeys, getMaxFieldSize, getMaxRows, getMoreResults, getMoreResults, getQueryTimeout, getResultSet, getResultSetConcurrency, getResultSetHoldability, getResultSetType, getUpdateCount, getWarnings, getWrappedStatement, postCallHook, postexecuteHook, preexecuteHook, setConnection, setCursorName, setEscapeProcessing, setFetchDirection, setFetchSize, setMaxFieldSize, setMaxRows, setQueryTimeout
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.sql.Statement
addBatch, cancel, clearBatch, clearWarnings, close, execute, execute, execute, execute, executeBatch, executeQuery, executeUpdate, executeUpdate, executeUpdate, executeUpdate, getConnection, getFetchDirection, getFetchSize, getGeneratedKeys, getMaxFieldSize, getMaxRows, getMoreResults, getMoreResults, getQueryTimeout, getResultSet, getResultSetConcurrency, getResultSetHoldability, getResultSetType, getUpdateCount, getWarnings, setCursorName, setEscapeProcessing, setFetchDirection, setFetchSize, setMaxFieldSize, setMaxRows, setQueryTimeout
 

Field Detail

pstmt

protected java.sql.PreparedStatement pstmt
Reference to the wrapped statement as a prepared statement so we don't have to do class casting all the time.

Constructor Detail

PreparedStatementWrapper

public PreparedStatementWrapper()
Creates a new wrapper. The wrapped statement should be set by a setWrappedStatement call before using the instance.


PreparedStatementWrapper

public PreparedStatementWrapper(java.sql.PreparedStatement pstmt)
Creates a new wrapper and sets the wrapped statement to the specified one.

Parameters:
pstmt - the prepared statement to be wrapped.
Method Detail

setWrappedStatement

public void setWrappedStatement(java.sql.Statement stmt)
Sets the wrapped statement.

Overrides:
setWrappedStatement in class StatementWrapper
Parameters:
stmt - statement to wrap, must be a prepared statement.
Throws:
java.lang.IllegalArgumentException - if the specified argument is not a prepared statement.

getWrappedPreparedStatement

public java.sql.PreparedStatement getWrappedPreparedStatement()
Returns the wrapped prepared statement.

Returns:
the wrapped prepared statement.

executeQuery

public java.sql.ResultSet executeQuery()
                                throws java.sql.SQLException
Interface method wrapper.

Also calles preexecuteHook and postexecuteHook hooks.

Specified by:
executeQuery in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

executeUpdate

public int executeUpdate()
                  throws java.sql.SQLException
Interface method wrapper.

Also calles preexecuteHook and postexecuteHook hooks.

Specified by:
executeUpdate in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

setNull

public void setNull(int parameterIndex,
                    int sqlType)
             throws java.sql.SQLException
Interface method wrapper.

Specified by:
setNull in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

setBoolean

public void setBoolean(int parameterIndex,
                       boolean x)
                throws java.sql.SQLException
Interface method wrapper.

Specified by:
setBoolean in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

setByte

public void setByte(int parameterIndex,
                    byte x)
             throws java.sql.SQLException
Interface method wrapper.

Specified by:
setByte in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

setShort

public void setShort(int parameterIndex,
                     short x)
              throws java.sql.SQLException
Interface method wrapper.

Specified by:
setShort in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

setInt

public void setInt(int parameterIndex,
                   int x)
            throws java.sql.SQLException
Interface method wrapper.

Specified by:
setInt in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

setLong

public void setLong(int parameterIndex,
                    long x)
             throws java.sql.SQLException
Interface method wrapper.

Specified by:
setLong in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

setFloat

public void setFloat(int parameterIndex,
                     float x)
              throws java.sql.SQLException
Interface method wrapper.

Specified by:
setFloat in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

setDouble

public void setDouble(int parameterIndex,
                      double x)
               throws java.sql.SQLException
Interface method wrapper.

Specified by:
setDouble in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

setBigDecimal

public void setBigDecimal(int parameterIndex,
                          java.math.BigDecimal x)
                   throws java.sql.SQLException
Interface method wrapper.

Specified by:
setBigDecimal in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

setString

public void setString(int parameterIndex,
                      java.lang.String x)
               throws java.sql.SQLException
Interface method wrapper.

Specified by:
setString in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

setBytes

public void setBytes(int parameterIndex,
                     byte[] x)
              throws java.sql.SQLException
Interface method wrapper.

Specified by:
setBytes in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

setDate

public void setDate(int parameterIndex,
                    java.sql.Date x)
             throws java.sql.SQLException
Interface method wrapper.

Specified by:
setDate in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

setTime

public void setTime(int parameterIndex,
                    java.sql.Time x)
             throws java.sql.SQLException
Interface method wrapper.

Specified by:
setTime in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

setTimestamp

public void setTimestamp(int parameterIndex,
                         java.sql.Timestamp x)
                  throws java.sql.SQLException
Interface method wrapper.

Specified by:
setTimestamp in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

setAsciiStream

public void setAsciiStream(int parameterIndex,
                           java.io.InputStream x,
                           int length)
                    throws java.sql.SQLException
Interface method wrapper.

Specified by:
setAsciiStream in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

setUnicodeStream

public void setUnicodeStream(int parameterIndex,
                             java.io.InputStream x,
                             int length)
                      throws java.sql.SQLException
Deprecated.  

Interface method wrapper.

Specified by:
setUnicodeStream in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

setBinaryStream

public void setBinaryStream(int parameterIndex,
                            java.io.InputStream x,
                            int length)
                     throws java.sql.SQLException
Interface method wrapper.

Specified by:
setBinaryStream in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

clearParameters

public void clearParameters()
                     throws java.sql.SQLException
Interface method wrapper.

Specified by:
clearParameters in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

setObject

public void setObject(int parameterIndex,
                      java.lang.Object x,
                      int targetSqlType,
                      int scale)
               throws java.sql.SQLException
Interface method wrapper.

Specified by:
setObject in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

setObject

public void setObject(int parameterIndex,
                      java.lang.Object x,
                      int targetSqlType)
               throws java.sql.SQLException
Interface method wrapper.

Specified by:
setObject in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

setObject

public void setObject(int parameterIndex,
                      java.lang.Object x)
               throws java.sql.SQLException
Interface method wrapper.

Specified by:
setObject in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

execute

public boolean execute()
                throws java.sql.SQLException
Interface method wrapper.

Also calles preexecuteHook and postexecuteHook hooks.

Specified by:
execute in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

addBatch

public void addBatch()
              throws java.sql.SQLException
Interface method wrapper.

Specified by:
addBatch in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

setCharacterStream

public void setCharacterStream(int parameterIndex,
                               java.io.Reader reader,
                               int length)
                        throws java.sql.SQLException
Interface method wrapper.

Specified by:
setCharacterStream in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

setRef

public void setRef(int i,
                   java.sql.Ref x)
            throws java.sql.SQLException
Interface method wrapper.

Specified by:
setRef in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

setBlob

public void setBlob(int i,
                    java.sql.Blob x)
             throws java.sql.SQLException
Interface method wrapper.

Specified by:
setBlob in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

setClob

public void setClob(int i,
                    java.sql.Clob x)
             throws java.sql.SQLException
Interface method wrapper.

Specified by:
setClob in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

setArray

public void setArray(int i,
                     java.sql.Array x)
              throws java.sql.SQLException
Interface method wrapper.

Specified by:
setArray in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

getMetaData

public java.sql.ResultSetMetaData getMetaData()
                                       throws java.sql.SQLException
Interface method wrapper.

Specified by:
getMetaData in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

setDate

public void setDate(int parameterIndex,
                    java.sql.Date x,
                    java.util.Calendar cal)
             throws java.sql.SQLException
Interface method wrapper.

Specified by:
setDate in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

setTime

public void setTime(int parameterIndex,
                    java.sql.Time x,
                    java.util.Calendar cal)
             throws java.sql.SQLException
Interface method wrapper.

Specified by:
setTime in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

setTimestamp

public void setTimestamp(int parameterIndex,
                         java.sql.Timestamp x,
                         java.util.Calendar cal)
                  throws java.sql.SQLException
Interface method wrapper.

Specified by:
setTimestamp in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

setNull

public void setNull(int paramIndex,
                    int sqlType,
                    java.lang.String typeName)
             throws java.sql.SQLException
Interface method wrapper.

Specified by:
setNull in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

setURL

public void setURL(int parameterIndex,
                   java.net.URL x)
            throws java.sql.SQLException
Interface method wrapper.

Specified by:
setURL in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

getParameterMetaData

public java.sql.ParameterMetaData getParameterMetaData()
                                                throws java.sql.SQLException
Interface method wrapper.

Specified by:
getParameterMetaData in interface java.sql.PreparedStatement
Throws:
java.sql.SQLException

setParameterHook

protected java.lang.Object setParameterHook(int index,
                                            java.lang.Object param)
                                     throws java.sql.SQLException
This hook is called by all methods that set query parameters. The default implementation just returns the param.

Parameters:
index - index of the parameter.
param - the param's value, which is properly wrapped if it is a primitive type, null if setNull is called.
Returns:
value, which will be passed down to the underlying statement implementation. Has to be of the proper runtime type.
Throws:
java.sql.SQLException - if an error happens.


Copyright © 2002,2003,2004 - Boyle Software, Inc.