net.sourceforge.jtds.jdbc

Class JtdsPreparedStatement

Implemented Interfaces:
PreparedStatement, java.sql.Statement
Known Direct Subclasses:
JtdsCallableStatement

public class JtdsPreparedStatement
extends JtdsStatement
implements PreparedStatement

jTDS implementation of the java.sql.PreparedStatement interface.

Implementation notes:

  1. Generally a simple subclass of Statement mainly adding support for the setting of parameters.
  2. The stream logic is taken over from the work Brian did to add Blob support to the original jTDS.
  3. Use of Statement specific method calls eg executeQuery(sql) is blocked by this version of the driver. This is unlike the original jTDS but inline with all the other JDBC drivers that I have been able to test.
Version:
$Id: JtdsPreparedStatement.java,v 1.63 2007/07/12 21:03:23 bheineman Exp $
Authors:
Mike Hutchinson
Brian Heineman

Field Summary

private static NumberFormat
f
Used to format numeric values when scale is specified.
(package private) Collection
handles
Collection of handles used by this statement
protected ParamInfo[]
paramMetaData
The cached parameter meta data.
protected ParamInfo[]
parameters
The parameter list for the call.
protected String
procName
The procedure name for CallableStatements.
private boolean
returnKeys
True to return generated keys.
protected String
sql
The SQL statement being prepared.
protected String
sqlWord
The first SQL keyword in the SQL string.

Fields inherited from class net.sourceforge.jtds.jdbc.JtdsStatement

BOOLEAN, CLOSE_ALL_RESULTS, CLOSE_CURRENT_RESULT, DATALINK, DEFAULT_FETCH_SIZE, EXECUTE_FAILED, KEEP_CURRENT_RESULT, NO_GENERATED_KEYS, RETURN_GENERATED_KEYS, SUCCESS_NO_INFO, batchValues, closed, colMetaData, connection, currentResult, cursorName, escapeProcessing, fetchDirection, fetchSize, genKeyResultSet, maxFieldSize, maxRows, messages, openResultSets, queryTimeout, resultQueue, resultSetConcurrency, resultSetType, tds, updateCount

Constructor Summary

JtdsPreparedStatement(ConnectionJDBC2 connection, String sql, int resultSetType, int concurrency, boolean returnKeys)
Construct a new preparedStatement object.

Method Summary

void
addBatch()
void
addBatch(String sql)
protected void
checkOpen()
Check that this statement is still open.
void
clearParameters()
void
close()
boolean
execute()
boolean
execute(String sql)
boolean
execute(String arg0, String[] arg1)
boolean
execute(String arg0, int arg1)
boolean
execute(String arg0, int[] arg1)
protected SQLException
executeMSBatch(int size, int executeSize, ArrayList counts)
Execute the SQL batch on a MS server.
ResultSet
executeQuery()
ResultSet
executeQuery(String sql)
protected SQLException
executeSybaseBatch(int size, int executeSize, ArrayList counts)
Execute the SQL batch on a Sybase server.
int
executeUpdate()
int
executeUpdate(String sql)
int
executeUpdate(String arg0, String[] arg1)
int
executeUpdate(String sql, int getKeys)
int
executeUpdate(String arg0, int[] arg1)
ResultSetMetaData
getMetaData()
protected ParamInfo
getParameter(int parameterIndex)
Check the supplied index and return the selected parameter.
ParameterMetaData
getParameterMetaData()
protected static String
normalizeCall(String sql)
This method converts native call syntax into (hopefully) valid JDBC escape syntax.
protected void
notSupported(String method)
Report that user tried to call a method not supported on this type of statement.
void
setArray(int arg0, Array arg1)
void
setAsciiStream(int parameterIndex, InputStream inputStream, int length)
void
setBigDecimal(int parameterIndex, BigDecimal x)
void
setBinaryStream(int parameterIndex, InputStream x, int length)
void
setBlob(int parameterIndex, Blob x)
void
setBoolean(int parameterIndex, boolean x)
void
setByte(int parameterIndex, byte x)
void
setBytes(int parameterIndex, byte[] x)
void
setCharacterStream(int parameterIndex, Reader reader, int length)
void
setClob(int parameterIndex, Clob x)
(package private) void
setColMetaData(ColInfo[] value)
Update the cached column meta data information.
void
setDate(int parameterIndex, Date x)
void
setDate(int parameterIndex, Date x, Calendar cal)
void
setDouble(int parameterIndex, double x)
void
setFloat(int parameterIndex, float x)
void
setInt(int parameterIndex, int x)
void
setLong(int parameterIndex, long x)
void
setNull(int parameterIndex, int sqlType)
void
setNull(int parameterIndex, int sqlType, String typeName)
void
setObject(int parameterIndex, Object x)
void
setObject(int parameterIndex, Object x, int targetSqlType)
void
setObject(int parameterIndex, Object x, int targetSqlType, int scale)
void
setObjectBase(int parameterIndex, Object x, int targetSqlType, int scale)
Generic setObject method.
(package private) void
setParamMetaData(ParamInfo[] value)
Update the cached parameter meta data information.
protected void
setParameter(int parameterIndex, Object x, int targetSqlType, int scale, int length)
Update the ParamInfo object for the specified parameter.
void
setRef(int parameterIndex, Ref x)
void
setShort(int parameterIndex, short x)
void
setString(int parameterIndex, String x)
void
setTime(int parameterIndex, Time x)
void
setTime(int parameterIndex, Time x, Calendar cal)
void
setTimestamp(int parameterIndex, Timestamp x)
void
setTimestamp(int parameterIndex, Timestamp x, Calendar cal)
void
setURL(int parameterIndex, URL url)
void
setUnicodeStream(int parameterIndex, InputStream inputStream, int length)

Methods inherited from class net.sourceforge.jtds.jdbc.JtdsStatement

addBatch, addWarning, cacheResults, cancel, checkCursorException, checkOpen, clearBatch, clearWarnings, close, closeAllResultSets, closeCurrentResultSet, execute, execute, execute, execute, executeBatch, executeImpl, executeMSBatch, executeQuery, executeSQL, executeSQLQuery, executeSybaseBatch, executeUpdate, executeUpdate, executeUpdate, executeUpdate, finalize, getConnection, getDefaultFetchSize, getFetchDirection, getFetchSize, getGeneratedKeys, getMaxFieldSize, getMaxRows, getMessages, getMoreResults, getMoreResults, getQueryTimeout, getResultSet, getResultSetConcurrency, getResultSetHoldability, getResultSetType, getTds, getUpdateCount, getWarnings, initialize, notImplemented, processResults, setCursorName, setEscapeProcessing, setFetchDirection, setFetchSize, setMaxFieldSize, setMaxRows, setQueryTimeout, useCursor

Field Details

f

private static final NumberFormat f
Used to format numeric values when scale is specified.

handles

(package private)  Collection handles
Collection of handles used by this statement

paramMetaData

protected ParamInfo[] paramMetaData
The cached parameter meta data.

parameters

protected ParamInfo[] parameters
The parameter list for the call.

procName

protected String procName
The procedure name for CallableStatements.

returnKeys

private boolean returnKeys
True to return generated keys.

sql

protected final String sql
The SQL statement being prepared.

sqlWord

protected String sqlWord
The first SQL keyword in the SQL string.

Constructor Details

JtdsPreparedStatement

(package private)  JtdsPreparedStatement(ConnectionJDBC2 connection,
                                         String sql,
                                         int resultSetType,
                                         int concurrency,
                                         boolean returnKeys)
            throws SQLException
Construct a new preparedStatement object.
Parameters:
connection - The parent connection.
sql - The SQL statement to prepare.
resultSetType - The result set type eg SCROLLABLE etc.
concurrency - The result set concurrency eg READONLY.
returnKeys - True if generated keys should be returned.

Method Details

addBatch

public void addBatch()
            throws SQLException

addBatch

public void addBatch(String sql)
            throws SQLException
Overrides:
addBatch in interface JtdsStatement

checkOpen

protected void checkOpen()
            throws SQLException
Check that this statement is still open.
Overrides:
checkOpen in interface JtdsStatement

clearParameters

public void clearParameters()
            throws SQLException

close

public void close()
            throws SQLException
Overrides:
close in interface JtdsStatement

execute

public boolean execute()
            throws SQLException

execute

public boolean execute(String sql)
            throws SQLException
Overrides:
execute in interface JtdsStatement

execute

public boolean execute(String arg0,
                       String[] arg1)
            throws SQLException
Overrides:
execute in interface JtdsStatement

execute

public boolean execute(String arg0,
                       int arg1)
            throws SQLException
Overrides:
execute in interface JtdsStatement

execute

public boolean execute(String arg0,
                       int[] arg1)
            throws SQLException
Overrides:
execute in interface JtdsStatement

executeMSBatch

protected SQLException executeMSBatch(int size,
                                      int executeSize,
                                      ArrayList counts)
            throws SQLException
Execute the SQL batch on a MS server.

When running with prepareSQL=1 or 3, the driver will first prepare temporary stored procedures or statements for each parameter combination found in the batch. The handles to these pre-preared statements will then be used to execute the actual batch statements.

Overrides:
executeMSBatch in interface JtdsStatement
Parameters:
size - the total size of the batch
executeSize - the maximum number of statements to send in one request
counts - the returned update counts
Returns:
chained exceptions linked to a SQLException

executeQuery

public ResultSet executeQuery()
            throws SQLException

executeQuery

public ResultSet executeQuery(String sql)
            throws SQLException
Overrides:
executeQuery in interface JtdsStatement

executeSybaseBatch

protected SQLException executeSybaseBatch(int size,
                                          int executeSize,
                                          ArrayList counts)
            throws SQLException
Execute the SQL batch on a Sybase server.

Sybase needs to have the SQL concatenated into one TDS language packet followed by up to 1000 parameters. This method will be overriden for CallableStatements.

Overrides:
executeSybaseBatch in interface JtdsStatement
Parameters:
size - the total size of the batch
executeSize - the maximum number of statements to send in one request
counts - the returned update counts
Returns:
chained exceptions linked to a SQLException

executeUpdate

public int executeUpdate()
            throws SQLException

executeUpdate

public int executeUpdate(String sql)
            throws SQLException
Overrides:
executeUpdate in interface JtdsStatement

executeUpdate

public int executeUpdate(String arg0,
                         String[] arg1)
            throws SQLException
Overrides:
executeUpdate in interface JtdsStatement

executeUpdate

public int executeUpdate(String sql,
                         int getKeys)
            throws SQLException
Overrides:
executeUpdate in interface JtdsStatement

executeUpdate

public int executeUpdate(String arg0,
                         int[] arg1)
            throws SQLException
Overrides:
executeUpdate in interface JtdsStatement

getMetaData

public ResultSetMetaData getMetaData()
            throws SQLException

getParameter

protected ParamInfo getParameter(int parameterIndex)
            throws SQLException
Check the supplied index and return the selected parameter.
Parameters:
parameterIndex - the parameter index 1 to n.
Returns:
the parameter as a ParamInfo object.

getParameterMetaData

public ParameterMetaData getParameterMetaData()
            throws SQLException

normalizeCall

protected static String normalizeCall(String sql)
This method converts native call syntax into (hopefully) valid JDBC escape syntax.

Note: This method is required for backwards compatibility with previous versions of jTDS. Strictly speaking only the JDBC syntax needs to be recognised, constructions such as "?=#testproc ?,?" are neither valid native syntax nor valid escapes. All the substrings and trims below are not as bad as they look. The objects created all refer back to the original sql string it is just the start and length positions which change.

Parameters:
sql - the SQL statement to process
Returns:
the SQL, possibly in original form

notSupported

protected void notSupported(String method)
            throws SQLException
Report that user tried to call a method not supported on this type of statement.
Parameters:
method - The method name to report in the error message.

setArray

public void setArray(int arg0,
                     Array arg1)
            throws SQLException

setAsciiStream

public void setAsciiStream(int parameterIndex,
                           InputStream inputStream,
                           int length)
            throws SQLException

setBigDecimal

public void setBigDecimal(int parameterIndex,
                          BigDecimal x)
            throws SQLException

setBinaryStream

public void setBinaryStream(int parameterIndex,
                            InputStream x,
                            int length)
            throws SQLException

setBlob

public void setBlob(int parameterIndex,
                    Blob x)
            throws SQLException

setBoolean

public void setBoolean(int parameterIndex,
                       boolean x)
            throws SQLException

setByte

public void setByte(int parameterIndex,
                    byte x)
            throws SQLException

setBytes

public void setBytes(int parameterIndex,
                     byte[] x)
            throws SQLException

setCharacterStream

public void setCharacterStream(int parameterIndex,
                               Reader reader,
                               int length)
            throws SQLException

setClob

public void setClob(int parameterIndex,
                    Clob x)
            throws SQLException

setColMetaData

(package private)  void setColMetaData(ColInfo[] value)
Update the cached column meta data information.
Parameters:
value - The Column meta data array.

setDate

public void setDate(int parameterIndex,
                    Date x)
            throws SQLException

setDate

public void setDate(int parameterIndex,
                    Date x,
                    Calendar cal)
            throws SQLException

setDouble

public void setDouble(int parameterIndex,
                      double x)
            throws SQLException

setFloat

public void setFloat(int parameterIndex,
                     float x)
            throws SQLException

setInt

public void setInt(int parameterIndex,
                   int x)
            throws SQLException

setLong

public void setLong(int parameterIndex,
                    long x)
            throws SQLException

setNull

public void setNull(int parameterIndex,
                    int sqlType)
            throws SQLException

setNull

public void setNull(int parameterIndex,
                    int sqlType,
                    String typeName)
            throws SQLException

setObject

public void setObject(int parameterIndex,
                      Object x)
            throws SQLException

setObject

public void setObject(int parameterIndex,
                      Object x,
                      int targetSqlType)
            throws SQLException

setObject

public void setObject(int parameterIndex,
                      Object x,
                      int targetSqlType,
                      int scale)
            throws SQLException

setObjectBase

public void setObjectBase(int parameterIndex,
                          Object x,
                          int targetSqlType,
                          int scale)
            throws SQLException
Generic setObject method.
Parameters:
parameterIndex - Parameter index 1 to n.
x - The value to set.
targetSqlType - The java.sql.Types constant describing the data.
scale - The decimal scale -1 if not set.

setParamMetaData

(package private)  void setParamMetaData(ParamInfo[] value)
Update the cached parameter meta data information.
Parameters:
value - The Column meta data array.

setParameter

protected void setParameter(int parameterIndex,
                            Object x,
                            int targetSqlType,
                            int scale,
                            int length)
            throws SQLException
Update the ParamInfo object for the specified parameter.
Parameters:
parameterIndex - Parameter index 1 to n.
x - The value to set.
targetSqlType - The java.sql.Types constant describing the data.
scale - The decimal scale -1 if not set.
length - The length of the data item.

setRef

public void setRef(int parameterIndex,
                   Ref x)
            throws SQLException

setShort

public void setShort(int parameterIndex,
                     short x)
            throws SQLException

setString

public void setString(int parameterIndex,
                      String x)
            throws SQLException

setTime

public void setTime(int parameterIndex,
                    Time x)
            throws SQLException

setTime

public void setTime(int parameterIndex,
                    Time x,
                    Calendar cal)
            throws SQLException

setTimestamp

public void setTimestamp(int parameterIndex,
                         Timestamp x)
            throws SQLException

setTimestamp

public void setTimestamp(int parameterIndex,
                         Timestamp x,
                         Calendar cal)
            throws SQLException

setURL

public void setURL(int parameterIndex,
                   URL url)
            throws SQLException

setUnicodeStream

public void setUnicodeStream(int parameterIndex,
                             InputStream inputStream,
                             int length)
            throws SQLException

Generated on September 18 2009