|
|
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.
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.
statement.clearBatch()
Clears the list of statements in the batch that have been added by the addBatch() method.
statement.clearWarnings()
Clears all the warnings associated with the statement.
statement.close()
Frees all resources associated with the statement.
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.
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.
statement.executeQuery(String sql) preparedstatement.executeQuery()
Executes a query that returns data (such as SELECT or SHOW) and returns a single result set.
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.
statement.getConnection()
Returns the connection object that created the statement.
statement.getFetchSize()
Returns as an integer the number of the default fetch size for a ResultSet object from this statement.
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.
statement.getMaxRows()
Returns as an integer the maximum number of rows it's possible for a ResultSet object from this statement to contain.
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).
statement.getQueryTimeout()
Returns the number of seconds the driver will wait for a query to execute before it times out.
statement.getResultSet()
Returns a result set from the current statement.
statement.getResultSetType()
Returns the type for ResultSet objects for the current statement.
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.
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();
statement.setCursorName(String cursorname)
Sets the SQL cursor name to be used by later execute() methods.
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.
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.
statement.setMaxFieldSize(int limit)
Sets the maximum number of bytes in a binary or character ResultSet column.
statement.setMaxRows(int limit)
Sets the maximum number of rows that a ResultObject can contain.
statement.setQueryTimeout(int seconds)
Sets the number of seconds the driver will wait for a query to execute before it times out.
|
|