Statement and Prepared Statement Methods
These methods must be invoked via a valid Statement or PreparedStatement object.Most of these methods apply to both statements and prepared statements. Those with preparedstatement as an object can only be called with a prepared statement, and the methods with statement as an object can be called by either.
addBatch
statement.addBatch(String sql)
preparedstatement.addBatch()
Adds the SQL statement to a current list of statements, which can then be executed with the executeBatch() method.
clearBatch
statement.clearBatch()
Clears the list of statements in the batch that have been added by the addBatch() method.
clearWarnings
statement.clearWarnings()
Clears all the warnings associated with the statement.
close
statement.close()
Frees all resources associated with the statement.
execute
statement.execute(String sql [,int autoGeneratedKeys | int[]
columnIndexesÃ
| String[] columnNames])
preparestatement.execute()
Executes a SQL statement. It returns true if the query returns a result set (such as for a SELECT statement), and returns false if no result set was produced (such as for an INSERT or UPDATE statement). The options indicate that auto-generated keys should be made available for retrieval—either all of them or all in the integer or string arrays, respectively.
executeBatch
statement.executeBatch()
Executes all statements in the batch (added by addBatch), returning an integer array of update counts, or returning false if any of the statements did not execute correctly.
executeQuery
statement.executeQuery(String sql)
preparedstatement.executeQuery()
Executes a query that returns data (such as SELECT or SHOW) and returns a single result set.
executeUpdate
statement.executeUpdate(String sql)
preparedstatement.executeUpdate()
Executes a query that modifies data (such as UPDATE, INSERT, or ALTER) and returns the number of rows affected.
getConnection
statement.getConnection()
Returns the connection object that created the statement.
getFetchSize
statement.getFetchSize()
Returns as an integer the number of the default fetch size for a ResultSet object from this statement.
getMaxFieldSize
statement.getMaxFieldSize()
Returns as an integer the maximum number of bytes that can be returned for character and binary column values for a ResultSet object from this statement.
getMaxRows
statement.getMaxRows()
Returns as an integer the maximum number of rows it's possible for a ResultSet object from this statement to contain.
getMoreResults
statement.getMoreResults([int current])
Moves to the next result from the statement, returning true if there is another valid ResultSet, or returning false if not. If there's no parameter, any current ResultSet objects are closed; otherwise they are dealt with according to the value of current (which can be CLOSE_ CURRENT_RESULT, KEEP_CURRENT_RESULT, or CLOSE_ALL_RESULTS).
getQueryTimeout
statement.getQueryTimeout()
Returns the number of seconds the driver will wait for a query to execute before it times out.
getResultSet
statement.getResultSet()
Returns a result set from the current statement.
getResultSetType
statement.getResultSetType()
Returns the type for ResultSet objects for the current statement.
getUpdateCount
statement.getUpdateCount()
Retrieves the current result as an update count; if the result is a ResultSet object or there are no more results, –1 is returned.
setXXX
preparedstatement.setXXX(int parameter, xxx value)
Sets a parameter in a previously prepared statement. The parameters start at 1. The value is of the appropriate type (see Table F.2).
SQL Type | Java Method |
---|---|
BIGINT | setLong() |
BINARY | setBytes() |
BIT | setBoolean() |
BLOB | SetBlob() |
CHAR | setString() |
DATE | setDate() |
DECIMAL | setBigDecimal() |
DOUBLE | setDouble() |
FLOAT | setDouble() |
INTEGER | setInt() |
LONGVARBINARY | setBytes() |
LONGVARCHAR | setString() |
NUMERIC | setBigDecimal() |
OTHER | setObject() |
REAL | setFloat() |
SMALLINT | setShort() |
TIME | setTime() |
TIMESTAMP | setTimestamp() |
TINYINT | setByte() |
VARBINARY | setBytes() |
VARCHAR | setString() |
For example:
preparedstatement = connection.prepareStatement("UPDATE customer–
SET surname = ? WHERE id=?");
preparedstatement.setString(1,"Burger");
preparedstatement.setInt(2,9);
preparedstatement.executeUpdate();
setCursorName
statement.setCursorName(String cursorname)
Sets the SQL cursor name to be used by later execute() methods.
setEscapeProcessing
statement.setEscapeProcessing(boolean mode)
Sets escape processing (if mode is true) or disables it (if mode is false). The default is true. Escape processing has no effect with PreparedStatement objects.
setFetchSize
statement.setFetchSize(int size)
Gives the driver an idea of how many rows should be returned from the database when more rows are needed for the statement.
setMaxFieldSize
statement.setMaxFieldSize(int limit)
Sets the maximum number of bytes in a binary or character ResultSet column.
setMaxRows
statement.setMaxRows(int limit)
Sets the maximum number of rows that a ResultObject can contain.
setQueryTimeout
statement.setQueryTimeout(int seconds)
Sets the number of seconds the driver will wait for a query to execute before it times out.