Class JdbcStatement
- All Implemented Interfaces:
AutoCloseable,Statement,Wrapper
- Direct Known Subclasses:
JdbcPreparedStatement
Thread safety: the statement is not thread-safe. If the same statement is used by multiple threads access to it must be synchronized. The single synchronized block must include execution of the command and all operations with its result.
synchronized (stat) {
try (ResultSet rs = stat.executeQuery(queryString)) {
while (rs.next) {
// Do something
}
}
}
synchronized (stat) {
updateCount = stat.executeUpdate(commandString);
}
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected JdbcConnectionprotected intprotected JdbcResultSetprotected longprotected JdbcResultSetprotected final intprotected final intprotected Sessionprotected longFields inherited from class org.h2.message.TraceObject
ARRAY, BLOB, CALLABLE_STATEMENT, CLOB, CONNECTION, DATA_SOURCE, DATABASE_META_DATA, PARAMETER_META_DATA, PREPARED_STATEMENT, RESULT_SET, RESULT_SET_META_DATA, SAVEPOINT, SQLXML, STATEMENT, trace, XA_DATA_SOURCE, XIDFields 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 -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a statement to the batch.voidcancel()Cancels a currently running statement.voidClears the batch.voidClears all warnings.voidclose()Closes this statement.protected voidINTERNAL.voidSpecifies that this statement will be closed when its dependent result set is closed.enquoteIdentifier(String identifier, boolean alwaysQuote) final booleanExecutes a statement and returns type of its result.final booleanExecutes a statement and returns type of its result.final booleanExecutes a statement and returns type of its result.final booleanExecutes a statement and returns type of its result.int[]Executes the batch.long[]Executes the batch.final longexecuteLargeUpdate(String sql) Executes a statement (insert, update, delete, create, drop) and returns the update count.final longexecuteLargeUpdate(String sql, int autoGeneratedKeys) Executes a statement and returns the update count.final longexecuteLargeUpdate(String sql, int[] columnIndexes) Executes a statement and returns the update count.final longexecuteLargeUpdate(String sql, String[] columnNames) Executes a statement and returns the update count.executeQuery(String sql) Executes a query (select statement) and returns the result set.final intexecuteUpdate(String sql) Executes a statement (insert, update, delete, create, drop) and returns the update count.final intexecuteUpdate(String sql, int autoGeneratedKeys) Executes a statement and returns the update count.final intexecuteUpdate(String sql, int[] columnIndexes) Executes a statement and returns the update count.final intexecuteUpdate(String sql, String[] columnNames) Executes a statement and returns the update count.Returns the connection that created this object.intGets the fetch direction.intGets the number of rows suggested to read in one step.final ResultSetReturn a result set with generated keys from the latest executed command or an empty result set if keys were not generated or were not requested withStatement.RETURN_GENERATED_KEYS, column indexes, or column names.longGets the maximum number of rows for a ResultSet.final longReturns the last update count of this statement.intGets the maximum number of bytes for a result set column.intGets the maximum number of rows for a ResultSet.booleanMoves to the next result set - however there is always only one result set.booleangetMoreResults(int current) Move to the next result set.intGets the current query timeout in seconds.Returns the last result set produces by this statement.intGets the result set concurrency created by this object.intGets the result set holdability.intGets the result set type.final intReturns the last update count of this statement.Gets the first warning reported by calls on this object.booleanCheck whether the statement was cancelled.booleanisClosed()Returns whether this statement is closed.booleanReturns whether this statement will be closed when its dependent result set is closed.booleanReturns whether this object is poolable.booleanisSimpleIdentifier(String identifier) booleanisWrapperFor(Class<?> iface) Checks if unwrap can return an object of this class.voidsetCursorName(String name) Sets the name of the cursor.voidsetEscapeProcessing(boolean enable) Enables or disables processing or JDBC escape syntax.voidsetFetchDirection(int direction) Sets the fetch direction.voidsetFetchSize(int rows) Sets the number of rows suggested to read in one step.voidsetLargeMaxRows(long maxRows) Gets the maximum number of rows for a ResultSet.voidsetMaxFieldSize(int max) Sets the maximum number of bytes for a result set column.voidsetMaxRows(int maxRows) Gets the maximum number of rows for a ResultSet.voidsetPoolable(boolean poolable) Requests that this object should be pooled or not.voidsetQueryTimeout(int seconds) Sets the current query timeout in seconds.toString()INTERNAL<T> TReturn an object of this class if possible.Methods inherited from class org.h2.message.TraceObject
debugCode, debugCodeAssign, debugCodeCall, debugCodeCall, debugCodeCall, getNextId, getTraceId, getTraceObjectName, isDebugEnabled, isInfoEnabled, logAndConvert, quote, quoteArray, quoteBigDecimal, quoteBytes, quoteDate, quoteIntArray, quoteMap, quoteTime, quoteTimestamp, setTrace, unsupportedMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.sql.Statement
enquoteLiteral, enquoteNCharLiteral
-
Field Details
-
conn
-
session
-
resultSet
-
maxRows
protected long maxRows -
fetchSize
protected int fetchSize -
updateCount
protected long updateCount -
generatedKeys
-
resultSetType
protected final int resultSetType -
resultSetConcurrency
protected final int resultSetConcurrency
-
-
Method Details
-
executeQuery
Executes a query (select statement) and returns the result set. If another result set exists for this statement, this will be closed (even if this statement fails).- Specified by:
executeQueryin interfaceStatement- Parameters:
sql- the SQL statement to execute- Returns:
- the result set
- Throws:
SQLException
-
executeUpdate
Executes a statement (insert, update, delete, create, drop) and returns the update count. This method is not allowed for prepared statements. If another result set exists for this statement, this will be closed (even if this statement fails). If auto commit is on, this statement will be committed. If the statement is a DDL statement (create, drop, alter) and does not throw an exception, the current transaction (if any) is committed after executing the statement.- Specified by:
executeUpdatein interfaceStatement- Parameters:
sql- the SQL statement- Returns:
- the update count (number of affected rows by a DML statement or
other statement able to return number of rows, or 0 if no rows
were affected or the statement returned nothing, or
Statement.SUCCESS_NO_INFOif number of rows is too large for theintdata type) - Throws:
SQLException- if a database error occurred or a select statement was executed- See Also:
-
executeLargeUpdate
Executes a statement (insert, update, delete, create, drop) and returns the update count. This method is not allowed for prepared statements. If another result set exists for this statement, this will be closed (even if this statement fails). If auto commit is on, this statement will be committed. If the statement is a DDL statement (create, drop, alter) and does not throw an exception, the current transaction (if any) is committed after executing the statement.- Specified by:
executeLargeUpdatein interfaceStatement- Parameters:
sql- the SQL statement- Returns:
- the update count (number of affected rows by a DML statement or other statement able to return number of rows, or 0 if no rows were affected or the statement returned nothing)
- Throws:
SQLException- if a database error occurred or a select statement was executed
-
execute
Executes a statement and returns type of its result. This method is not allowed for prepared statements. If another result set exists for this statement, this will be closed (even if this statement fails). If the statement is a create or drop and does not throw an exception, the current transaction (if any) is committed after executing the statement. If auto commit is on, and the statement is not a select, this statement will be committed.- Specified by:
executein interfaceStatement- Parameters:
sql- the SQL statement to execute- Returns:
- true if result is a result set, false otherwise
- Throws:
SQLException
-
getResultSet
Returns the last result set produces by this statement.- Specified by:
getResultSetin interfaceStatement- Returns:
- the result set
- Throws:
SQLException
-
getUpdateCount
Returns the last update count of this statement.- Specified by:
getUpdateCountin interfaceStatement- Returns:
- the update count (number of affected rows by a DML statement or
other statement able to return number of rows, or 0 if no rows
were affected or the statement returned nothing, or -1 if
statement was a query, or
Statement.SUCCESS_NO_INFOif number of rows is too large for theintdata type) - Throws:
SQLException- if this object is closed or invalid- See Also:
-
getLargeUpdateCount
Returns the last update count of this statement.- Specified by:
getLargeUpdateCountin interfaceStatement- Returns:
- the update count (number of affected rows by a DML statement or other statement able to return number of rows, or 0 if no rows were affected or the statement returned nothing, or -1 if statement was a query)
- Throws:
SQLException- if this object is closed or invalid
-
close
Closes this statement. All result sets that where created by this statement become invalid after calling this method.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceStatement- Throws:
SQLException
-
getConnection
Returns the connection that created this object.- Specified by:
getConnectionin interfaceStatement- Returns:
- the connection
-
getWarnings
Gets the first warning reported by calls on this object. This driver does not support warnings, and will always return null.- Specified by:
getWarningsin interfaceStatement- Returns:
- null
- Throws:
SQLException
-
clearWarnings
Clears all warnings. As this driver does not support warnings, this call is ignored.- Specified by:
clearWarningsin interfaceStatement- Throws:
SQLException
-
setCursorName
Sets the name of the cursor. This call is ignored.- Specified by:
setCursorNamein interfaceStatement- Parameters:
name- ignored- Throws:
SQLException- if this object is closed
-
setFetchDirection
Sets the fetch direction. This call is ignored by this driver.- Specified by:
setFetchDirectionin interfaceStatement- Parameters:
direction- ignored- Throws:
SQLException- if this object is closed
-
getFetchDirection
Gets the fetch direction.- Specified by:
getFetchDirectionin interfaceStatement- Returns:
- FETCH_FORWARD
- Throws:
SQLException- if this object is closed
-
getMaxRows
Gets the maximum number of rows for a ResultSet.- Specified by:
getMaxRowsin interfaceStatement- Returns:
- the number of rows where 0 means no limit
- Throws:
SQLException- if this object is closed
-
getLargeMaxRows
Gets the maximum number of rows for a ResultSet.- Specified by:
getLargeMaxRowsin interfaceStatement- Returns:
- the number of rows where 0 means no limit
- Throws:
SQLException- if this object is closed
-
setMaxRows
Gets the maximum number of rows for a ResultSet.- Specified by:
setMaxRowsin interfaceStatement- Parameters:
maxRows- the number of rows where 0 means no limit- Throws:
SQLException- if this object is closed
-
setLargeMaxRows
Gets the maximum number of rows for a ResultSet.- Specified by:
setLargeMaxRowsin interfaceStatement- Parameters:
maxRows- the number of rows where 0 means no limit- Throws:
SQLException- if this object is closed
-
setFetchSize
Sets the number of rows suggested to read in one step. This value cannot be higher than the maximum rows (setMaxRows) set by the statement or prepared statement, otherwise an exception is throws. Setting the value to 0 will set the default value. The default value can be changed using the system property h2.serverResultSetFetchSize.- Specified by:
setFetchSizein interfaceStatement- Parameters:
rows- the number of rows- Throws:
SQLException- if this object is closed
-
getFetchSize
Gets the number of rows suggested to read in one step.- Specified by:
getFetchSizein interfaceStatement- Returns:
- the current fetch size
- Throws:
SQLException- if this object is closed
-
getResultSetConcurrency
Gets the result set concurrency created by this object.- Specified by:
getResultSetConcurrencyin interfaceStatement- Returns:
- the concurrency
- Throws:
SQLException
-
getResultSetType
Gets the result set type.- Specified by:
getResultSetTypein interfaceStatement- Returns:
- the type
- Throws:
SQLException- if this object is closed
-
getMaxFieldSize
Gets the maximum number of bytes for a result set column.- Specified by:
getMaxFieldSizein interfaceStatement- Returns:
- always 0 for no limit
- Throws:
SQLException- if this object is closed
-
setMaxFieldSize
Sets the maximum number of bytes for a result set column. This method does currently do nothing for this driver.- Specified by:
setMaxFieldSizein interfaceStatement- Parameters:
max- the maximum size - ignored- Throws:
SQLException- if this object is closed
-
setEscapeProcessing
Enables or disables processing or JDBC escape syntax. See also Connection.nativeSQL.- Specified by:
setEscapeProcessingin interfaceStatement- Parameters:
enable- - true (default) or false (no conversion is attempted)- Throws:
SQLException- if this object is closed
-
cancel
Cancels a currently running statement. This method must be called from within another thread than the execute method. Operations on large objects are not interrupted, only operations that process many rows.- Specified by:
cancelin interfaceStatement- Throws:
SQLException- if this object is closed
-
isCancelled
public boolean isCancelled()Check whether the statement was cancelled.- Returns:
- true if yes
-
getQueryTimeout
Gets the current query timeout in seconds. This method will return 0 if no query timeout is set. The result is rounded to the next second. For performance reasons, only the first call to this method will query the database. If the query timeout was changed in another way than calling setQueryTimeout, this method will always return the last value.- Specified by:
getQueryTimeoutin interfaceStatement- Returns:
- the timeout in seconds
- Throws:
SQLException- if this object is closed
-
setQueryTimeout
Sets the current query timeout in seconds. Changing the value will affect all statements of this connection. This method does not commit a transaction, and rolling back a transaction does not affect this setting.- Specified by:
setQueryTimeoutin interfaceStatement- Parameters:
seconds- the timeout in seconds - 0 means no timeout, values smaller 0 will throw an exception- Throws:
SQLException- if this object is closed
-
addBatch
Adds a statement to the batch.- Specified by:
addBatchin interfaceStatement- Parameters:
sql- the SQL statement- Throws:
SQLException
-
clearBatch
Clears the batch.- Specified by:
clearBatchin interfaceStatement- Throws:
SQLException
-
executeBatch
Executes the batch. If one of the batched statements fails, this database will continue.- Specified by:
executeBatchin interfaceStatement- Returns:
- the array of update counts
- Throws:
SQLException- See Also:
-
executeLargeBatch
Executes the batch. If one of the batched statements fails, this database will continue.- Specified by:
executeLargeBatchin interfaceStatement- Returns:
- the array of update counts
- Throws:
SQLException
-
getGeneratedKeys
Return a result set with generated keys from the latest executed command or an empty result set if keys were not generated or were not requested withStatement.RETURN_GENERATED_KEYS, column indexes, or column names.Generated keys are only returned from from
INSERT,UPDATE,MERGE INTO, andMERGE INTO ... USINGcommands.If SQL command inserts or updates multiple rows with generated keys each such inserted or updated row is returned. Batch methods are also supported.
When
Statement.RETURN_GENERATED_KEYSis used H2 chooses columns to return automatically. The following columns are chosen:- Columns with sequences including
IDENTITYcolumns and columns withAUTO_INCREMENT. - Columns with other default values that are not evaluated into
constant expressions (like
DEFAULT RANDOM_UUID()). - Columns that are included into the PRIMARY KEY constraint.
Exact required columns for the returning result set may be specified on execution of command with names or indexes of columns.
- Specified by:
getGeneratedKeysin interfaceStatement- Returns:
- the possibly empty result set with generated keys
- Throws:
SQLException- if this object is closed
- Columns with sequences including
-
getMoreResults
Moves to the next result set - however there is always only one result set. This call also closes the current result set (if there is one). Returns true if there is a next result set (that means - it always returns false).- Specified by:
getMoreResultsin interfaceStatement- Returns:
- false
- Throws:
SQLException- if this object is closed.
-
getMoreResults
Move to the next result set. This method always returns false.- Specified by:
getMoreResultsin interfaceStatement- Parameters:
current- Statement.CLOSE_CURRENT_RESULT, Statement.KEEP_CURRENT_RESULT, or Statement.CLOSE_ALL_RESULTS- Returns:
- false
- Throws:
SQLException
-
executeUpdate
Executes a statement and returns the update count. This method is not allowed for prepared statements.- Specified by:
executeUpdatein interfaceStatement- Parameters:
sql- the SQL statementautoGeneratedKeys-Statement.RETURN_GENERATED_KEYSif generated keys should be available for retrieval,Statement.NO_GENERATED_KEYSif generated keys should not be available- Returns:
- the update count (number of affected rows by a DML statement or
other statement able to return number of rows, or 0 if no rows
were affected or the statement returned nothing, or
Statement.SUCCESS_NO_INFOif number of rows is too large for theintdata type) - Throws:
SQLException- if a database error occurred or a select statement was executed- See Also:
-
executeLargeUpdate
Executes a statement and returns the update count. This method is not allowed for prepared statements.- Specified by:
executeLargeUpdatein interfaceStatement- Parameters:
sql- the SQL statementautoGeneratedKeys-Statement.RETURN_GENERATED_KEYSif generated keys should be available for retrieval,Statement.NO_GENERATED_KEYSif generated keys should not be available- Returns:
- the update count (number of affected rows by a DML statement or other statement able to return number of rows, or 0 if no rows were affected or the statement returned nothing)
- Throws:
SQLException- if a database error occurred or a select statement was executed
-
executeUpdate
Executes a statement and returns the update count. This method is not allowed for prepared statements.- Specified by:
executeUpdatein interfaceStatement- Parameters:
sql- the SQL statementcolumnIndexes- an array of column indexes indicating the columns with generated keys that should be returned from the inserted row- Returns:
- the update count (number of affected rows by a DML statement or
other statement able to return number of rows, or 0 if no rows
were affected or the statement returned nothing, or
Statement.SUCCESS_NO_INFOif number of rows is too large for theintdata type) - Throws:
SQLException- if a database error occurred or a select statement was executed- See Also:
-
executeLargeUpdate
Executes a statement and returns the update count. This method is not allowed for prepared statements.- Specified by:
executeLargeUpdatein interfaceStatement- Parameters:
sql- the SQL statementcolumnIndexes- an array of column indexes indicating the columns with generated keys that should be returned from the inserted row- Returns:
- the update count (number of affected rows by a DML statement or other statement able to return number of rows, or 0 if no rows were affected or the statement returned nothing)
- Throws:
SQLException- if a database error occurred or a select statement was executed
-
executeUpdate
Executes a statement and returns the update count. This method is not allowed for prepared statements.- Specified by:
executeUpdatein interfaceStatement- Parameters:
sql- the SQL statementcolumnNames- an array of column names indicating the columns with generated keys that should be returned from the inserted row- Returns:
- the update count (number of affected rows by a DML statement or
other statement able to return number of rows, or 0 if no rows
were affected or the statement returned nothing, or
Statement.SUCCESS_NO_INFOif number of rows is too large for theintdata type) - Throws:
SQLException- if a database error occurred or a select statement was executed- See Also:
-
executeLargeUpdate
Executes a statement and returns the update count. This method is not allowed for prepared statements.- Specified by:
executeLargeUpdatein interfaceStatement- Parameters:
sql- the SQL statementcolumnNames- an array of column names indicating the columns with generated keys that should be returned from the inserted row- Returns:
- the update count (number of row affected by an insert, update or delete, or 0 if no rows or the statement was a create, drop, commit or rollback)
- Throws:
SQLException- if a database error occurred or a select statement was executed
-
execute
Executes a statement and returns type of its result. This method is not allowed for prepared statements.- Specified by:
executein interfaceStatement- Parameters:
sql- the SQL statementautoGeneratedKeys-Statement.RETURN_GENERATED_KEYSif generated keys should be available for retrieval,Statement.NO_GENERATED_KEYSif generated keys should not be available- Returns:
- true if result is a result set, false otherwise
- Throws:
SQLException- if a database error occurred or a select statement was executed
-
execute
Executes a statement and returns type of its result. This method is not allowed for prepared statements.- Specified by:
executein interfaceStatement- Parameters:
sql- the SQL statementcolumnIndexes- an array of column indexes indicating the columns with generated keys that should be returned from the inserted row- Returns:
- true if result is a result set, false otherwise
- Throws:
SQLException- if a database error occurred or a select statement was executed
-
execute
Executes a statement and returns type of its result. This method is not allowed for prepared statements.- Specified by:
executein interfaceStatement- Parameters:
sql- the SQL statementcolumnNames- an array of column names indicating the columns with generated keys that should be returned from the inserted row- Returns:
- true if result is a result set, false otherwise
- Throws:
SQLException- if a database error occurred or a select statement was executed
-
getResultSetHoldability
Gets the result set holdability.- Specified by:
getResultSetHoldabilityin interfaceStatement- Returns:
- the holdability
- Throws:
SQLException
-
closeOnCompletion
Specifies that this statement will be closed when its dependent result set is closed.- Specified by:
closeOnCompletionin interfaceStatement- Throws:
SQLException- if this statement is closed
-
isCloseOnCompletion
Returns whether this statement will be closed when its dependent result set is closed.- Specified by:
isCloseOnCompletionin interfaceStatement- Returns:
trueif this statement will be closed when its dependent result set is closed- Throws:
SQLException- if this statement is closed
-
closeOldResultSet
protected void closeOldResultSet()INTERNAL. Close and old result set if there is still one open. -
isClosed
Returns whether this statement is closed.- Specified by:
isClosedin interfaceStatement- Returns:
- true if the statement is closed
- Throws:
SQLException
-
unwrap
Return an object of this class if possible.- Specified by:
unwrapin interfaceWrapper- Parameters:
iface- the class- Returns:
- this
- Throws:
SQLException
-
isWrapperFor
Checks if unwrap can return an object of this class.- Specified by:
isWrapperForin interfaceWrapper- Parameters:
iface- the class- Returns:
- whether or not the interface is assignable from this class
- Throws:
SQLException
-
isPoolable
public boolean isPoolable()Returns whether this object is poolable.- Specified by:
isPoolablein interfaceStatement- Returns:
- false
-
setPoolable
public void setPoolable(boolean poolable) Requests that this object should be pooled or not. This call is ignored.- Specified by:
setPoolablein interfaceStatement- Parameters:
poolable- the requested value
-
enquoteIdentifier
- Specified by:
enquoteIdentifierin interfaceStatement- Parameters:
identifier- identifier to quote if required, may be quoted or unquotedalwaysQuote- iftrueidentifier will be quoted unconditionally- Returns:
- specified identifier quoted if required, explicitly requested, or if it was already quoted
- Throws:
NullPointerException- if identifier isnullSQLException- if identifier is not a valid identifier
-
isSimpleIdentifier
- Specified by:
isSimpleIdentifierin interfaceStatement- Parameters:
identifier- identifier to check- Returns:
- is specified identifier may be used without quotes
- Throws:
NullPointerException- if identifier isnullSQLException
-
toString
INTERNAL
-