Package org.h2.jdbc

Class JdbcStatement

java.lang.Object
org.h2.message.TraceObject
org.h2.jdbc.JdbcStatement
All Implemented Interfaces:
AutoCloseable, Statement, Wrapper
Direct Known Subclasses:
JdbcPreparedStatement

public class JdbcStatement extends org.h2.message.TraceObject implements Statement
Represents a statement.

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 Details

    • conn

      protected JdbcConnection conn
    • session

      protected Session session
    • resultSet

      protected JdbcResultSet resultSet
    • maxRows

      protected long maxRows
    • fetchSize

      protected int fetchSize
    • updateCount

      protected long updateCount
    • generatedKeys

      protected JdbcResultSet generatedKeys
    • resultSetType

      protected final int resultSetType
    • resultSetConcurrency

      protected final int resultSetConcurrency
  • Method Details

    • executeQuery

      public ResultSet executeQuery(String sql) throws SQLException
      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 interface Statement
      Parameters:
      sql - the SQL statement to execute
      Returns:
      the result set
      Throws:
      SQLException
    • executeUpdate

      public final int executeUpdate(String sql) throws SQLException
      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 interface Statement
      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 the int data type)
      Throws:
      SQLException - if a database error occurred or a select statement was executed
      See Also:
    • executeLargeUpdate

      public final long executeLargeUpdate(String sql) throws SQLException
      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 interface Statement
      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

      public final boolean execute(String sql) throws SQLException
      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 interface Statement
      Parameters:
      sql - the SQL statement to execute
      Returns:
      true if result is a result set, false otherwise
      Throws:
      SQLException
    • getResultSet

      public ResultSet getResultSet() throws SQLException
      Returns the last result set produces by this statement.
      Specified by:
      getResultSet in interface Statement
      Returns:
      the result set
      Throws:
      SQLException
    • getUpdateCount

      public final int getUpdateCount() throws SQLException
      Returns the last update count of this statement.
      Specified by:
      getUpdateCount in interface 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 -1 if statement was a query, or Statement.SUCCESS_NO_INFO if number of rows is too large for the int data type)
      Throws:
      SQLException - if this object is closed or invalid
      See Also:
    • getLargeUpdateCount

      public final long getLargeUpdateCount() throws SQLException
      Returns the last update count of this statement.
      Specified by:
      getLargeUpdateCount in interface 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 -1 if statement was a query)
      Throws:
      SQLException - if this object is closed or invalid
    • close

      public void close() throws SQLException
      Closes this statement. All result sets that where created by this statement become invalid after calling this method.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Statement
      Throws:
      SQLException
    • getConnection

      public Connection getConnection()
      Returns the connection that created this object.
      Specified by:
      getConnection in interface Statement
      Returns:
      the connection
    • getWarnings

      public SQLWarning getWarnings() throws SQLException
      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 interface Statement
      Returns:
      null
      Throws:
      SQLException
    • clearWarnings

      public void clearWarnings() throws SQLException
      Clears all warnings. As this driver does not support warnings, this call is ignored.
      Specified by:
      clearWarnings in interface Statement
      Throws:
      SQLException
    • setCursorName

      public void setCursorName(String name) throws SQLException
      Sets the name of the cursor. This call is ignored.
      Specified by:
      setCursorName in interface Statement
      Parameters:
      name - ignored
      Throws:
      SQLException - if this object is closed
    • setFetchDirection

      public void setFetchDirection(int direction) throws SQLException
      Sets the fetch direction. This call is ignored by this driver.
      Specified by:
      setFetchDirection in interface Statement
      Parameters:
      direction - ignored
      Throws:
      SQLException - if this object is closed
    • getFetchDirection

      public int getFetchDirection() throws SQLException
      Gets the fetch direction.
      Specified by:
      getFetchDirection in interface Statement
      Returns:
      FETCH_FORWARD
      Throws:
      SQLException - if this object is closed
    • getMaxRows

      public int getMaxRows() throws SQLException
      Gets the maximum number of rows for a ResultSet.
      Specified by:
      getMaxRows in interface Statement
      Returns:
      the number of rows where 0 means no limit
      Throws:
      SQLException - if this object is closed
    • getLargeMaxRows

      public long getLargeMaxRows() throws SQLException
      Gets the maximum number of rows for a ResultSet.
      Specified by:
      getLargeMaxRows in interface Statement
      Returns:
      the number of rows where 0 means no limit
      Throws:
      SQLException - if this object is closed
    • setMaxRows

      public void setMaxRows(int maxRows) throws SQLException
      Gets the maximum number of rows for a ResultSet.
      Specified by:
      setMaxRows in interface Statement
      Parameters:
      maxRows - the number of rows where 0 means no limit
      Throws:
      SQLException - if this object is closed
    • setLargeMaxRows

      public void setLargeMaxRows(long maxRows) throws SQLException
      Gets the maximum number of rows for a ResultSet.
      Specified by:
      setLargeMaxRows in interface Statement
      Parameters:
      maxRows - the number of rows where 0 means no limit
      Throws:
      SQLException - if this object is closed
    • setFetchSize

      public void setFetchSize(int rows) throws SQLException
      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 interface Statement
      Parameters:
      rows - the number of rows
      Throws:
      SQLException - if this object is closed
    • getFetchSize

      public int getFetchSize() throws SQLException
      Gets the number of rows suggested to read in one step.
      Specified by:
      getFetchSize in interface Statement
      Returns:
      the current fetch size
      Throws:
      SQLException - if this object is closed
    • getResultSetConcurrency

      public int getResultSetConcurrency() throws SQLException
      Gets the result set concurrency created by this object.
      Specified by:
      getResultSetConcurrency in interface Statement
      Returns:
      the concurrency
      Throws:
      SQLException
    • getResultSetType

      public int getResultSetType() throws SQLException
      Gets the result set type.
      Specified by:
      getResultSetType in interface Statement
      Returns:
      the type
      Throws:
      SQLException - if this object is closed
    • getMaxFieldSize

      public int getMaxFieldSize() throws SQLException
      Gets the maximum number of bytes for a result set column.
      Specified by:
      getMaxFieldSize in interface Statement
      Returns:
      always 0 for no limit
      Throws:
      SQLException - if this object is closed
    • setMaxFieldSize

      public void setMaxFieldSize(int max) throws SQLException
      Sets the maximum number of bytes for a result set column. This method does currently do nothing for this driver.
      Specified by:
      setMaxFieldSize in interface Statement
      Parameters:
      max - the maximum size - ignored
      Throws:
      SQLException - if this object is closed
    • setEscapeProcessing

      public void setEscapeProcessing(boolean enable) throws SQLException
      Enables or disables processing or JDBC escape syntax. See also Connection.nativeSQL.
      Specified by:
      setEscapeProcessing in interface Statement
      Parameters:
      enable - - true (default) or false (no conversion is attempted)
      Throws:
      SQLException - if this object is closed
    • cancel

      public void cancel() throws SQLException
      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 interface Statement
      Throws:
      SQLException - if this object is closed
    • isCancelled

      public boolean isCancelled()
      Check whether the statement was cancelled.
      Returns:
      true if yes
    • getQueryTimeout

      public int getQueryTimeout() throws SQLException
      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 interface Statement
      Returns:
      the timeout in seconds
      Throws:
      SQLException - if this object is closed
    • setQueryTimeout

      public void setQueryTimeout(int seconds) throws SQLException
      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 interface Statement
      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

      public void addBatch(String sql) throws SQLException
      Adds a statement to the batch.
      Specified by:
      addBatch in interface Statement
      Parameters:
      sql - the SQL statement
      Throws:
      SQLException
    • clearBatch

      public void clearBatch() throws SQLException
      Clears the batch.
      Specified by:
      clearBatch in interface Statement
      Throws:
      SQLException
    • executeBatch

      public int[] executeBatch() throws SQLException
      Executes the batch. If one of the batched statements fails, this database will continue.
      Specified by:
      executeBatch in interface Statement
      Returns:
      the array of update counts
      Throws:
      SQLException
      See Also:
    • executeLargeBatch

      public long[] executeLargeBatch() throws SQLException
      Executes the batch. If one of the batched statements fails, this database will continue.
      Specified by:
      executeLargeBatch in interface Statement
      Returns:
      the array of update counts
      Throws:
      SQLException
    • getGeneratedKeys

      public final ResultSet getGeneratedKeys() throws SQLException
      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 with Statement.RETURN_GENERATED_KEYS, column indexes, or column names.

      Generated keys are only returned from from INSERT, UPDATE, MERGE INTO, and MERGE 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 with AUTO_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 interface Statement
      Returns:
      the possibly empty result set with generated keys
      Throws:
      SQLException - if this object is closed
    • getMoreResults

      public boolean getMoreResults() throws SQLException
      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 interface Statement
      Returns:
      false
      Throws:
      SQLException - if this object is closed.
    • getMoreResults

      public boolean getMoreResults(int current) throws SQLException
      Move to the next result set. This method always returns false.
      Specified by:
      getMoreResults in interface Statement
      Parameters:
      current - Statement.CLOSE_CURRENT_RESULT, Statement.KEEP_CURRENT_RESULT, or Statement.CLOSE_ALL_RESULTS
      Returns:
      false
      Throws:
      SQLException
    • executeUpdate

      public final int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException
      Executes a statement and returns the update count. This method is not allowed for prepared statements.
      Specified by:
      executeUpdate in interface Statement
      Parameters:
      sql - the SQL statement
      autoGeneratedKeys - 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 the int data type)
      Throws:
      SQLException - if a database error occurred or a select statement was executed
      See Also:
    • executeLargeUpdate

      public final long executeLargeUpdate(String sql, int autoGeneratedKeys) throws SQLException
      Executes a statement and returns the update count. This method is not allowed for prepared statements.
      Specified by:
      executeLargeUpdate in interface Statement
      Parameters:
      sql - the SQL statement
      autoGeneratedKeys - 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

      public final int executeUpdate(String sql, int[] columnIndexes) throws SQLException
      Executes a statement and returns the update count. This method is not allowed for prepared statements.
      Specified by:
      executeUpdate in interface Statement
      Parameters:
      sql - the SQL statement
      columnIndexes - 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 the int data type)
      Throws:
      SQLException - if a database error occurred or a select statement was executed
      See Also:
    • executeLargeUpdate

      public final long executeLargeUpdate(String sql, int[] columnIndexes) throws SQLException
      Executes a statement and returns the update count. This method is not allowed for prepared statements.
      Specified by:
      executeLargeUpdate in interface Statement
      Parameters:
      sql - the SQL statement
      columnIndexes - 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

      public final int executeUpdate(String sql, String[] columnNames) throws SQLException
      Executes a statement and returns the update count. This method is not allowed for prepared statements.
      Specified by:
      executeUpdate in interface Statement
      Parameters:
      sql - the SQL statement
      columnNames - 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 the int data type)
      Throws:
      SQLException - if a database error occurred or a select statement was executed
      See Also:
    • executeLargeUpdate

      public final long executeLargeUpdate(String sql, String[] columnNames) throws SQLException
      Executes a statement and returns the update count. This method is not allowed for prepared statements.
      Specified by:
      executeLargeUpdate in interface Statement
      Parameters:
      sql - the SQL statement
      columnNames - 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

      public final boolean execute(String sql, int autoGeneratedKeys) throws SQLException
      Executes a statement and returns type of its result. This method is not allowed for prepared statements.
      Specified by:
      execute in interface Statement
      Parameters:
      sql - the SQL statement
      autoGeneratedKeys - 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

      public final boolean execute(String sql, int[] columnIndexes) throws SQLException
      Executes a statement and returns type of its result. This method is not allowed for prepared statements.
      Specified by:
      execute in interface Statement
      Parameters:
      sql - the SQL statement
      columnIndexes - 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

      public final boolean execute(String sql, String[] columnNames) throws SQLException
      Executes a statement and returns type of its result. This method is not allowed for prepared statements.
      Specified by:
      execute in interface Statement
      Parameters:
      sql - the SQL statement
      columnNames - 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

      public int getResultSetHoldability() throws SQLException
      Gets the result set holdability.
      Specified by:
      getResultSetHoldability in interface Statement
      Returns:
      the holdability
      Throws:
      SQLException
    • closeOnCompletion

      public void closeOnCompletion() throws SQLException
      Specifies that this statement will be closed when its dependent result set is closed.
      Specified by:
      closeOnCompletion in interface Statement
      Throws:
      SQLException - if this statement is closed
    • isCloseOnCompletion

      public boolean isCloseOnCompletion() throws SQLException
      Returns whether this statement will be closed when its dependent result set is closed.
      Specified by:
      isCloseOnCompletion in interface Statement
      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

      public boolean isClosed() throws SQLException
      Returns whether this statement is closed.
      Specified by:
      isClosed in interface Statement
      Returns:
      true if the statement is closed
      Throws:
      SQLException
    • unwrap

      public <T> T unwrap(Class<T> iface) throws SQLException
      Return an object of this class if possible.
      Specified by:
      unwrap in interface Wrapper
      Parameters:
      iface - the class
      Returns:
      this
      Throws:
      SQLException
    • isWrapperFor

      public boolean isWrapperFor(Class<?> iface) throws SQLException
      Checks if unwrap can return an object of this class.
      Specified by:
      isWrapperFor in interface Wrapper
      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 interface Statement
      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 interface Statement
      Parameters:
      poolable - the requested value
    • enquoteIdentifier

      public String enquoteIdentifier(String identifier, boolean alwaysQuote) throws SQLException
      Specified by:
      enquoteIdentifier in interface Statement
      Parameters:
      identifier - identifier to quote if required, may be quoted or unquoted
      alwaysQuote - if true identifier will be quoted unconditionally
      Returns:
      specified identifier quoted if required, explicitly requested, or if it was already quoted
      Throws:
      NullPointerException - if identifier is null
      SQLException - if identifier is not a valid identifier
    • isSimpleIdentifier

      public boolean isSimpleIdentifier(String identifier) throws SQLException
      Specified by:
      isSimpleIdentifier in interface Statement
      Parameters:
      identifier - identifier to check
      Returns:
      is specified identifier may be used without quotes
      Throws:
      NullPointerException - if identifier is null
      SQLException
    • toString

      public String toString()
      INTERNAL
      Overrides:
      toString in class Object