Package org.h2.jdbc

Class JdbcPreparedStatement

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

public class JdbcPreparedStatement extends JdbcStatement implements PreparedStatement
Represents a prepared statement.

Thread safety: the prepared statement is not thread-safe. If the same prepared statement is used by multiple threads access to it must be synchronized. The single synchronized block must include assignment of parameters, execution of the command and all operations with its result.

 synchronized (prep) {
     prep.setInt(1, 10);
     try (ResultSet rs = prep.executeQuery()) {
         while (rs.next) {
             // Do something
         }
     }
 }
 synchronized (prep) {
     prep.setInt(1, 15);
     updateCount = prep.executeUpdate();
 }
 
  • Field Details

    • command

      protected org.h2.command.CommandInterface command
  • Method Details

    • executeQuery

      public ResultSet executeQuery() 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 PreparedStatement
      Returns:
      the result set
      Throws:
      SQLException - if this object is closed or invalid
    • executeUpdate

      public int executeUpdate() throws SQLException
      Executes a statement (insert, update, delete, create, drop) and returns the update count. 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 PreparedStatement
      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 returns nothing, or Statement.SUCCESS_NO_INFO if number of rows is too large for int data type)
      Throws:
      SQLException - if this object is closed or invalid
      See Also:
    • executeLargeUpdate

      public long executeLargeUpdate() throws SQLException
      Executes a statement (insert, update, delete, create, drop) and returns the update count. 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 PreparedStatement
      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 returns nothing)
      Throws:
      SQLException - if this object is closed or invalid
    • execute

      public boolean execute() throws SQLException
      Executes an arbitrary statement. If another result set exists for this statement, this will be closed (even if this statement fails). If auto commit is on, and the statement is not a select, this statement will be committed.
      Specified by:
      execute in interface PreparedStatement
      Returns:
      true if a result set is available, false if not
      Throws:
      SQLException - if this object is closed or invalid
    • clearParameters

      public void clearParameters() throws SQLException
      Clears all parameters.
      Specified by:
      clearParameters in interface PreparedStatement
      Throws:
      SQLException - if this object is closed or invalid
    • executeQuery

      public ResultSet executeQuery(String sql) throws SQLException
      Calling this method is not legal on a PreparedStatement.
      Specified by:
      executeQuery in interface Statement
      Overrides:
      executeQuery in class JdbcStatement
      Parameters:
      sql - ignored
      Returns:
      the result set
      Throws:
      SQLException - Unsupported Feature
    • addBatch

      public void addBatch(String sql) throws SQLException
      Calling this method is not legal on a PreparedStatement.
      Specified by:
      addBatch in interface Statement
      Overrides:
      addBatch in class JdbcStatement
      Parameters:
      sql - ignored
      Throws:
      SQLException - Unsupported Feature
    • setNull

      public void setNull(int parameterIndex, int sqlType) throws SQLException
      Sets a parameter to null.
      Specified by:
      setNull in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      sqlType - the data type (Types.x)
      Throws:
      SQLException - if this object is closed
    • setInt

      public void setInt(int parameterIndex, int x) throws SQLException
      Sets the value of a parameter.
      Specified by:
      setInt in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      Throws:
      SQLException - if this object is closed
    • setString

      public void setString(int parameterIndex, String x) throws SQLException
      Sets the value of a parameter.
      Specified by:
      setString in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      Throws:
      SQLException - if this object is closed
    • setBigDecimal

      public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException
      Sets the value of a parameter.
      Specified by:
      setBigDecimal in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      Throws:
      SQLException - if this object is closed
    • setDate

      public void setDate(int parameterIndex, Date x) throws SQLException
      Sets the value of a parameter.

      Usage of this method is discouraged. Use setObject(parameterIndex, value) with LocalDate parameter instead.

      Specified by:
      setDate in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      Throws:
      SQLException - if this object is closed
      See Also:
    • setTime

      public void setTime(int parameterIndex, Time x) throws SQLException
      Sets the value of a parameter.

      Usage of this method is discouraged. Use setObject(parameterIndex, value) with LocalTime parameter instead.

      Specified by:
      setTime in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      Throws:
      SQLException - if this object is closed
      See Also:
    • setTimestamp

      public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException
      Sets the value of a parameter.

      Usage of this method is discouraged. Use setObject(parameterIndex, value) with LocalDateTime parameter instead.

      Specified by:
      setTimestamp in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      Throws:
      SQLException - if this object is closed
      See Also:
    • setObject

      public void setObject(int parameterIndex, Object x) throws SQLException
      Sets the value of a parameter. Objects of unknown classes are serialized (on the client side).
      Specified by:
      setObject in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      Throws:
      SQLException - if this object is closed
    • setObject

      public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException
      Sets the value of a parameter. The object is converted, if required, to the specified data type before sending to the database. Objects of unknown classes are serialized (on the client side).
      Specified by:
      setObject in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value, null is allowed
      targetSqlType - the type as defined in java.sql.Types
      Throws:
      SQLException - if this object is closed
    • setObject

      public void setObject(int parameterIndex, Object x, int targetSqlType, int scale) throws SQLException
      Sets the value of a parameter. The object is converted, if required, to the specified data type before sending to the database. Objects of unknown classes are serialized (on the client side).
      Specified by:
      setObject in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value, null is allowed
      targetSqlType - the type as defined in java.sql.Types
      scale - is ignored
      Throws:
      SQLException - if this object is closed
    • setObject

      public void setObject(int parameterIndex, Object x, SQLType targetSqlType) throws SQLException
      Sets the value of a parameter. The object is converted, if required, to the specified data type before sending to the database. Objects of unknown classes are serialized (on the client side).
      Specified by:
      setObject in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value, null is allowed
      targetSqlType - the SQL type
      Throws:
      SQLException - if this object is closed
    • setObject

      public void setObject(int parameterIndex, Object x, SQLType targetSqlType, int scaleOrLength) throws SQLException
      Sets the value of a parameter. The object is converted, if required, to the specified data type before sending to the database. Objects of unknown classes are serialized (on the client side).
      Specified by:
      setObject in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value, null is allowed
      targetSqlType - the SQL type
      scaleOrLength - is ignored
      Throws:
      SQLException - if this object is closed
    • setBoolean

      public void setBoolean(int parameterIndex, boolean x) throws SQLException
      Sets the value of a parameter.
      Specified by:
      setBoolean in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      Throws:
      SQLException - if this object is closed
    • setByte

      public void setByte(int parameterIndex, byte x) throws SQLException
      Sets the value of a parameter.
      Specified by:
      setByte in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      Throws:
      SQLException - if this object is closed
    • setShort

      public void setShort(int parameterIndex, short x) throws SQLException
      Sets the value of a parameter.
      Specified by:
      setShort in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      Throws:
      SQLException - if this object is closed
    • setLong

      public void setLong(int parameterIndex, long x) throws SQLException
      Sets the value of a parameter.
      Specified by:
      setLong in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      Throws:
      SQLException - if this object is closed
    • setFloat

      public void setFloat(int parameterIndex, float x) throws SQLException
      Sets the value of a parameter.
      Specified by:
      setFloat in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      Throws:
      SQLException - if this object is closed
    • setDouble

      public void setDouble(int parameterIndex, double x) throws SQLException
      Sets the value of a parameter.
      Specified by:
      setDouble in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      Throws:
      SQLException - if this object is closed
    • setRef

      public void setRef(int parameterIndex, Ref x) throws SQLException
      [Not supported] Sets the value of a column as a reference.
      Specified by:
      setRef in interface PreparedStatement
      Throws:
      SQLException
    • setDate

      public void setDate(int parameterIndex, Date x, Calendar calendar) throws SQLException
      Sets the date using a specified time zone. The value will be converted to the local time zone.

      Usage of this method is discouraged. Use setObject(parameterIndex, value) with LocalDate parameter instead.

      Specified by:
      setDate in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      calendar - the calendar
      Throws:
      SQLException - if this object is closed
      See Also:
    • setTime

      public void setTime(int parameterIndex, Time x, Calendar calendar) throws SQLException
      Sets the time using a specified time zone. The value will be converted to the local time zone.

      Usage of this method is discouraged. Use setObject(parameterIndex, value) with LocalTime parameter instead.

      Specified by:
      setTime in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      calendar - the calendar
      Throws:
      SQLException - if this object is closed
      See Also:
    • setTimestamp

      public void setTimestamp(int parameterIndex, Timestamp x, Calendar calendar) throws SQLException
      Sets the timestamp using a specified time zone. The value will be converted to the local time zone.

      Usage of this method is discouraged. Use setObject(parameterIndex, value) with LocalDateTime parameter instead.

      Specified by:
      setTimestamp in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      calendar - the calendar
      Throws:
      SQLException - if this object is closed
      See Also:
    • setUnicodeStream

      @Deprecated public void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException
      Deprecated.
      since JDBC 2.0, use setCharacterStream
      [Not supported] This feature is deprecated and not supported.
      Specified by:
      setUnicodeStream in interface PreparedStatement
      Throws:
      SQLException
    • setNull

      public void setNull(int parameterIndex, int sqlType, String typeName) throws SQLException
      Sets a parameter to null.
      Specified by:
      setNull in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      sqlType - the data type (Types.x)
      typeName - this parameter is ignored
      Throws:
      SQLException - if this object is closed
    • setBlob

      public void setBlob(int parameterIndex, Blob x) throws SQLException
      Sets the value of a parameter as a Blob.
      Specified by:
      setBlob in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      Throws:
      SQLException - if this object is closed
    • setBlob

      public void setBlob(int parameterIndex, InputStream x) throws SQLException
      Sets the value of a parameter as a Blob. This method does not close the stream. The stream may be closed after executing the statement.
      Specified by:
      setBlob in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      Throws:
      SQLException - if this object is closed
    • setClob

      public void setClob(int parameterIndex, Clob x) throws SQLException
      Sets the value of a parameter as a Clob.
      Specified by:
      setClob in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      Throws:
      SQLException - if this object is closed
    • setClob

      public void setClob(int parameterIndex, Reader x) throws SQLException
      Sets the value of a parameter as a Clob. This method does not close the reader. The reader may be closed after executing the statement.
      Specified by:
      setClob in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      Throws:
      SQLException - if this object is closed
    • setArray

      public void setArray(int parameterIndex, Array x) throws SQLException
      Sets the value of a parameter as an Array.
      Specified by:
      setArray in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      Throws:
      SQLException - if this object is closed
    • setBytes

      public void setBytes(int parameterIndex, byte[] x) throws SQLException
      Sets the value of a parameter as a byte array.
      Specified by:
      setBytes in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      Throws:
      SQLException - if this object is closed
    • setBinaryStream

      public void setBinaryStream(int parameterIndex, InputStream x, long length) throws SQLException
      Sets the value of a parameter as an input stream. This method does not close the stream. The stream may be closed after executing the statement.
      Specified by:
      setBinaryStream in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      length - the maximum number of bytes
      Throws:
      SQLException - if this object is closed
    • setBinaryStream

      public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException
      Sets the value of a parameter as an input stream. This method does not close the stream. The stream may be closed after executing the statement.
      Specified by:
      setBinaryStream in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      length - the maximum number of bytes
      Throws:
      SQLException - if this object is closed
    • setBinaryStream

      public void setBinaryStream(int parameterIndex, InputStream x) throws SQLException
      Sets the value of a parameter as an input stream. This method does not close the stream. The stream may be closed after executing the statement.
      Specified by:
      setBinaryStream in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      Throws:
      SQLException - if this object is closed
    • setAsciiStream

      public void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException
      Sets the value of a parameter as an ASCII stream. This method does not close the stream. The stream may be closed after executing the statement.
      Specified by:
      setAsciiStream in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      length - the maximum number of bytes
      Throws:
      SQLException - if this object is closed
    • setAsciiStream

      public void setAsciiStream(int parameterIndex, InputStream x, long length) throws SQLException
      Sets the value of a parameter as an ASCII stream. This method does not close the stream. The stream may be closed after executing the statement.
      Specified by:
      setAsciiStream in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      length - the maximum number of bytes
      Throws:
      SQLException - if this object is closed
    • setAsciiStream

      public void setAsciiStream(int parameterIndex, InputStream x) throws SQLException
      Sets the value of a parameter as an ASCII stream. This method does not close the stream. The stream may be closed after executing the statement.
      Specified by:
      setAsciiStream in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      Throws:
      SQLException - if this object is closed
    • setCharacterStream

      public void setCharacterStream(int parameterIndex, Reader x, int length) throws SQLException
      Sets the value of a parameter as a character stream. This method does not close the reader. The reader may be closed after executing the statement.
      Specified by:
      setCharacterStream in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      length - the maximum number of characters
      Throws:
      SQLException - if this object is closed
    • setCharacterStream

      public void setCharacterStream(int parameterIndex, Reader x) throws SQLException
      Sets the value of a parameter as a character stream. This method does not close the reader. The reader may be closed after executing the statement.
      Specified by:
      setCharacterStream in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      Throws:
      SQLException - if this object is closed
    • setCharacterStream

      public void setCharacterStream(int parameterIndex, Reader x, long length) throws SQLException
      Sets the value of a parameter as a character stream. This method does not close the reader. The reader may be closed after executing the statement.
      Specified by:
      setCharacterStream in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      length - the maximum number of characters
      Throws:
      SQLException - if this object is closed
    • setURL

      public void setURL(int parameterIndex, URL x) throws SQLException
      [Not supported]
      Specified by:
      setURL in interface PreparedStatement
      Throws:
      SQLException
    • getMetaData

      public ResultSetMetaData getMetaData() throws SQLException
      Gets the result set metadata of the query returned when the statement is executed. If this is not a query, this method returns null.
      Specified by:
      getMetaData in interface PreparedStatement
      Returns:
      the meta data or null if this is not a query
      Throws:
      SQLException - if this object is closed
    • clearBatch

      public void clearBatch() throws SQLException
      Clears the batch.
      Specified by:
      clearBatch in interface Statement
      Overrides:
      clearBatch in class JdbcStatement
      Throws:
      SQLException
    • 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
      Overrides:
      close in class JdbcStatement
      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
      Overrides:
      executeBatch in class JdbcStatement
      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
      Overrides:
      executeLargeBatch in class JdbcStatement
      Returns:
      the array of update counts
      Throws:
      SQLException
    • addBatch

      public void addBatch() throws SQLException
      Adds the current settings to the batch.
      Specified by:
      addBatch in interface PreparedStatement
      Throws:
      SQLException
    • getParameterMetaData

      public ParameterMetaData getParameterMetaData() throws SQLException
      Get the parameter meta data of this prepared statement.
      Specified by:
      getParameterMetaData in interface PreparedStatement
      Returns:
      the meta data
      Throws:
      SQLException
    • setRowId

      public void setRowId(int parameterIndex, RowId x) throws SQLException
      [Not supported] Sets the value of a parameter as a row id.
      Specified by:
      setRowId in interface PreparedStatement
      Throws:
      SQLException
    • setNString

      public void setNString(int parameterIndex, String x) throws SQLException
      Sets the value of a parameter.
      Specified by:
      setNString in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      Throws:
      SQLException - if this object is closed
    • setNCharacterStream

      public void setNCharacterStream(int parameterIndex, Reader x, long length) throws SQLException
      Sets the value of a parameter as a character stream. This method does not close the reader. The reader may be closed after executing the statement.
      Specified by:
      setNCharacterStream in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      length - the maximum number of characters
      Throws:
      SQLException - if this object is closed
    • setNCharacterStream

      public void setNCharacterStream(int parameterIndex, Reader x) throws SQLException
      Sets the value of a parameter as a character stream. This method does not close the reader. The reader may be closed after executing the statement.
      Specified by:
      setNCharacterStream in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      Throws:
      SQLException - if this object is closed
    • setNClob

      public void setNClob(int parameterIndex, NClob x) throws SQLException
      Sets the value of a parameter as a Clob.
      Specified by:
      setNClob in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      Throws:
      SQLException - if this object is closed
    • setNClob

      public void setNClob(int parameterIndex, Reader x) throws SQLException
      Sets the value of a parameter as a Clob. This method does not close the reader. The reader may be closed after executing the statement.
      Specified by:
      setNClob in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      Throws:
      SQLException - if this object is closed
    • setClob

      public void setClob(int parameterIndex, Reader x, long length) throws SQLException
      Sets the value of a parameter as a Clob. This method does not close the reader. The reader may be closed after executing the statement.
      Specified by:
      setClob in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      length - the maximum number of characters
      Throws:
      SQLException - if this object is closed
    • setBlob

      public void setBlob(int parameterIndex, InputStream x, long length) throws SQLException
      Sets the value of a parameter as a Blob. This method does not close the stream. The stream may be closed after executing the statement.
      Specified by:
      setBlob in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      length - the maximum number of bytes
      Throws:
      SQLException - if this object is closed
    • setNClob

      public void setNClob(int parameterIndex, Reader x, long length) throws SQLException
      Sets the value of a parameter as a Clob. This method does not close the reader. The reader may be closed after executing the statement.
      Specified by:
      setNClob in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      length - the maximum number of characters
      Throws:
      SQLException - if this object is closed
    • setSQLXML

      public void setSQLXML(int parameterIndex, SQLXML x) throws SQLException
      Sets the value of a parameter as a SQLXML.
      Specified by:
      setSQLXML in interface PreparedStatement
      Parameters:
      parameterIndex - the parameter index (1, 2, ...)
      x - the value
      Throws:
      SQLException - if this object is closed
    • toString

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