Interface FieldNamedPreparedStatement

  • All Superinterfaces:
    AutoCloseable
    All Known Implementing Classes:
    FieldNamedPreparedStatementImpl

    @PublicEvolving
    public interface FieldNamedPreparedStatement
    extends AutoCloseable
    This is a wrapper around PreparedStatement and allows the users to set parameters by name instead of by index. This allows users to use the same variable parameter multiple times in a statement.

    Code such as this:

       Connection con = getConnection();
       String query = "select * from my_table where first_name=? or last_name=?";
       PreparedStatement st = con.prepareStatement(query);
       st.setString(1, "bob");
       st.setString(2, "bob");
       ResultSet rs = st.executeQuery();
     

    Can be replaced with:

       Connection con = getConnection();
       String query = "select * from my_table where first_name=:name or last_name=:name";
       FieldNamedPreparedStatement st = FieldNamedPreparedStatement.prepareStatement(con, query, new String[]{"name"});
       st.setString(0, "bob");
       ResultSet rs = st.executeQuery();
     
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      void addBatch()
      Adds a set of parameters to this NamedPreparedStatement object's batch of commands.
      void clearParameters()
      Clears the current parameter values immediately.
      void close()
      Releases this Statement object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed.
      int[] executeBatch()
      Submits a batch of commands to the database for execution and if all commands execute successfully, returns an array of update counts.
      ResultSet executeQuery()
      Executes the SQL query in this NamedPreparedStatement object and returns the ResultSet object generated by the query.
      static FieldNamedPreparedStatement prepareStatement​(Connection connection, String sql, String[] fieldNames)
      Creates a NamedPreparedStatement object for sending parameterized SQL statements to the database.
      void setBigDecimal​(int fieldIndex, BigDecimal x)
      Sets the designated parameter to the given java.math.BigDecimal value.
      void setBoolean​(int fieldIndex, boolean x)
      Sets the designated parameter to the given Java boolean value.
      void setByte​(int fieldIndex, byte x)
      Sets the designated parameter to the given Java byte value.
      void setBytes​(int fieldIndex, byte[] x)
      Sets the designated parameter to the given Java array of bytes.
      void setDate​(int fieldIndex, Date x)
      Sets the designated parameter to the given java.sql.Date value using the default time zone of the virtual machine that is running the application.
      void setDouble​(int fieldIndex, double x)
      Sets the designated parameter to the given Java double value.
      void setFloat​(int fieldIndex, float x)
      Sets the designated parameter to the given Java float value.
      void setInt​(int fieldIndex, int x)
      Sets the designated parameter to the given Java int value.
      void setLong​(int fieldIndex, long x)
      Sets the designated parameter to the given Java long value.
      void setNull​(int fieldIndex, int sqlType)
      Sets the designated parameter to SQL NULL.
      void setObject​(int fieldIndex, Object x)
      Sets the value of the designated parameter using the given object.
      void setShort​(int fieldIndex, short x)
      Sets the designated parameter to the given Java short value.
      void setString​(int fieldIndex, String x)
      Sets the designated parameter to the given Java String value.
      void setTime​(int fieldIndex, Time x)
      Sets the designated parameter to the given java.sql.Time value.
      void setTimestamp​(int fieldIndex, Timestamp x)
      Sets the designated parameter to the given java.sql.Timestamp value.
    • Method Detail

      • prepareStatement

        static FieldNamedPreparedStatement prepareStatement​(Connection connection,
                                                            String sql,
                                                            String[] fieldNames)
                                                     throws SQLException
        Creates a NamedPreparedStatement object for sending parameterized SQL statements to the database.
        Parameters:
        connection - the connection used to connect to database.
        sql - an SQL statement that may contain one or more ':fieldName' as parameter placeholders
        fieldNames - the field names in schema order used as the parameter names
        Throws:
        SQLException
      • clearParameters

        void clearParameters()
                      throws SQLException
        Clears the current parameter values immediately.

        In general, parameter values remain in force for repeated use of a statement. Setting a parameter value automatically clears its previous value. However, in some cases it is useful to immediately release the resources used by the current parameter values; this can be done by calling the method clearParameters.

        Throws:
        SQLException
        See Also:
        PreparedStatement.clearParameters()
      • executeBatch

        int[] executeBatch()
                    throws SQLException
        Submits a batch of commands to the database for execution and if all commands execute successfully, returns an array of update counts. The int elements of the array that is returned are ordered to correspond to the commands in the batch, which are ordered according to the order in which they were added to the batch.
        Throws:
        SQLException
        See Also:
        Statement.executeBatch()
      • setString

        void setString​(int fieldIndex,
                       String x)
                throws SQLException
        Sets the designated parameter to the given Java String value. The driver converts this to an SQL VARCHAR or LONGVARCHAR value (depending on the argument's size relative to the driver's limits on VARCHAR values) when it sends it to the database.
        Throws:
        SQLException
        See Also:
        PreparedStatement.setString(int, String)
      • setBytes

        void setBytes​(int fieldIndex,
                      byte[] x)
               throws SQLException
        Sets the designated parameter to the given Java array of bytes. The driver converts this to an SQL VARBINARY or LONGVARBINARY (depending on the argument's size relative to the driver's limits on VARBINARY values) when it sends it to the database.
        Throws:
        SQLException
        See Also:
        PreparedStatement.setBytes(int, byte[])
      • setDate

        void setDate​(int fieldIndex,
                     Date x)
              throws SQLException
        Sets the designated parameter to the given java.sql.Date value using the default time zone of the virtual machine that is running the application. The driver converts this to an SQL DATE value when it sends it to the database.
        Throws:
        SQLException
        See Also:
        PreparedStatement.setDate(int, Date)
      • close

        void close()
            throws SQLException
        Releases this Statement object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed. It is generally good practice to release resources as soon as you are finished with them to avoid tying up database resources.
        Specified by:
        close in interface AutoCloseable
        Throws:
        SQLException
        See Also:
        Statement.close()