Class JDBCStatement
- All Implemented Interfaces:
AutoCloseable,Statement,Wrapper
- Direct Known Subclasses:
JDBCPreparedStatement
public class JDBCStatement extends Object implements Statement
-
Field Summary
Fields Modifier and Type Field Description protected JDBCConnectionconnprotected intmaxrowsprotected JDBCResultSetrsprotected intupdcntFields inherited from interface java.sql.Statement
CLOSE_ALL_RESULTS, CLOSE_CURRENT_RESULT, EXECUTE_FAILED, KEEP_CURRENT_RESULT, NO_GENERATED_KEYS, RETURN_GENERATED_KEYS, SUCCESS_NO_INFO -
Constructor Summary
Constructors Constructor Description JDBCStatement(JDBCConnection conn) -
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 autokeys)Executes a supplied SQL statement.booleanexecute(String sql, int[] colIndexes)Executes the supplied SQL statement.booleanexecute(String sql, String[] colIndexes)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 autokeys)Executes the supplied SQL statement.intexecuteUpdate(String sql, int[] colIndexes)Executes the supplied SQL statement.intexecuteUpdate(String sql, String[] colIndexes)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 x)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.booleanisWrapperFor(Class iface)If the caller is a wrapper of the class or implements the given interface, the methods return false and vice versa.voidsetCursorName(String name)Sets the SQL cursor name.voidsetEscapeProcessing(boolean enable)Sets Escape Processing mode.voidsetFetchDirection(int fetchDirection)Sets the fetch direction - a hint to the JDBC driver about the direction of processing of rows inResultSets created by this statement.voidsetFetchSize(int fetchSize)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 yes)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.<T> Tunwrap(Class<T> iface)Returns an object that implements the given interface.
-
Field Details
-
conn
-
rs
-
updcnt
protected int updcnt -
maxrows
protected int maxrows
-
-
Constructor Details
-
JDBCStatement
-
-
Method Details
-
setFetchSize
Description copied from interface:StatementSets 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.- Specified by:
setFetchSizein interfaceStatement- Parameters:
fetchSize- 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.
-
getFetchSize
Description copied from interface:StatementGets the default number of rows for a fetch for theResultSetobjects returned from this statement.- Specified by:
getFetchSizein interfaceStatement- Returns:
- the default fetch size for
ResultSets produced by this statement. - Throws:
SQLException- if an error occurs accessing the database.
-
getMaxRows
Description copied from interface:StatementGets the maximum number of rows that aResultSetcan contain when produced from this statement. If the limit is exceeded, the excess rows are discarded silently.- Specified by:
getMaxRowsin interfaceStatement- Returns:
- the current row limit, where
0means that there is no limit. - Throws:
SQLException- if an error occurs accessing the database.
-
setMaxRows
Description copied from interface:StatementSets the maximum number of rows that anyResultSetcan contain. If the number of rows exceeds this value, the additional rows are silently discarded.- Specified by:
setMaxRowsin interfaceStatement- Parameters:
max- the maximum number of rows.0means "no limit".- Throws:
SQLException- if an error occurs accessing the database or if max <0.
-
setFetchDirection
Description copied from interface:StatementSets 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.- Specified by:
setFetchDirectionin interfaceStatement- Parameters:
fetchDirection- 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.
-
getFetchDirection
Description copied from interface:StatementGets the default direction for fetching rows forResultSets generated from this statement.- Specified by:
getFetchDirectionin interfaceStatement- 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.
-
getResultSetConcurrency
Description copied from interface:StatementGets the concurrency setting forResultSetobjects generated by this statement.- Specified by:
getResultSetConcurrencyin interfaceStatement- Returns:
ResultSet.CONCUR_READ_ONLYorResultSet.CONCUR_UPDATABLE.- Throws:
SQLException- if an error occurs accessing the database.
-
getResultSetType
Description copied from interface:StatementGets theResultSettype setting forResultSets derived from this statement.- Specified by:
getResultSetTypein interfaceStatement- 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.
-
setQueryTimeout
Description copied from interface:StatementSets 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.- Specified by:
setQueryTimeoutin interfaceStatement- Parameters:
seconds- timeout in seconds. 0 means no timeout ("wait forever")- Throws:
SQLException- if an error occurs accessing the database or if seconds <0.
-
getQueryTimeout
Description copied from interface:StatementGets 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.- Specified by:
getQueryTimeoutin interfaceStatement- Returns:
- the current query timeout value, where
0indicates that there is no current timeout. - Throws:
SQLException- if an error occurs accessing the database.
-
getResultSet
Description copied from interface:StatementGets the current result. Should only be called once per result.- Specified by:
getResultSetin interfaceStatement- 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.
-
executeQuery
Description copied from interface:StatementExecutes a supplied SQL statement. Returns a singleResultSet.- Specified by:
executeQueryin interfaceStatement- 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.
-
execute
Description copied from interface:StatementExecutes a supplied SQL statement. This may return multipleResultSets.Use the
getResultSetorgetUpdateCountmethods to get the first result andgetMoreResultsto get any subsequent results.- Specified by:
executein interfaceStatement- 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.
-
cancel
Description copied from interface:StatementCancels 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.- Specified by:
cancelin interfaceStatement- Throws:
SQLException- if an error occurs accessing the database.
-
clearWarnings
Description copied from interface:StatementClears allSQLWarningsfrom this statement.- Specified by:
clearWarningsin interfaceStatement- Throws:
SQLException- if an error occurs accessing the database.
-
getConnection
Description copied from interface:StatementGets theConnectionobject which created this statement.- Specified by:
getConnectionin interfaceStatement- Returns:
- the
Connectionthrough which this statement is transmitted to the database. - Throws:
SQLException- if an error occurs accessing the database.
-
addBatch
Description copied from interface:StatementAdds a specified SQL command to the list of commands for thisStatement.The list of commands is executed by invoking the
executeBatchmethod.- Specified by:
addBatchin interfaceStatement- 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.
-
executeBatch
Description copied from interface:StatementSubmits 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.- Specified by:
executeBatchin interfaceStatement- 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.
-
clearBatch
Description copied from interface:StatementClears the current list of SQL commands for this statement.- Specified by:
clearBatchin interfaceStatement- Throws:
SQLException- if an error occurs accessing the database or the database does not support batch updates.
-
close
Description copied from interface:StatementReleases 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- Specified by:
closein interfaceStatement- Throws:
SQLException- if an error occurs accessing the database.
-
executeUpdate
Description copied from interface:StatementExecutes the supplied SQL statement. The statement may be anINSERT,UPDATEorDELETEstatement or a statement which returns nothing.- Specified by:
executeUpdatein interfaceStatement- 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.
-
getMaxFieldSize
Description copied from interface:StatementGets 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.- Specified by:
getMaxFieldSizein interfaceStatement- Returns:
- the current size limit, where
0means that there is no limit. - Throws:
SQLException- if an error occurs accessing the database.
-
getMoreResults
Description copied from interface:StatementMoves to this statement's next result. Returnstrueif it is aResultSet. Any currentResultSetobjects previously obtained withgetResultSet()are closed implicitly.- Specified by:
getMoreResultsin interfaceStatement- 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.
-
getUpdateCount
Description copied from interface:StatementGets an update count for the current result if it is not aResultSet.- Specified by:
getUpdateCountin interfaceStatement- 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
Description copied from interface:StatementRetrieves 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.- Specified by:
getWarningsin interfaceStatement- Returns:
- an SQLWarning, null if there are no warnings
- Throws:
SQLException- if an error occurs accessing the database.
-
setCursorName
Description copied from interface:StatementSets 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.- Specified by:
setCursorNamein interfaceStatement- Parameters:
name- the Cursor name as a string,- Throws:
SQLException- if an error occurs accessing the database.
-
setEscapeProcessing
Description copied from interface:StatementSets 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.- Specified by:
setEscapeProcessingin interfaceStatement- Parameters:
enable-trueto set escape processing mode on,falseto turn it off.- Throws:
SQLException- if an error occurs accessing the database.
-
setMaxFieldSize
Description copied from interface:StatementSets 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.- Specified by:
setMaxFieldSizein interfaceStatement- Parameters:
max- the maximum field size in bytes.0means "no limit".- Throws:
SQLException- if an error occurs accessing the database or themaxvalue is <0.
-
getMoreResults
Description copied from interface:StatementMoves 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.- Specified by:
getMoreResultsin interfaceStatement- Parameters:
x- 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.
-
getGeneratedKeys
Description copied from interface:StatementReturns auto generated keys created by executing this statement.- Specified by:
getGeneratedKeysin interfaceStatement- 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.
-
executeUpdate
Description copied from interface:StatementExecutes the supplied SQL statement. This method allows control of whether auto-generated Keys should be made available for retrieval.- Specified by:
executeUpdatein interfaceStatement- Parameters:
sql- an SQL statement to execute - an SQLINSERT,UPDATE,DELETEor a statement which does not return anything.autokeys- 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
Description copied from interface:StatementExecutes the supplied SQL statement. This method allows retrieval of auto generated keys specified by the supplied array of column indexes.- Specified by:
executeUpdatein interfaceStatement- Parameters:
sql- an SQL statement to execute - an SQLINSERT,UPDATE,DELETEor a statement which returns nothingcolIndexes- 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
Description copied from interface:StatementExecutes the supplied SQL statement. This method allows retrieval of auto generated keys specified by the supplied array of column names.- Specified by:
executeUpdatein interfaceStatement- Parameters:
sql- an SQL statement to execute - an SQLINSERT,UPDATE,DELETEor a statement which returns nothingcolIndexes- 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.
-
execute
Description copied from interface:StatementExecutes 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.- Specified by:
executein interfaceStatement- Parameters:
sql- the SQL statement to execute.autokeys- 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
Description copied from interface:StatementExecutes 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.- Specified by:
executein interfaceStatement- Parameters:
sql- the SQL statement to execute.colIndexes- 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
Description copied from interface:StatementExecutes 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.- Specified by:
executein interfaceStatement- Parameters:
sql- the SQL statement to execute.colIndexes- 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.
-
getResultSetHoldability
Description copied from interface:StatementGets the cursor hold setting forResultSetobjects generated by this statement.- Specified by:
getResultSetHoldabilityin interfaceStatement- Returns:
ResultSet.HOLD_CURSORS_OVER_COMMITorResultSet.CLOSE_CURSORS_AT_COMMIT- Throws:
SQLException- if there is an error while accessing the database.
-
isClosed
Description copied from interface:StatementReturns true if this statement has been closed, false otherwise.- Specified by:
isClosedin interfaceStatement- Throws:
SQLException
-
setPoolable
Description copied from interface:StatementHints whether this statement should be pooled. Defaults to false forStatement, but true forCallableStatementandPreparedStatement. Pool manager implementations may or may not honor this hint.- Specified by:
setPoolablein interfaceStatement- Throws:
SQLException
-
isPoolable
Description copied from interface:StatementReturns true if this statement is poolable, false otherwise.- Specified by:
isPoolablein interfaceStatement- Throws:
SQLException
-
unwrap
Description copied from interface:WrapperReturns an object that implements the given interface. If the caller is not a wrapper, a SQLException will be thrown.- Specified by:
unwrapin interfaceWrapper- Parameters:
iface- - the class that defines the interface- Returns:
- - an object that implements the interface
- Throws:
SQLException- - if there is no object implementing the specific interface
-
isWrapperFor
Description copied from interface:WrapperIf the caller is a wrapper of the class or implements the given interface, the methods return false and vice versa.- Specified by:
isWrapperForin interfaceWrapper- Parameters:
iface- - the class that defines the interface- Returns:
- - true if the instance implements the interface
- Throws:
SQLException- - when an error occurs when judges the object
-