This version of the manual is outdated. For the latest version, follow this link: http://www.jooq.org/doc/3.0/manual.
| The jOOQ User Manual. Multiple Pages : jOOQ classes and their usage : Results, Cursors and Records | previous : next |
# The Result
The Result<R extends Record> is essentially a wrapper for a List<R extends Record> providing many convenience methods for accessing single elements in the result set. Depending on the type of SELECT statement, <R> can be bound to a sub-type of Record, for instance to an org.jooq.UpdatableRecord. See the section on Updatable Records for further details.
# The Cursor
A similar object is the Cursor<R extends Record>. Unlike the Result, the cursor has not fetched all data from the database yet. This means, you save memory (and potentially speed), but you can only access data sequentially and you have to keep a JDBC ResultSet alive. Cursors behave very much like the java.util.Iterator, by providing a very simple API. Some sample methods are:
// Check whether there are any more records to be fetched boolean hasNext() throws DataAccessException; // Fetch the next record from the underlying JDBC ResultSet R fetchOne() throws DataAccessException; // Close the underlying JDBC ResultSet. Don't forget to call this, before disposing the Cursor. void close() throws DataAccessException;
# The Record
The Record itself holds all the data from your selected tuple. If it is a org.jooq.TableRecord, then it corresponds exactly to the type of one of your physical tables in your database. But any anonymous or ad-hoc tuple can be represented by the plain Record. A record mainly provides access to its data and adds convenience methods for data type conversion. These are the main access ways:
// If you can keep a reference of the selected field, then you can get the corresponding value type-safely <T> T getValue(Field<T> field); // If you know the name of the selected field within the tuple, // then you can get its value without any type information Object getValue(String fieldName); // If you know the index of the selected field within the tuple, // then you can get its value without any type information Object getValue(int index);
In some cases, you will not be able to reference the selected Fields both when you create the SELECT statement and when you fetch data from Records. Then you might use field names or indexes, as with JDBC. However, of course, the type information will then be lost as well. If you know what type you want to get, you can always use the Record's convenience methods for type conversion, however. Some examples:
// These methods will try to convert a value to a BigDecimal. // This will work for all numeric types and for CHAR/VARCHAR types, if they contain numeric values: BigDecimal getValueAsBigDecimal(String fieldName); BigDecimal getValueAsBigDecimal(int fieldIndex); // This method can perform arbitrary conversions <T> T getValue(String fieldName, Class<? extends T> type); <T> T getValue(int fieldIndex, Class<? extends T> type);
For more information about the type conversions that are supported by jOOQ, read the Javadoc on org.jooq.tools.Convert
| The jOOQ User Manual. Multiple Pages : jOOQ classes and their usage : Results, Cursors and Records | previous : next |
