public final class

SecureDatabaseResultSet

extends Object
implements AutoCloseable
java.lang.Object
   ↳ com.sap.cloud.mobile.foundation.securestore.SecureDatabaseResultSet

Class Overview

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.

Summary

Public Methods
void close()
Closes the result set.
int columnIndex(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.
byte[] getBlob(String columnName)
Retrieves the value as a byte array for the specified column name.
byte[] getBlob(int columnIndex)
Retrieves the value as a byte array at the specified column position.
boolean getBoolean(int columnIndex)
Retrieves the value as a boolean value for the given column.
boolean getBoolean(String columnName)
Retrieves the value as a boolean value for the given column.
Double getDouble(int columnIndex)
Retrieves the value as a Double for the given column.
Double getDouble(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(String columnName)
Retrieves the value as a Float for the given column.
Integer getInt(String columnName)
Retrieves the value as a signed 32 bit Integer for the given column.
Integer getInt(int columnIndex)
Retrieves the value as a signed 32 bit Integer for the given column.
Short getInt16(String columnName)
Retrieves the value as a signed 16 bit integer (Short) for the given column.
Short getInt16(int columnIndex)
Retrieves the value as a signed 16 bit integer (Short) for the given column.
Long getInt64(String columnName)
Retrieves the value as a signed 64 bit integer (Long) for the given column.
Long getInt64(int columnIndex)
Retrieves the value as a signed 64 bit integer (Long) for the given column.
String getString(String columnName)
Retrieves the value as a String for the given column.
String getString(int columnIndex)
Retrieves the value as a String for the given column.
boolean isColumnValueNull(int columnIndex)
Checks if the value at the specified column position is null.
boolean isColumnValueNull(String columnName)
Checks if the value for the specified column name is null.
boolean next()
Moves to the next row in the result set.
[Expand]
Inherited Methods
From class java.lang.Object
From interface java.lang.AutoCloseable

Public Methods

public void close ()

Closes the result set.

public int columnIndex (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.
Throws
BackingStoreException if the column with the given name does not exist.

public 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.
Throws
BackingStoreException if the column at the specified position does not exist.

public int count ()

Retrieves the number of rows in the result set.

Returns
  • The number of rows in the result set.

public byte[] getBlob (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.

public 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.

public 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.

public boolean getBoolean (String columnName)

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

Parameters
columnName column name
Returns
  • True or false.

public 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.

public Double getDouble (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.

public 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.

public Float getFloat (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.

public Integer getInt (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.

public 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.

public Short getInt16 (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.

public 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.

public Long getInt64 (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.

public 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.

public String getString (String columnName)

Retrieves the value as a String for the given column.

Parameters
columnName column name
Returns
  • String value of the column or null.

public 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.

public 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.

public boolean isColumnValueNull (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.

public 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.
Throws
FileClosedException if the result set has been closed.
BackingStoreException if error occurs when retrieving the next row.