6.8.5.3 Iterating Through a Result Set
There are two ways to iterate through a result set: in a JDBC-like manner or using the Java Iterator interface.
For JDBC-like iterations, the methods in
PgqlResultSet (package
oracle.pgx.api) are similar to the ones in
java.sql.ResultSet. A noteworthy difference is that
PGQL's result set interface is based on the new date and time library that
was introduced in Java 8, while java.sql.ResultSet is based
on the legacy java.util.Date. To bridge the gap, PGQL's
result set provides getLegacyDate(..) for applications that
still use java.util.Date.
PgqlResultSet has a cursor that
is initially set before the first row. Then, the following methods are
available to reposition the cursor:
next() : booleanprevious() : booleanbeforeFirst()afterLast()first() : booleanlast() : booleanabsolute(long row) : booleanrelative(long rows) : boolean
Above methods are documented in more detail here.
getObject(int columnIdx) : ObjectgetObject(String columnName) : ObjectgetString(int columnIdx) : StringgetString(String columnName) : StringgetInteger(int columnIdx) : IntegergetInteger(String columnName) : IntegergetLong(int columnIdx) : LonggetLong(String columnName) : LonggetFloat(int columnIdx) : FloatgetFloat(String columnName) : FloatgetDouble(int columnIdx) : DoublegetDouble(String columnName) : DoublegetBoolean(int columnIdx) : BooleangetBoolean(String columnName) : BooleangetVertexLabels(int columnIdx) : Set<String>getVertexLabels(String columnName) : Set<String>getDate(int columnIdx) : LocalDategetDate(String columnName) : LocalDategetTime(int columnIdx) : LocalTimegetTime(String columnName) : LocalTimegetTimestamp(int columnIdx) : LocalDateTimegetTimestamp(String columnName) : LocalDateTimegetTimeWithTimezone(int columnIdx) : OffsetTimegetTimeWithTimezone(String columnName) : OffsetTimegetTimestampWithTimezone(int columnIdx) : OffsetDateTimegetTimestampWithTimezone(String columnName) : OffsetDateTimegetLegacyDate(int columnIdx) : java.util.DategetLegacyDate(String columnName) : java.util.DategetVertex(int columnIdx) : PgxVertex<ID>getVertex(String columnName) : PgxVertex<ID>getEdge(int columnIdx) : PgxEdgegetEdge(String columnName) : PgxEdge
Above methods are documented in more detail here.
Finally, there is a PgqlResultSet.close() which releases the
result set’s resources, and there is a
PgqlResultSet.getMetaData() through which the
column names and column count can be retrieved.
An example for result set iteration is as follows:
PgqlResultSet resultSet = g.queryPgql(
" SELECT owner.name AS account_holder, SUM(t.amount) AS total_transacted_with_Nikita "
+ " FROM MATCH (p:Person) -[:ownerOf]-> (account1:Account) "
+ " , MATCH (account1) -[t:transaction]- (account2) "
+ " , MATCH (account2:Account) <-[:ownerOf]- (owner:Person|Company) "
+ " WHERE p.name = 'Nikita' "
+ " GROUP BY owner");
while (resultSet.next()) {
String accountHolder = resultSet.getString(1);
long totalTransacted = resultSet.getLong(2);
System.out.println(accountHolder + ": " + totalTransacted);
}
resultSet.close();
The output of the above example will look like:
Oracle: 4501
Camille: 1000
In addition, the PgqlResultSet is also
iterable via the Java Iterator interface. An example of a “for each
loop” over the result set is as follows:
for (PgxResult result : resultSet) {
String accountHolder = result.getString(1);
long totalTransacted = result.getLong(2);
System.out.println(accountHolder + ": " + totalTransacted);
}
The output of the above example will look like:
Oracle: 4501
Camille: 1000
Note that the same getters that are available for
PgqlResultSet are also available for
PgxResult.
Parent topic: Java APIs for Executing SELECT Queries