Class SecureDatabaseResultSet
-
- All Implemented Interfaces:
-
java.io.Closeable,java.lang.AutoCloseable
public final class SecureDatabaseResultSet implements CloseableExecuting a SQL query returns a
SecureDatabaseResultSetobject. You typically use awhile()loop to iterate over the results of the query when you expect the result to contain multiple rows until next returnsfalse:// 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.
-
-
Method Summary
Modifier and Type Method Description booleannext()Moves to the next row in the result set. intcolumnIndex(@NonNull() String columnName)Returns the column index of the specified column name. StringcolumnName(int columnIndex)Returns the column name at the specified column position. intcount()Retrieves the number of rows in the result set. booleanisColumnValueNull(int columnIndex)Checks if the value at the specified column position is null.booleanisColumnValueNull(@NonNull() String columnName)Checks if the value for the specified column name is null.StringgetString(int columnIndex)Retrieves the value as a Stringfor the given column.StringgetString(@NonNull() String columnName)Retrieves the value as a Stringfor the given column.IntegergetInt(int columnIndex)Retrieves the value as a signed 32 bit Integerfor the given column.IntegergetInt(@NonNull() String columnName)Retrieves the value as a signed 32 bit Integerfor 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. ShortgetInt16(int columnIndex)Retrieves the value as a signed 16 bit integer ( Short) for the given column.ShortgetInt16(@NonNull() String columnName)Retrieves the value as a signed 16 bit integer ( Short) for the given column.LonggetInt64(int columnIndex)Retrieves the value as a signed 64 bit integer ( Long) for the given column.LonggetInt64(@NonNull() String columnName)Retrieves the value as a signed 64 bit integer ( Long) for the given column.DoublegetDouble(int columnIndex)Retrieves the value as a Doublefor the given column.DoublegetDouble(@NonNull() String columnName)Retrieves the value as a Doublefor the given column.FloatgetFloat(int columnIndex)Retrieves the value as a Floatfor the given column.FloatgetFloat(@NonNull() String columnName)Retrieves the value as a Floatfor the given column.voidclose()Closes the result set. booleangetBoolean(@NonNull() String columnName)Retrieves the value as a boolean value for the given column. booleangetBoolean(int columnIndex)Retrieves the value as a boolean value for the given column. -
-
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 thegetmethods based on the table column type.Note that if any
getmethod is still called after this method returnsfalse,net.sqlcipher.CursorIndexOutOfBoundsExceptionwill be thrown from thegetmethod.- Returns:
Trueif pointing to a valid row,falseotherwise.
-
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:
Trueif the value isnull,falseotherwise.
-
isColumnValueNull
boolean isColumnValueNull(@NonNull() String columnName)
Checks if the value for the specified column name is
null.- Parameters:
columnName- column name- Returns:
Trueif the value is null,falseotherwise.
-
getString
@Nullable() String getString(int columnIndex)
Retrieves the value as a
Stringfor 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
Stringfor 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
Integerfor the given column.- Parameters:
columnIndex- zero-based column index- Returns:
The
Integervalue of the column ornull.
-
getInt
@Nullable() Integer getInt(@NonNull() String columnName)
Retrieves the value as a signed 32 bit
Integerfor the given column.- Parameters:
columnName- column name- Returns:
The
Integervalue of the column ornull.
-
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
Shortvalue of the column ornull.
-
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
Shortvalue of the column ornull.
-
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
Longvalue of the column ornull.
-
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
Longvalue of the column ornull.
-
getDouble
@Nullable() Double getDouble(int columnIndex)
Retrieves the value as a
Doublefor the given column.- Parameters:
columnIndex- zero-based column index- Returns:
The
Doublevalue of the column ornull.
-
getDouble
@Nullable() Double getDouble(@NonNull() String columnName)
Retrieves the value as a
Doublefor the given column.- Parameters:
columnName- column name- Returns:
The
Doublevalue of the column ornull.
-
getFloat
@Nullable() Float getFloat(int columnIndex)
Retrieves the value as a
Floatfor the given column.- Parameters:
columnIndex- zero-based column index- Returns:
The
Floatvalue of the column ornull.
-
getFloat
@Nullable() Float getFloat(@NonNull() String columnName)
Retrieves the value as a
Floatfor the given column.- Parameters:
columnName- column name- Returns:
The
Floatvalue of the column ornull.
-
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:
Trueorfalse.
-
getBoolean
boolean getBoolean(int columnIndex)
Retrieves the value as a boolean value for the given column.
- Parameters:
columnIndex- zero-based column index- Returns:
Trueorfalse.
-
-
-
-