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
Modifier and TypeFieldDescriptionprotected JdbcConnection
protected int
protected JdbcResultSet
protected long
protected JdbcResultSet
protected final int
protected final int
protected Session
protected long
Fields 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, XID
Fields 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 TypeMethodDescriptionvoid
Adds a statement to the batch.void
cancel()
Cancels a currently running statement.void
Clears the batch.void
Clears all warnings.void
close()
Closes this statement.protected void
INTERNAL.void
Specifies that this statement will be closed when its dependent result set is closed.enquoteIdentifier
(String identifier, boolean alwaysQuote) final boolean
Executes a statement and returns type of its result.final boolean
Executes a statement and returns type of its result.final boolean
Executes a statement and returns type of its result.final boolean
Executes a statement and returns type of its result.int[]
Executes the batch.long[]
Executes the batch.final long
executeLargeUpdate
(String sql) Executes a statement (insert, update, delete, create, drop) and returns the update count.final long
executeLargeUpdate
(String sql, int autoGeneratedKeys) Executes a statement and returns the update count.final long
executeLargeUpdate
(String sql, int[] columnIndexes) Executes a statement and returns the update count.final long
executeLargeUpdate
(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 int
executeUpdate
(String sql) Executes a statement (insert, update, delete, create, drop) and returns the update count.final int
executeUpdate
(String sql, int autoGeneratedKeys) Executes a statement and returns the update count.final int
executeUpdate
(String sql, int[] columnIndexes) Executes a statement and returns the update count.final int
executeUpdate
(String sql, String[] columnNames) Executes a statement and returns the update count.Returns the connection that created this object.int
Gets the fetch direction.int
Gets the number of rows suggested to read in one step.final ResultSet
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.long
Gets the maximum number of rows for a ResultSet.final long
Returns the last update count of this statement.int
Gets the maximum number of bytes for a result set column.int
Gets the maximum number of rows for a ResultSet.boolean
Moves to the next result set - however there is always only one result set.boolean
getMoreResults
(int current) Move to the next result set.int
Gets the current query timeout in seconds.Returns the last result set produces by this statement.int
Gets the result set concurrency created by this object.int
Gets the result set holdability.int
Gets the result set type.final int
Returns the last update count of this statement.Gets the first warning reported by calls on this object.boolean
Check whether the statement was cancelled.boolean
isClosed()
Returns whether this statement is closed.boolean
Returns whether this statement will be closed when its dependent result set is closed.boolean
Returns whether this object is poolable.boolean
isSimpleIdentifier
(String identifier) boolean
isWrapperFor
(Class<?> iface) Checks if unwrap can return an object of this class.void
setCursorName
(String name) Sets the name of the cursor.void
setEscapeProcessing
(boolean enable) Enables or disables processing or JDBC escape syntax.void
setFetchDirection
(int direction) Sets the fetch direction.void
setFetchSize
(int rows) Sets the number of rows suggested to read in one step.void
setLargeMaxRows
(long maxRows) Gets the maximum number of rows for a ResultSet.void
setMaxFieldSize
(int max) Sets the maximum number of bytes for a result set column.void
setMaxRows
(int maxRows) Gets the maximum number of rows for a ResultSet.void
setPoolable
(boolean poolable) Requests that this object should be pooled or not.void
setQueryTimeout
(int seconds) Sets the current query timeout in seconds.toString()
INTERNAL<T> T
Return 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, unsupported
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods 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:
executeQuery
in 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:
executeUpdate
in 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_INFO
if number of rows is too large for theint
data 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:
executeLargeUpdate
in 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:
execute
in 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:
getResultSet
in interfaceStatement
- Returns:
- the result set
- Throws:
SQLException
-
getUpdateCount
Returns the last update count of this statement.- Specified by:
getUpdateCount
in 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_INFO
if number of rows is too large for theint
data type) - Throws:
SQLException
- if this object is closed or invalid- See Also:
-
getLargeUpdateCount
Returns the last update count of this statement.- Specified by:
getLargeUpdateCount
in 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:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceStatement
- Throws:
SQLException
-
getConnection
Returns the connection that created this object.- Specified by:
getConnection
in 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:
getWarnings
in interfaceStatement
- Returns:
- null
- Throws:
SQLException
-
clearWarnings
Clears all warnings. As this driver does not support warnings, this call is ignored.- Specified by:
clearWarnings
in interfaceStatement
- Throws:
SQLException
-
setCursorName
Sets the name of the cursor. This call is ignored.- Specified by:
setCursorName
in 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:
setFetchDirection
in interfaceStatement
- Parameters:
direction
- ignored- Throws:
SQLException
- if this object is closed
-
getFetchDirection
Gets the fetch direction.- Specified by:
getFetchDirection
in interfaceStatement
- Returns:
- FETCH_FORWARD
- Throws:
SQLException
- if this object is closed
-
getMaxRows
Gets the maximum number of rows for a ResultSet.- Specified by:
getMaxRows
in 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:
getLargeMaxRows
in 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:
setMaxRows
in 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:
setLargeMaxRows
in 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:
setFetchSize
in 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:
getFetchSize
in 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:
getResultSetConcurrency
in interfaceStatement
- Returns:
- the concurrency
- Throws:
SQLException
-
getResultSetType
Gets the result set type.- Specified by:
getResultSetType
in 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:
getMaxFieldSize
in 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:
setMaxFieldSize
in 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:
setEscapeProcessing
in 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:
cancel
in 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:
getQueryTimeout
in 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:
setQueryTimeout
in 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:
addBatch
in interfaceStatement
- Parameters:
sql
- the SQL statement- Throws:
SQLException
-
clearBatch
Clears the batch.- Specified by:
clearBatch
in interfaceStatement
- Throws:
SQLException
-
executeBatch
Executes the batch. If one of the batched statements fails, this database will continue.- Specified by:
executeBatch
in 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:
executeLargeBatch
in 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 ... USING
commands.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_KEYS
is used H2 chooses columns to return automatically. The following columns are chosen:- Columns with sequences including
IDENTITY
columns 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:
getGeneratedKeys
in 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:
getMoreResults
in interfaceStatement
- Returns:
- false
- Throws:
SQLException
- if this object is closed.
-
getMoreResults
Move to the next result set. This method always returns false.- Specified by:
getMoreResults
in 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:
executeUpdate
in interfaceStatement
- Parameters:
sql
- the SQL statementautoGeneratedKeys
-Statement.RETURN_GENERATED_KEYS
if generated keys should be available for retrieval,Statement.NO_GENERATED_KEYS
if 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_INFO
if number of rows is too large for theint
data 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:
executeLargeUpdate
in interfaceStatement
- Parameters:
sql
- the SQL statementautoGeneratedKeys
-Statement.RETURN_GENERATED_KEYS
if generated keys should be available for retrieval,Statement.NO_GENERATED_KEYS
if 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:
executeUpdate
in 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_INFO
if number of rows is too large for theint
data 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:
executeLargeUpdate
in 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:
executeUpdate
in 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_INFO
if number of rows is too large for theint
data 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:
executeLargeUpdate
in 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:
execute
in interfaceStatement
- Parameters:
sql
- the SQL statementautoGeneratedKeys
-Statement.RETURN_GENERATED_KEYS
if generated keys should be available for retrieval,Statement.NO_GENERATED_KEYS
if 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:
execute
in 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:
execute
in 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:
getResultSetHoldability
in interfaceStatement
- Returns:
- the holdability
- Throws:
SQLException
-
closeOnCompletion
Specifies that this statement will be closed when its dependent result set is closed.- Specified by:
closeOnCompletion
in 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:
isCloseOnCompletion
in interfaceStatement
- Returns:
true
if 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:
isClosed
in interfaceStatement
- Returns:
- true if the statement is closed
- Throws:
SQLException
-
unwrap
Return an object of this class if possible.- Specified by:
unwrap
in interfaceWrapper
- Parameters:
iface
- the class- Returns:
- this
- Throws:
SQLException
-
isWrapperFor
Checks if unwrap can return an object of this class.- Specified by:
isWrapperFor
in 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:
isPoolable
in interfaceStatement
- Returns:
- false
-
setPoolable
public void setPoolable(boolean poolable) Requests that this object should be pooled or not. This call is ignored.- Specified by:
setPoolable
in interfaceStatement
- Parameters:
poolable
- the requested value
-
enquoteIdentifier
- Specified by:
enquoteIdentifier
in interfaceStatement
- Parameters:
identifier
- identifier to quote if required, may be quoted or unquotedalwaysQuote
- iftrue
identifier will be quoted unconditionally- Returns:
- specified identifier quoted if required, explicitly requested, or if it was already quoted
- Throws:
NullPointerException
- if identifier isnull
SQLException
- if identifier is not a valid identifier
-
isSimpleIdentifier
- Specified by:
isSimpleIdentifier
in interfaceStatement
- Parameters:
identifier
- identifier to check- Returns:
- is specified identifier may be used without quotes
- Throws:
NullPointerException
- if identifier isnull
SQLException
-
toString
INTERNAL
-