Home » MySQL-JDBC-5.1.11 » com.mysql.jdbc » [javadoc | source]
com.mysql.jdbc
public class: PreparedStatement [javadoc | source]
java.lang.Object
   com.mysql.jdbc.StatementImpl
      com.mysql.jdbc.PreparedStatement

All Implemented Interfaces:
    PreparedStatement, Statement

Direct Known Subclasses:
    CallableStatement, JDBC4PreparedStatement, JDBC4CallableStatement, JDBC4ServerPreparedStatement, ServerPreparedStatement

A SQL Statement is pre-compiled and stored in a PreparedStatement object. This object can then be used to efficiently execute this statement multiple times.

Note: The setXXX methods for setting IN parameter values must specify types that are compatible with the defined SQL type of the input parameter. For instance, if the IN parameter has SQL type Integer, then setInt should be used.

If arbitrary parameter type conversions are required, then the setObject method should be used with a target SQL type.

Nested Class Summary:
class  PreparedStatement.BatchParams   
class  PreparedStatement.EndPoint   
class  PreparedStatement.ParseInfo   
interface  PreparedStatement.BatchVisitor   
class  PreparedStatement.AppendingBatchVisitor   
class  PreparedStatement.EmulatedPreparedStatementBindings   
Field Summary
protected  boolean batchHasPlainStatements    Does the batch (if any) contain "plain" statements added by Statement.addBatch(String)? If so, we can't re-write it to use multi-value or multi-queries. 
protected  char firstCharOfStmt    What is the first character of the prepared statement (used to check for SELECT vs. INSERT/UPDATE/DELETE) 
protected  boolean hasLimitClause    Does the SQL for this statement contain a 'limit' clause? 
protected  boolean isLoadDataQuery    Is this query a LOAD DATA query? 
protected  int numberOfExecutions     
protected  String originalSql    The SQL that was passed in to 'prepare' 
protected  int parameterCount    The number of parameters in this PreparedStatement 
protected  MysqlParameterMetadata parameterMetaData     
protected  int[] parameterTypes    Only used by statement interceptors at the moment to provide introspection of bound values 
protected  ParseInfo parseInfo     
protected  boolean useTrueBoolean    Are we using a version of MySQL where we can use 'true' boolean values? 
protected  boolean usingAnsiMode     
protected  String batchedValuesClause     
protected  int rewrittenBatchSize     
Fields inherited from com.mysql.jdbc.StatementImpl:
PING_MARKER,  cancelTimeoutMutex,  statementCounter,  USES_VARIABLES_FALSE,  USES_VARIABLES_TRUE,  USES_VARIABLES_UNKNOWN,  wasCancelled,  wasCancelledByTimeout,  batchedArgs,  charConverter,  charEncoding,  connection,  connectionId,  currentCatalog,  doEscapeProcessing,  eventSink,  isClosed,  lastInsertId,  maxFieldSize,  maxRows,  maxRowsChanged,  openResults,  pedantic,  pointOfOrigin,  profileSQL,  results,  resultSetConcurrency,  resultSetType,  statementId,  timeoutInMillis,  updateCount,  useUsageAdvisor,  warningChain,  holdResultsOpenOverClose,  batchedGeneratedKeys,  retrieveGeneratedKeys,  continueBatchOnError,  pingTarget,  useLegacyDatetimeCode,  lastQueryIsOnDupKeyUpdate
Constructor:
 public PreparedStatement(ConnectionImpl conn,
    String catalog) throws SQLException 
    Constructor used by server-side prepared statements
    Parameters:
    conn - the connection that created us
    catalog - the catalog in use when we were created
    Throws:
    SQLException - if an error occurs
 public PreparedStatement(ConnectionImpl conn,
    String sql,
    String catalog) throws SQLException 
    Constructor for the PreparedStatement class.
    Parameters:
    conn - the connection creating this statement
    sql - the SQL for this statement
    catalog - the catalog/database this statement should be issued against
    Throws:
    SQLException - if a database error occurs.
 public PreparedStatement(ConnectionImpl conn,
    String sql,
    String catalog,
    ParseInfo cachedParseInfo) throws SQLException 
    Creates a new PreparedStatement object.
    Parameters:
    conn - the connection creating this statement
    sql - the SQL for this statement
    catalog - the catalog/database this statement should be issued against
    cachedParseInfo - already created parseInfo.
    Throws:
    SQLException - DOCUMENT ME!
Method from com.mysql.jdbc.PreparedStatement Summary:
addBatch,   addBatch,   asSql,   asSql,   canRewrite,   canRewriteAsMultiValueInsertAtSqlLevel,   checkReadOnlySafeStatement,   clearBatch,   clearParameters,   close,   computeBatchSize,   computeMaxParameterSetSizeAndBatchSize,   containsOnDuplicateKeyUpdateInSQL,   execute,   executeBatch,   executeBatchSerially,   executeBatchedInserts,   executeInternal,   executePreparedBatchAsMultiStatement,   executeQuery,   executeUpdate,   executeUpdate,   executeUpdate,   fillSendPacket,   fillSendPacket,   getBytesRepresentation,   getBytesRepresentationForBatch,   getInstance,   getInstance,   getInstance,   getLocationOfOnDuplicateKeyUpdate,   getMetaData,   getNonRewrittenSql,   getParameterBindings,   getParameterIndexOffset,   getParameterMetaData,   getParseInfo,   getPreparedSql,   getRewrittenBatchSize,   getUpdateCount,   getValuesClause,   isClosed,   isNull,   isSelectQuery,   prepareBatchedInsertSQL,   readFully,   realClose,   setArray,   setAsciiStream,   setAsciiStream,   setAsciiStream,   setBigDecimal,   setBinaryStream,   setBinaryStream,   setBinaryStream,   setBlob,   setBlob,   setBlob,   setBoolean,   setByte,   setBytes,   setBytes,   setBytesNoEscape,   setBytesNoEscapeNoQuotes,   setCharacterStream,   setCharacterStream,   setCharacterStream,   setClob,   setClob,   setClob,   setDate,   setDate,   setDouble,   setFloat,   setInt,   setInternal,   setInternal,   setLong,   setNCharacterStream,   setNCharacterStream,   setNClob,   setNClob,   setNString,   setNull,   setNull,   setObject,   setObject,   setObject,   setOneBatchedParameterSet,   setRef,   setResultSetConcurrency,   setResultSetType,   setRetrieveGeneratedKeys,   setShort,   setString,   setTime,   setTime,   setTimestamp,   setTimestamp,   setURL,   setUnicodeStream,   toString
Methods from com.mysql.jdbc.StatementImpl:
addBatch,   cancel,   checkClosed,   checkForDml,   checkNullOrEmptyQuery,   clearBatch,   clearWarnings,   close,   closeAllOpenResults,   containsOnDuplicateKeyInString,   createStreamingResultSet,   disableStreamingResults,   doPingInstead,   enableStreamingResults,   execute,   execute,   execute,   execute,   executeBatch,   executeQuery,   executeSimpleNonQuery,   executeUpdate,   executeUpdate,   executeUpdate,   executeUpdate,   executeUpdate,   findStartOfStatement,   generatePingResultSet,   getBatchedGeneratedKeys,   getBatchedGeneratedKeys,   getCalendarInstanceForSessionOrNew,   getConnection,   getExceptionInterceptor,   getFetchDirection,   getFetchSize,   getGeneratedKeys,   getGeneratedKeysInternal,   getGeneratedKeysInternal,   getId,   getLastInsertID,   getLocalInfileInputStream,   getLongUpdateCount,   getMaxFieldSize,   getMaxRows,   getMoreResults,   getMoreResults,   getOnDuplicateKeyLocation,   getOpenResultSetCount,   getQueryTimeout,   getResultSet,   getResultSetConcurrency,   getResultSetHoldability,   getResultSetInternal,   getResultSetType,   getUpdateCount,   getWarnings,   handleExceptionForBatch,   hasDeadlockOrTimeoutRolledBackTx,   isClosed,   isPoolable,   isWrapperFor,   processMultiCountsAndKeys,   realClose,   removeOpenResultSet,   resetCancelledState,   setCursorName,   setEscapeProcessing,   setFetchDirection,   setFetchSize,   setHoldResultsOpenOverClose,   setLocalInfileInputStream,   setMaxFieldSize,   setMaxRows,   setPingTarget,   setPoolable,   setQueryTimeout,   setResultSetConcurrency,   setResultSetType,   unwrap
Methods from java.lang.Object:
clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from com.mysql.jdbc.PreparedStatement Detail:
 public  void addBatch() throws SQLException 
    JDBC 2.0 Add a set of parameters to the batch.
 public synchronized  void addBatch(String sql) throws SQLException 
 protected String asSql() throws SQLException 
 protected String asSql(boolean quoteStreamsAndUnknowns) throws SQLException 
 protected static boolean canRewrite(String sql,
    boolean isOnDuplicateKeyUpdate,
    int locationOfOnDuplicateKeyUpdate,
    int statementStartPos) 
 public boolean canRewriteAsMultiValueInsertAtSqlLevel() throws SQLException 
 protected boolean checkReadOnlySafeStatement() throws SQLException 
    Check to see if the statement is safe for read-only slaves after failover.
 public synchronized  void clearBatch() throws SQLException 
 public synchronized  void clearParameters() throws SQLException 
    In general, parameter values remain in force for repeated used of a Statement. Setting a parameter value automatically clears its previous value. However, in some cases, it is useful to immediately release the resources used by the current parameter values; this can be done by calling clearParameters
 public synchronized  void close() throws SQLException 
    Closes this prepared statement and releases all resources.
 protected int computeBatchSize(int numBatchedArgs) throws SQLException 
    Computes the optimum number of batched parameter lists to send without overflowing max_allowed_packet.
 protected long[] computeMaxParameterSetSizeAndBatchSize(int numBatchedArgs) throws SQLException 
    Computes the maximum parameter set size, and entire batch size given the number of arguments in the batch.
 protected boolean containsOnDuplicateKeyUpdateInSQL() 
 public boolean execute() throws SQLException 
    Some prepared statements return multiple results; the execute method handles these complex statements as well as the simpler form of statements handled by executeQuery and executeUpdate
 public int[] executeBatch() throws SQLException 
    JDBC 2.0 Submit a batch of commands to the database for execution. This method is optional.
 protected int[] executeBatchSerially(int batchTimeout) throws SQLException 
    Executes the current batch of statements by executing them one-by-one.
 protected int[] executeBatchedInserts(int batchTimeout) throws SQLException 
    Rewrites the already prepared statement into a multi-value insert statement of 'statementsPerBatch' values and executes the entire batch using this new statement.
 protected ResultSetInternalMethods executeInternal(int maxRowsToRetrieve,
    Buffer sendPacket,
    boolean createStreamingResultSet,
    boolean queryIsSelectOnly,
    Field[] metadataFromCache,
    boolean isBatch) throws SQLException 
    Actually execute the prepared statement. This is here so server-side PreparedStatements can re-use most of the code from this class.
 protected int[] executePreparedBatchAsMultiStatement(int batchTimeout) throws SQLException 
    Rewrites the already prepared statement into a multi-statement query of 'statementsPerBatch' values and executes the entire batch using this new statement.
 public ResultSet executeQuery() throws SQLException 
    A Prepared SQL query is executed and its ResultSet is returned
 public int executeUpdate() throws SQLException 
    Execute a SQL INSERT, UPDATE or DELETE statement. In addition, SQL statements that return nothing such as SQL DDL statements can be executed.
 protected int executeUpdate(boolean clearBatchedGeneratedKeysAndWarnings,
    boolean isBatch) throws SQLException 
 protected int executeUpdate(byte[][] batchedParameterStrings,
    InputStream[] batchedParameterStreams,
    boolean[] batchedIsStream,
    int[] batchedStreamLengths,
    boolean[] batchedIsNull,
    boolean isReallyBatch) throws SQLException 
    Added to allow batch-updates
 protected Buffer fillSendPacket() throws SQLException 
    Creates the packet that contains the query to be sent to the server.
 protected Buffer fillSendPacket(byte[][] batchedParameterStrings,
    InputStream[] batchedParameterStreams,
    boolean[] batchedIsStream,
    int[] batchedStreamLengths) throws SQLException 
    Creates the packet that contains the query to be sent to the server.
 public byte[] getBytesRepresentation(int parameterIndex) throws SQLException 
    DOCUMENT ME!
 protected byte[] getBytesRepresentationForBatch(int parameterIndex,
    int commandIndex) throws SQLException 
    Get bytes representation for a parameter in a statement batch.
 protected static PreparedStatement getInstance(ConnectionImpl conn,
    String catalog) throws SQLException 
    Creates a prepared statement instance -- We need to provide factory-style methods so we can support both JDBC3 (and older) and JDBC4 runtimes, otherwise the class verifier complains when it tries to load JDBC4-only interface classes that are present in JDBC4 method signatures.
 protected static PreparedStatement getInstance(ConnectionImpl conn,
    String sql,
    String catalog) throws SQLException 
    Creates a prepared statement instance -- We need to provide factory-style methods so we can support both JDBC3 (and older) and JDBC4 runtimes, otherwise the class verifier complains when it tries to load JDBC4-only interface classes that are present in JDBC4 method signatures.
 protected static PreparedStatement getInstance(ConnectionImpl conn,
    String sql,
    String catalog,
    ParseInfo cachedParseInfo) throws SQLException 
    Creates a prepared statement instance -- We need to provide factory-style methods so we can support both JDBC3 (and older) and JDBC4 runtimes, otherwise the class verifier complains when it tries to load JDBC4-only interface classes that are present in JDBC4 method signatures.
 protected int getLocationOfOnDuplicateKeyUpdate() 
 public ResultSetMetaData getMetaData() throws SQLException 
    The number, types and properties of a ResultSet's columns are provided by the getMetaData method.
 public String getNonRewrittenSql() 
 public ParameterBindings getParameterBindings() throws SQLException 
 protected int getParameterIndexOffset() 
    For calling stored functions, this will be -1 as we don't really count the first '?' parameter marker, it's only syntax, but JDBC counts it as #1, otherwise it will return 0
 public ParameterMetaData getParameterMetaData() throws SQLException 
 ParseInfo getParseInfo() 
 public String getPreparedSql() 
 public int getRewrittenBatchSize() 
 public int getUpdateCount() throws SQLException 
 protected String getValuesClause() throws SQLException 
 public synchronized boolean isClosed() throws SQLException 
 boolean isNull(int paramIndex) 
 protected boolean isSelectQuery() 
 protected PreparedStatement prepareBatchedInsertSQL(ConnectionImpl localConn,
    int numBatches) throws SQLException 
    Returns a prepared statement for the number of batched parameters, used when re-writing batch INSERTs.
 protected static int readFully(Reader reader,
    char[] buf,
    int length) throws IOException 
    Reads length bytes from reader into buf. Blocks until enough input is available
 protected  void realClose(boolean calledExplicitly,
    boolean closeOpenResults) throws SQLException 
    Closes this statement, releasing all resources
 public  void setArray(int i,
    Array x) throws SQLException 
    JDBC 2.0 Set an Array parameter.
 public  void setAsciiStream(int parameterIndex,
    InputStream x) throws SQLException 
 public  void setAsciiStream(int parameterIndex,
    InputStream x,
    int length) throws SQLException 
    When a very large ASCII value is input to a LONGVARCHAR parameter, it may be more practical to send it via a java.io.InputStream. JDBC will read the data from the stream as needed, until it reaches end-of-file. The JDBC driver will do any necessary conversion from ASCII to the database char format.

    Note: This stream object can either be a standard Java stream object or your own subclass that implements the standard interface.

 public  void setAsciiStream(int parameterIndex,
    InputStream x,
    long length) throws SQLException 
 public  void setBigDecimal(int parameterIndex,
    BigDecimal x) throws SQLException 
    Set a parameter to a java.math.BigDecimal value. The driver converts this to a SQL NUMERIC value when it sends it to the database.
 public  void setBinaryStream(int parameterIndex,
    InputStream x) throws SQLException 
 public  void setBinaryStream(int parameterIndex,
    InputStream x,
    int length) throws SQLException 
    When a very large binary value is input to a LONGVARBINARY parameter, it may be more practical to send it via a java.io.InputStream. JDBC will read the data from the stream as needed, until it reaches end-of-file.

    Note: This stream object can either be a standard Java stream object or your own subclass that implements the standard interface.

 public  void setBinaryStream(int parameterIndex,
    InputStream x,
    long length) throws SQLException 
 public  void setBlob(int i,
    Blob x) throws SQLException 
    JDBC 2.0 Set a BLOB parameter.
 public  void setBlob(int parameterIndex,
    InputStream inputStream) throws SQLException 
 public  void setBlob(int parameterIndex,
    InputStream inputStream,
    long length) throws SQLException 
 public  void setBoolean(int parameterIndex,
    boolean x) throws SQLException 
    Set a parameter to a Java boolean value. The driver converts this to a SQL BIT value when it sends it to the database.
 public  void setByte(int parameterIndex,
    byte x) throws SQLException 
    Set a parameter to a Java byte value. The driver converts this to a SQL TINYINT value when it sends it to the database.
 public  void setBytes(int parameterIndex,
    byte[] x) throws SQLException 
    Set a parameter to a Java array of bytes. The driver converts this to a SQL VARBINARY or LONGVARBINARY (depending on the argument's size relative to the driver's limits on VARBINARYs) when it sends it to the database.
 protected  void setBytes(int parameterIndex,
    byte[] x,
    boolean checkForIntroducer,
    boolean escapeForMBChars) throws SQLException 
 protected  void setBytesNoEscape(int parameterIndex,
    byte[] parameterAsBytes) throws SQLException 
    Used by updatable result sets for refreshRow() because the parameter has already been escaped for updater or inserter prepared statements.
 protected  void setBytesNoEscapeNoQuotes(int parameterIndex,
    byte[] parameterAsBytes) throws SQLException 
 public  void setCharacterStream(int parameterIndex,
    Reader reader) throws SQLException 
 public  void setCharacterStream(int parameterIndex,
    Reader reader,
    int length) throws SQLException 
    JDBC 2.0 When a very large UNICODE value is input to a LONGVARCHAR parameter, it may be more practical to send it via a java.io.Reader. JDBC will read the data from the stream as needed, until it reaches end-of-file. The JDBC driver will do any necessary conversion from UNICODE to the database char format.

    Note: This stream object can either be a standard Java stream object or your own subclass that implements the standard interface.

 public  void setCharacterStream(int parameterIndex,
    Reader reader,
    long length) throws SQLException 
 public  void setClob(int i,
    Clob x) throws SQLException 
    JDBC 2.0 Set a CLOB parameter.
 public  void setClob(int parameterIndex,
    Reader reader) throws SQLException 
 public  void setClob(int parameterIndex,
    Reader reader,
    long length) throws SQLException 
 public  void setDate(int parameterIndex,
    Date x) throws SQLException 
    Set a parameter to a java.sql.Date value. The driver converts this to a SQL DATE value when it sends it to the database.
 public  void setDate(int parameterIndex,
    Date x,
    Calendar cal) throws SQLException 
    Set a parameter to a java.sql.Date value. The driver converts this to a SQL DATE value when it sends it to the database.
 public  void setDouble(int parameterIndex,
    double x) throws SQLException 
    Set a parameter to a Java double value. The driver converts this to a SQL DOUBLE value when it sends it to the database
 public  void setFloat(int parameterIndex,
    float x) throws SQLException 
    Set a parameter to a Java float value. The driver converts this to a SQL FLOAT value when it sends it to the database.
 public  void setInt(int parameterIndex,
    int x) throws SQLException 
    Set a parameter to a Java int value. The driver converts this to a SQL INTEGER value when it sends it to the database.
 protected final  void setInternal(int paramIndex,
    byte[] val) throws SQLException 
 protected final  void setInternal(int paramIndex,
    String val) throws SQLException 
 public  void setLong(int parameterIndex,
    long x) throws SQLException 
    Set a parameter to a Java long value. The driver converts this to a SQL BIGINT value when it sends it to the database.
 public  void setNCharacterStream(int parameterIndex,
    Reader value) throws SQLException 
 public  void setNCharacterStream(int parameterIndex,
    Reader reader,
    long length) throws SQLException 
    JDBC 2.0 When a very large UNICODE value is input to a LONGVARCHAR parameter, it may be more practical to send it via a java.io.Reader. JDBC will read the data from the stream as needed, until it reaches end-of-file. The JDBC driver will do any necessary conversion from UNICODE to the database char format.

    Note: This stream object can either be a standard Java stream object or your own subclass that implements the standard interface.

 public  void setNClob(int parameterIndex,
    Reader reader) throws SQLException 
 public  void setNClob(int parameterIndex,
    Reader reader,
    long length) throws SQLException 
    JDBC 4.0 Set a NCLOB parameter.
 public  void setNString(int parameterIndex,
    String x) throws SQLException 
    Set a parameter to a Java String value. The driver converts this to a SQL VARCHAR or LONGVARCHAR value with introducer _utf8 (depending on the arguments size relative to the driver's limits on VARCHARs) when it sends it to the database. If charset is set as utf8, this method just call setString.
 public  void setNull(int parameterIndex,
    int sqlType) throws SQLException 
    Set a parameter to SQL NULL

    Note: You must specify the parameters SQL type (although MySQL ignores it)

 public  void setNull(int parameterIndex,
    int sqlType,
    String arg) throws SQLException 
    Set a parameter to SQL NULL.

    Note: You must specify the parameter's SQL type.

 public  void setObject(int parameterIndex,
    Object parameterObj) throws SQLException 
    DOCUMENT ME!
 public  void setObject(int parameterIndex,
    Object parameterObj,
    int targetSqlType) throws SQLException 
    DOCUMENT ME!
 public  void setObject(int parameterIndex,
    Object parameterObj,
    int targetSqlType,
    int scale) throws SQLException 
    Set the value of a parameter using an object; use the java.lang equivalent objects for integral values.

    The given Java object will be converted to the targetSqlType before being sent to the database.

    note that this method may be used to pass database-specific abstract data types. This is done by using a Driver-specific Java type and using a targetSqlType of java.sql.Types.OTHER

 protected int setOneBatchedParameterSet(PreparedStatement batchedStatement,
    int batchedParamIndex,
    Object paramSet) throws SQLException 
 public  void setRef(int i,
    Ref x) throws SQLException 
    JDBC 2.0 Set a REF(<structured-type>) parameter.
  void setResultSetConcurrency(int concurrencyFlag) 
    Sets the concurrency for result sets generated by this statement
  void setResultSetType(int typeFlag) 
    Sets the result set type for result sets generated by this statement
 protected  void setRetrieveGeneratedKeys(boolean retrieveGeneratedKeys) 
    DOCUMENT ME!
 public  void setShort(int parameterIndex,
    short x) throws SQLException 
    Set a parameter to a Java short value. The driver converts this to a SQL SMALLINT value when it sends it to the database.
 public  void setString(int parameterIndex,
    String x) throws SQLException 
    Set a parameter to a Java String value. The driver converts this to a SQL VARCHAR or LONGVARCHAR value (depending on the arguments size relative to the driver's limits on VARCHARs) when it sends it to the database.
 public  void setTime(int parameterIndex,
    Time x) throws SQLException 
    Set a parameter to a java.sql.Time value. The driver converts this to a SQL TIME value when it sends it to the database.
 public  void setTime(int parameterIndex,
    Time x,
    Calendar cal) throws SQLException 
    Set a parameter to a java.sql.Time value. The driver converts this to a SQL TIME value when it sends it to the database.
 public  void setTimestamp(int parameterIndex,
    Timestamp x) throws SQLException 
    Set a parameter to a java.sql.Timestamp value. The driver converts this to a SQL TIMESTAMP value when it sends it to the database.
 public  void setTimestamp(int parameterIndex,
    Timestamp x,
    Calendar cal) throws SQLException 
    Set a parameter to a java.sql.Timestamp value. The driver converts this to a SQL TIMESTAMP value when it sends it to the database.
 public  void setURL(int parameterIndex,
    URL arg) throws SQLException 
 public  void setUnicodeStream(int parameterIndex,
    InputStream x,
    int length) throws SQLException 
Deprecated!
    When a very large Unicode value is input to a LONGVARCHAR parameter, it may be more practical to send it via a java.io.InputStream. JDBC will read the data from the stream as needed, until it reaches end-of-file. The JDBC driver will do any necessary conversion from UNICODE to the database char format.

    Note: This stream object can either be a standard Java stream object or your own subclass that implements the standard interface.

 public String toString() 
    Returns this PreparedStatement represented as a string.