Interface Statement
- All Superinterfaces:
AutoCloseable,Wrapper
- All Known Subinterfaces:
CallableStatement,PreparedStatement
- All Known Implementing Classes:
JDBCPreparedStatement,JDBCStatement
public interface Statement extends Wrapper, AutoCloseable
ResultSets. For any given
Statement object, only one ResultSet can be opened at one
time. A call to any of the execution methods of Statement will cause
any previously created ResultSet object for that Statement to
be closed implicitly.
To have multiple ResultSet objects opened concurrently, multiple
Statement objects must be created and then executed.
To obtain such an executable statement one needs to invoke
Connection#createStatement.
- See Also:
ResultSet,Connection.createStatement()
-
Field Summary
Fields Modifier and Type Field Description static intCLOSE_ALL_RESULTSPassing this constant togetMoreResults()implies that allResultSetobjects previously kept open should be closed.static intCLOSE_CURRENT_RESULTPassing this constant togetMoreResults()implies that the currentResultSetobject should be closed.static intEXECUTE_FAILEDIndicates that an error was encountered during execution of a batch statement.static intKEEP_CURRENT_RESULTPassing this constant to getMoreResults implies that the currentResultSetobject should not be closed.static intNO_GENERATED_KEYSIndicates that generated keys should not be accessible for retrieval.static intRETURN_GENERATED_KEYSIndicates that generated keys should be accessible for retrieval.static intSUCCESS_NO_INFOIndicates that a batch statement was executed with a successful result, but a count of the number of rows it affected is unavailable. -
Method Summary
Modifier and Type Method Description voidaddBatch(String sql)Adds a specified SQL command to the list of commands for thisStatement.voidcancel()Cancels this statement's execution if both the database and the JDBC driver support aborting an SQL statement in flight.voidclearBatch()Clears the current list of SQL commands for this statement.voidclearWarnings()Clears allSQLWarningsfrom this statement.voidclose()Releases this statement's database and JDBC driver resources.booleanexecute(String sql)Executes a supplied SQL statement.booleanexecute(String sql, int autoGeneratedKeys)Executes a supplied SQL statement.booleanexecute(String sql, int[] columnIndexes)Executes the supplied SQL statement.booleanexecute(String sql, String[] columnNames)Executes the supplied SQL statement.int[]executeBatch()Submits a batch of SQL commands to the database.ResultSetexecuteQuery(String sql)Executes a supplied SQL statement.intexecuteUpdate(String sql)Executes the supplied SQL statement.intexecuteUpdate(String sql, int autoGeneratedKeys)Executes the supplied SQL statement.intexecuteUpdate(String sql, int[] columnIndexes)Executes the supplied SQL statement.intexecuteUpdate(String sql, String[] columnNames)Executes the supplied SQL statement.ConnectiongetConnection()Gets theConnectionobject which created this statement.intgetFetchDirection()Gets the default direction for fetching rows forResultSets generated from this statement.intgetFetchSize()Gets the default number of rows for a fetch for theResultSetobjects returned from this statement.ResultSetgetGeneratedKeys()Returns auto generated keys created by executing this statement.intgetMaxFieldSize()Gets the maximum number of bytes which can be returned as values from character and binary type columns in aResultSetderived from this statement.intgetMaxRows()Gets the maximum number of rows that aResultSetcan contain when produced from this statement.booleangetMoreResults()Moves to this statement's next result.booleangetMoreResults(int current)Moves to this statement's next result.intgetQueryTimeout()Gets the timeout value for the statement's execution time.ResultSetgetResultSet()Gets the current result.intgetResultSetConcurrency()Gets the concurrency setting forResultSetobjects generated by this statement.intgetResultSetHoldability()Gets the cursor hold setting forResultSetobjects generated by this statement.intgetResultSetType()Gets theResultSettype setting forResultSets derived from this statement.intgetUpdateCount()Gets an update count for the current result if it is not aResultSet.SQLWarninggetWarnings()Retrieves the firstSQLWarningreported by calls on this statement.booleanisClosed()Returns true if this statement has been closed, false otherwise.booleanisPoolable()Returns true if this statement is poolable, false otherwise.voidsetCursorName(String name)Sets the SQL cursor name.voidsetEscapeProcessing(boolean enable)Sets Escape Processing mode.voidsetFetchDirection(int direction)Sets the fetch direction - a hint to the JDBC driver about the direction of processing of rows inResultSets created by this statement.voidsetFetchSize(int rows)Sets the fetch size.voidsetMaxFieldSize(int max)Sets the maximum number of bytes forResultSetcolumns that contain character or binary values.voidsetMaxRows(int max)Sets the maximum number of rows that anyResultSetcan contain.voidsetPoolable(boolean poolable)Hints whether this statement should be pooled.voidsetQueryTimeout(int seconds)Sets the timeout, in seconds, for queries - how long the driver will allow for completion of a statement execution.Methods inherited from interface java.sql.Wrapper
isWrapperFor, unwrap
-
Field Details
-
CLOSE_ALL_RESULTS
static final int CLOSE_ALL_RESULTSPassing this constant togetMoreResults()implies that allResultSetobjects previously kept open should be closed.- See Also:
- Constant Field Values
-
CLOSE_CURRENT_RESULT
static final int CLOSE_CURRENT_RESULTPassing this constant togetMoreResults()implies that the currentResultSetobject should be closed.- See Also:
- Constant Field Values
-
EXECUTE_FAILED
static final int EXECUTE_FAILEDIndicates that an error was encountered during execution of a batch statement.- See Also:
- Constant Field Values
-
KEEP_CURRENT_RESULT
static final int KEEP_CURRENT_RESULTPassing this constant to getMoreResults implies that the currentResultSetobject should not be closed.- See Also:
- Constant Field Values
-
NO_GENERATED_KEYS
static final int NO_GENERATED_KEYSIndicates that generated keys should not be accessible for retrieval.- See Also:
- Constant Field Values
-
RETURN_GENERATED_KEYS
static final int RETURN_GENERATED_KEYSIndicates that generated keys should be accessible for retrieval.- See Also:
- Constant Field Values
-
SUCCESS_NO_INFO
static final int SUCCESS_NO_INFOIndicates that a batch statement was executed with a successful result, but a count of the number of rows it affected is unavailable.- See Also:
- Constant Field Values
-
-
Method Details
-
addBatch
Adds a specified SQL command to the list of commands for thisStatement.The list of commands is executed by invoking the
executeBatchmethod.- Parameters:
sql- the SQL command as a String. Typically anINSERTorUPDATEstatement.- Throws:
SQLException- if an error occurs accessing the database or the database does not support batch updates.
-
cancel
Cancels this statement's execution if both the database and the JDBC driver support aborting an SQL statement in flight. This method can be used by one thread to stop a statement that is executed on another thread.- Throws:
SQLException- if an error occurs accessing the database.
-
clearBatch
Clears the current list of SQL commands for this statement.- Throws:
SQLException- if an error occurs accessing the database or the database does not support batch updates.
-
clearWarnings
Clears allSQLWarningsfrom this statement.- Throws:
SQLException- if an error occurs accessing the database.
-
close
Releases this statement's database and JDBC driver resources.Using this method to release these resources as soon as possible is strongly recommended.
One should not rely on the resources being automatically released when finalized during garbage collection. Doing so can result in unpredictable behavior for the application.
- Specified by:
closein interfaceAutoCloseable- Throws:
SQLException- if an error occurs accessing the database.
-
execute
Executes a supplied SQL statement. This may return multipleResultSets.Use the
getResultSetorgetUpdateCountmethods to get the first result andgetMoreResultsto get any subsequent results.- Parameters:
sql- the SQL statement to execute- Returns:
trueif the first result is aResultSet,falseif the first result is an update count or if there is no result.- Throws:
SQLException- if an error occurs accessing the database.
-
execute
Executes a supplied SQL statement. This may return multipleResultSets. This method allows control of whether auto-generated Keys should be made available for retrieval, if the SQL statement is anINSERTstatement.Use the
getResultSetorgetUpdateCountmethods to get the first result andgetMoreResultsto get any subsequent results.- Parameters:
sql- the SQL statement to execute.autoGeneratedKeys- a flag indicating whether to make auto generated keys available for retrieval. This parameter must be one ofStatement.NO_GENERATED_KEYSorStatement.RETURN_GENERATED_KEYS.- Returns:
trueif results exists and the first result is aResultSet,falseif the first result is an update count or if there is no result.- Throws:
SQLException- if an error occurs accessing the database.
-
execute
Executes the supplied SQL statement. This may return multipleResultSets. This method allows retrieval of auto generated keys specified by the supplied array of column indexes, if the SQL statement is anINSERTstatement.Use the
getResultSetorgetUpdateCountmethods to get the first result andgetMoreResultsto get any subsequent results.- Parameters:
sql- the SQL statement to execute.columnIndexes- an array of indexes of the columns in the inserted row which should be made available for retrieval via thegetGeneratedKeysmethod.- Returns:
trueif the first result is aResultSet,falseif the first result is an update count or if there is no result.- Throws:
SQLException- if an error occurs accessing the database.
-
execute
Executes the supplied SQL statement. This may return multipleResultSets. This method allows retrieval of auto generated keys specified by the supplied array of column indexes, if the SQL statement is anINSERTstatement.Use the
getResultSetorgetUpdateCountmethods to get the first result andgetMoreResultsto get any subsequent results.- Parameters:
sql- the SQL statement to execute.columnNames- an array of column names in the inserted row which should be made available for retrieval via thegetGeneratedKeysmethod.- Returns:
trueif the first result is aResultSet,falseif the first result is an update count or if there is no result- Throws:
SQLException- if an error occurs accessing the database.
-
executeBatch
Submits a batch of SQL commands to the database. Returns an array of update counts, if all the commands execute successfully.If one of the commands in the batch fails, this method can throw a
BatchUpdateExceptionand the JDBC driver may or may not process the remaining commands. The JDBC driver must behave consistently with the underlying database, following the "all or nothing" principle. If the driver continues processing, the array of results returned contains the same number of elements as there are commands in the batch, with a minimum of one of the elements having theEXECUTE_FAILEDvalue.- Returns:
- an array of update counts, with one entry for each command in the
batch. The elements are ordered according to the order in which
the commands were added to the batch.
- If the value of an element is ≥ 0, the corresponding command completed successfully and the value is the update count (the number of rows in the database affected by the command) for that command.
- If the value is
SUCCESS_NO_INFO, the command completed successfully but the number of rows affected is unknown. - If the value is
EXECUTE_FAILED, the command failed.
- Throws:
SQLException- if an error occurs accessing the database.
-
executeQuery
Executes a supplied SQL statement. Returns a singleResultSet.- Parameters:
sql- an SQL statement to execute. Typically aSELECTstatement- Returns:
- a
ResultSetcontaining the data produced by the SQL statement. Never null. - Throws:
SQLException- if an error occurs accessing the database or if the statement produces anything other than a singleResultSet.
-
executeUpdate
Executes the supplied SQL statement. The statement may be anINSERT,UPDATEorDELETEstatement or a statement which returns nothing.- Parameters:
sql- an SQL statement to execute - an SQLINSERT,UPDATE,DELETEor a statement which returns nothing- Returns:
- the count of updated rows, or 0 for a statement that returns nothing.
- Throws:
SQLException- if an error occurs accessing the database or if the statement produces aResultSet.
-
executeUpdate
Executes the supplied SQL statement. This method allows control of whether auto-generated Keys should be made available for retrieval.- Parameters:
sql- an SQL statement to execute - an SQLINSERT,UPDATE,DELETEor a statement which does not return anything.autoGeneratedKeys- a flag that indicates whether to allow retrieval of auto generated keys. Parameter must be one ofStatement.RETURN_GENERATED_KEYSorStatement.NO_GENERATED_KEYS- Returns:
- the number of updated rows, or 0 if the statement returns nothing.
- Throws:
SQLException- if an error occurs accessing the database or if the statement produces aResultSet.
-
executeUpdate
Executes the supplied SQL statement. This method allows retrieval of auto generated keys specified by the supplied array of column indexes.- Parameters:
sql- an SQL statement to execute - an SQLINSERT,UPDATE,DELETEor a statement which returns nothingcolumnIndexes- an array of indexes of the columns in the inserted row which should be made available for retrieval via thegetGeneratedKeysmethod.- Returns:
- the count of updated rows, or 0 for a statement that returns nothing.
- Throws:
SQLException- if an error occurs accessing the database or if the statement produces aResultSet.
-
executeUpdate
Executes the supplied SQL statement. This method allows retrieval of auto generated keys specified by the supplied array of column names.- Parameters:
sql- an SQL statement to execute - an SQLINSERT,UPDATE,DELETEor a statement which returns nothingcolumnNames- an array of column names in the inserted row which should be made available for retrieval via thegetGeneratedKeysmethod.- Returns:
- the count of updated rows, or 0 for a statement that returns nothing.
- Throws:
SQLException- if an error occurs accessing the database or if the statement produces aResultSet.
-
getConnection
Gets theConnectionobject which created this statement.- Returns:
- the
Connectionthrough which this statement is transmitted to the database. - Throws:
SQLException- if an error occurs accessing the database.
-
getFetchDirection
Gets the default direction for fetching rows forResultSets generated from this statement.- Returns:
- the default fetch direction, one of:
- ResultSet.FETCH_FORWARD
- ResultSet.FETCH_REVERSE
- ResultSet.FETCH_UNKNOWN
- Throws:
SQLException- if an error occurs accessing the database.
-
getFetchSize
Gets the default number of rows for a fetch for theResultSetobjects returned from this statement.- Returns:
- the default fetch size for
ResultSets produced by this statement. - Throws:
SQLException- if an error occurs accessing the database.
-
getGeneratedKeys
Returns auto generated keys created by executing this statement.- Returns:
- a
ResultSetcontaining the auto generated keys - empty if no keys are generated by this statement. - Throws:
SQLException- if an error occurs accessing the database.
-
getMaxFieldSize
Gets the maximum number of bytes which can be returned as values from character and binary type columns in aResultSetderived from this statement. This limit applies toBINARY,VARBINARY,LONGVARBINARY,CHAR,VARCHAR, andLONGVARCHARtypes. Any data exceeding the maximum size is abandoned without announcement.- Returns:
- the current size limit, where
0means that there is no limit. - Throws:
SQLException- if an error occurs accessing the database.
-
getMaxRows
Gets the maximum number of rows that aResultSetcan contain when produced from this statement. If the limit is exceeded, the excess rows are discarded silently.- Returns:
- the current row limit, where
0means that there is no limit. - Throws:
SQLException- if an error occurs accessing the database.
-
getMoreResults
Moves to this statement's next result. Returnstrueif it is aResultSet. Any currentResultSetobjects previously obtained withgetResultSet()are closed implicitly.- Returns:
trueif the next result is aResultSet,falseif the next result is not aResultSetor if there are no more results. Note that if there is no more data, this method will returnfalseandgetUpdateCountwill return -1.- Throws:
SQLException- if an error occurs accessing the database.
-
getMoreResults
Moves to this statement's next result. Returnstrueif the next result is aResultSet. Any currentResultSetobjects previously obtained withgetResultSet()are handled as indicated by a supplied Flag parameter.- Parameters:
current- a flag indicating what to do with existingResultSets. This parameter must be one ofStatement.CLOSE_ALL_RESULTS,Statement.CLOSE_CURRENT_RESULTorStatement.KEEP_CURRENT_RESULT.- Returns:
trueif the next result exists and is aResultSet,falseif the next result is not aResultSetor if there are no more results. Note that if there is no more data, this method will returnfalseandgetUpdateCountwill return -1.- Throws:
SQLException- if an error occurs accessing the database.
-
getQueryTimeout
Gets the timeout value for the statement's execution time. The JDBC driver will wait up to this value for the execution to complete - after the limit is exceeded an SQLExceptionis thrown.- Returns:
- the current query timeout value, where
0indicates that there is no current timeout. - Throws:
SQLException- if an error occurs accessing the database.
-
getResultSet
Gets the current result. Should only be called once per result.- Returns:
- the
ResultSetfor the current result.nullif the result is an update count or if there are no more results. - Throws:
SQLException- if an error occurs accessing the database.
-
getResultSetConcurrency
Gets the concurrency setting forResultSetobjects generated by this statement.- Returns:
ResultSet.CONCUR_READ_ONLYorResultSet.CONCUR_UPDATABLE.- Throws:
SQLException- if an error occurs accessing the database.
-
getResultSetHoldability
Gets the cursor hold setting forResultSetobjects generated by this statement.- Returns:
ResultSet.HOLD_CURSORS_OVER_COMMITorResultSet.CLOSE_CURSORS_AT_COMMIT- Throws:
SQLException- if there is an error while accessing the database.
-
getResultSetType
Gets theResultSettype setting forResultSets derived from this statement.- Returns:
ResultSet.TYPE_FORWARD_ONLYfor aResultSetwhere the cursor can only move forwards,ResultSet.TYPE_SCROLL_INSENSITIVEfor aResultSetwhich is scrollable but is not sensitive to changes made by others,ResultSet.TYPE_SCROLL_SENSITIVEfor aResultSetwhich is scrollable but is sensitive to changes made by others.- Throws:
SQLException- if there is an error accessing the database.
-
getUpdateCount
Gets an update count for the current result if it is not aResultSet.- Returns:
- the current result as an update count.
-1if the current result is aResultSetor if there are no more results. - Throws:
SQLException- if an error occurs accessing the database.
-
getWarnings
Retrieves the firstSQLWarningreported by calls on this statement. If there are multiple warnings, subsequent warnings are chained to the first one. The chain of warnings is cleared each time the statement is executed.Warnings associated with reads from the
ResultSetreturned from executing the statement will be attached to theResultSet, not the statement object.- Returns:
- an SQLWarning, null if there are no warnings
- Throws:
SQLException- if an error occurs accessing the database.
-
setCursorName
Sets the SQL cursor name. This name is used by subsequent statement execute methods.Cursor names must be unique within one Connection.
With the cursor name set, it can then be used in SQL positioned update or delete statements to determine the current row in a
ResultSetgenerated from this statement. The positioned update or delete must be done with a different statement than this one.- Parameters:
name- the Cursor name as a string,- Throws:
SQLException- if an error occurs accessing the database.
-
setEscapeProcessing
Sets Escape Processing mode.If Escape Processing is on, the JDBC driver will do escape substitution on an SQL statement before sending it for execution. This does not apply to
PreparedStatements since they are processed when created, before this method can be called.- Parameters:
enable-trueto set escape processing mode on,falseto turn it off.- Throws:
SQLException- if an error occurs accessing the database.
-
setFetchDirection
Sets the fetch direction - a hint to the JDBC driver about the direction of processing of rows inResultSets created by this statement. The default fetch direction isFETCH_FORWARD.- Parameters:
direction- which fetch direction to use. This parameter should be one ofResultSet.FETCH_UNKNOWNResultSet.FETCH_FORWARDResultSet.FETCH_REVERSE
- Throws:
SQLException- if there is an error while accessing the database or if the fetch direction is unrecognized.
-
setFetchSize
Sets the fetch size. This is a hint to the JDBC driver about how many rows should be fetched from the database when more are required by application processing.- Parameters:
rows- the number of rows that should be fetched.0tells the driver to ignore the hint. Should be less thangetMaxRowsfor this statement. Should not be negative.- Throws:
SQLException- if an error occurs accessing the database, or if the rows parameter is out of range.
-
setMaxFieldSize
Sets the maximum number of bytes forResultSetcolumns that contain character or binary values. This applies toBINARY,VARBINARY,LONGVARBINARY,CHAR,VARCHAR, andLONGVARCHARfields. Any data exceeding the maximum size is abandoned without announcement.- Parameters:
max- the maximum field size in bytes.0means "no limit".- Throws:
SQLException- if an error occurs accessing the database or themaxvalue is <0.
-
setMaxRows
Sets the maximum number of rows that anyResultSetcan contain. If the number of rows exceeds this value, the additional rows are silently discarded.- Parameters:
max- the maximum number of rows.0means "no limit".- Throws:
SQLException- if an error occurs accessing the database or if max <0.
-
setQueryTimeout
Sets the timeout, in seconds, for queries - how long the driver will allow for completion of a statement execution. If the timeout is exceeded, the query will throw anSQLException.- Parameters:
seconds- timeout in seconds. 0 means no timeout ("wait forever")- Throws:
SQLException- if an error occurs accessing the database or if seconds <0.
-
isClosed
Returns true if this statement has been closed, false otherwise.- Throws:
SQLException
-
setPoolable
Hints whether this statement should be pooled. Defaults to false forStatement, but true forCallableStatementandPreparedStatement. Pool manager implementations may or may not honor this hint.- Throws:
SQLException
-
isPoolable
Returns true if this statement is poolable, false otherwise.- Throws:
SQLException
-