Class SecureDatabaseResultSet

  • All Implemented Interfaces:
    java.io.Closeable , java.lang.AutoCloseable

    
    public final class SecureDatabaseResultSet
     implements Closeable
                        

    Executing a SQL query returns a SecureDatabaseResultSet object. You typically use a while() loop to iterate over the results of the query when you expect the result to contain multiple rows until next returns false:

    
        // Queries the database and iterates through the rows in the result set.
        final String queryAllUsers = "SELECT * FROM users";
        try (SecureDatabaseResultSet rs = store.executeQuery(queryAllUsers)) {
            while (rs.next()) {
                // Has a valid row, retrieves the column values with appropriate getter method.
                int age = rs.getInt("age");
                String name = rs.getString("name");
                String email = rs.getString("email");
                logger.debug("Retrieved user: age = {}, name = {}, email = {}", age, name, email);
            }
        } catch (BackingStoreException ex) {
            logger.error("Failed to execute query.", ex);
        }
    

    You must always invoke next() before attempting to access the values returned in a query, even if you are only expecting one row in the result set:

    
        int userCount = -1;
        try (SecureDatabaseResultSet rs =  store.executeQuery("SELECT COUNT(*) AS count FROM users")) {
            if (rs.next()) {
                userCount = rs.getInt(0);
                logger.debug("Number of users: {}", userCount);
            }
        } catch (BackingStoreException ex) {
            logger.error("Failed to get user count.", ex);
        }
    

    A SecureDatabaseResultSet contains many methods for retrieving different data types in an appropriate format.

    Each data retrieval method has two variants to retrieve the data based on the position of the column in the results, as opposed to the column’s name:

    • getInt
    • getInt16
    • getInt64
    • getDouble
    • getFloat
    • getBoolean
    • getBlob
    • getString

    SecureDatabaseResultSet needs to be closed. It will be closed automatically if you use a try-with block. Otherwise, you need to call close method explicitly.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      boolean next() Moves to the next row in the result set.
      int columnIndex(@NonNull() String columnName) Returns the column index of the specified column name.
      String columnName(int columnIndex) Returns the column name at the specified column position.
      int count() Retrieves the number of rows in the result set.
      boolean isColumnValueNull(int columnIndex) Checks if the value at the specified column position is null.
      boolean isColumnValueNull(@NonNull() String columnName) Checks if the value for the specified column name is null.
      String getString(int columnIndex) Retrieves the value as a String for the given column.
      String getString(@NonNull() String columnName) Retrieves the value as a String for the given column.
      Integer getInt(int columnIndex) Retrieves the value as a signed 32 bit Integer for the given column.
      Integer getInt(@NonNull() String columnName) Retrieves the value as a signed 32 bit Integer for the given column.
      Array<byte> getBlob(int columnIndex) Retrieves the value as a byte array at the specified column position.
      Array<byte> getBlob(@NonNull() String columnName) Retrieves the value as a byte array for the specified column name.
      Short getInt16(int columnIndex) Retrieves the value as a signed 16 bit integer (Short) for the given column.
      Short getInt16(@NonNull() String columnName) Retrieves the value as a signed 16 bit integer (Short) for the given column.
      Long getInt64(int columnIndex) Retrieves the value as a signed 64 bit integer (Long) for the given column.
      Long getInt64(@NonNull() String columnName) Retrieves the value as a signed 64 bit integer (Long) for the given column.
      Double getDouble(int columnIndex) Retrieves the value as a Double for the given column.
      Double getDouble(@NonNull() String columnName) Retrieves the value as a Double for the given column.
      Float getFloat(int columnIndex) Retrieves the value as a Float for the given column.
      Float getFloat(@NonNull() String columnName) Retrieves the value as a Float for the given column.
      void close() Closes the result set.
      boolean getBoolean(@NonNull() String columnName) Retrieves the value as a boolean value for the given column.
      boolean getBoolean(int columnIndex) Retrieves the value as a boolean value for the given column.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

    • Method Detail

      • next

         boolean next()

        Moves to the next row in the result set. If this method returns true (a valid row), then you can proceed with call(s) to one of the get methods based on the table column type.

        Note that if any get method is still called after this method returns false, net.sqlcipher.CursorIndexOutOfBoundsException will be thrown from the get method.

        Returns:

        True if pointing to a valid row, false otherwise.

      • columnIndex

         int columnIndex(@NonNull() String columnName)

        Returns the column index of the specified column name.

        Parameters:
        columnName - column name
        Returns:

        The zero-based column index for the column name.

      • columnName

        @Nullable() String columnName(int columnIndex)

        Returns the column name at the specified column position.

        Parameters:
        columnIndex - column index
        Returns:

        The column name at the specified column position.

      • count

         int count()

        Retrieves the number of rows in the result set.

        Returns:

        The number of rows in the result set.

      • isColumnValueNull

         boolean isColumnValueNull(int columnIndex)

        Checks if the value at the specified column position is null.

        Parameters:
        columnIndex - the column index
        Returns:

        True if the value is null, false otherwise.

      • isColumnValueNull

         boolean isColumnValueNull(@NonNull() String columnName)

        Checks if the value for the specified column name is null.

        Parameters:
        columnName - column name
        Returns:

        True if the value is null, false otherwise.

      • getString

        @Nullable() String getString(int columnIndex)

        Retrieves the value as a String for the given column.

        Parameters:
        columnIndex - zero-based column index
        Returns:

        String value of the column or null.

      • getString

        @Nullable() String getString(@NonNull() String columnName)

        Retrieves the value as a String for the given column.

        Parameters:
        columnName - column name
        Returns:

        String value of the column or null.

      • getInt

        @Nullable() Integer getInt(int columnIndex)

        Retrieves the value as a signed 32 bit Integer for the given column.

        Parameters:
        columnIndex - zero-based column index
        Returns:

        The Integer value of the column or null.

      • getInt

        @Nullable() Integer getInt(@NonNull() String columnName)

        Retrieves the value as a signed 32 bit Integer for the given column.

        Parameters:
        columnName - column name
        Returns:

        The Integer value of the column or null.

      • getBlob

        @Nullable() Array<byte> getBlob(int columnIndex)

        Retrieves the value as a byte array at the specified column position.

        Parameters:
        columnIndex - zero-based column index
        Returns:

        The value of the column as a byte array or null.

      • getBlob

        @Nullable() Array<byte> getBlob(@NonNull() String columnName)

        Retrieves the value as a byte array for the specified column name.

        Parameters:
        columnName - column name
        Returns:

        The value of the column as a byte array or null.

      • getInt16

        @Nullable() Short getInt16(int columnIndex)

        Retrieves the value as a signed 16 bit integer (Short) for the given column.

        Parameters:
        columnIndex - zero-based column index
        Returns:

        The Short value of the column or null.

      • getInt16

        @Nullable() Short getInt16(@NonNull() String columnName)

        Retrieves the value as a signed 16 bit integer (Short) for the given column.

        Parameters:
        columnName - column name
        Returns:

        The Short value of the column or null.

      • getInt64

        @Nullable() Long getInt64(int columnIndex)

        Retrieves the value as a signed 64 bit integer (Long) for the given column.

        Parameters:
        columnIndex - zero-based column index
        Returns:

        The Long value of the column or null.

      • getInt64

        @Nullable() Long getInt64(@NonNull() String columnName)

        Retrieves the value as a signed 64 bit integer (Long) for the given column.

        Parameters:
        columnName - column name
        Returns:

        The Long value of the column or null.

      • getDouble

        @Nullable() Double getDouble(int columnIndex)

        Retrieves the value as a Double for the given column.

        Parameters:
        columnIndex - zero-based column index
        Returns:

        The Double value of the column or null.

      • getDouble

        @Nullable() Double getDouble(@NonNull() String columnName)

        Retrieves the value as a Double for the given column.

        Parameters:
        columnName - column name
        Returns:

        The Double value of the column or null.

      • getFloat

        @Nullable() Float getFloat(int columnIndex)

        Retrieves the value as a Float for the given column.

        Parameters:
        columnIndex - zero-based column index
        Returns:

        The Float value of the column or null.

      • getFloat

        @Nullable() Float getFloat(@NonNull() String columnName)

        Retrieves the value as a Float for the given column.

        Parameters:
        columnName - column name
        Returns:

        The Float value of the column or null.

      • close

         void close()

        Closes the result set.

      • getBoolean

         boolean getBoolean(@NonNull() String columnName)

        Retrieves the value as a boolean value for the given column.

        Parameters:
        columnName - column name
        Returns:

        True or false.

      • getBoolean

         boolean getBoolean(int columnIndex)

        Retrieves the value as a boolean value for the given column.

        Parameters:
        columnIndex - zero-based column index
        Returns:

        True or false.