Using DataSet

In Database Select, we saw how to process the results set obtained from executing a SELECT query. If we want to receive only a filtered set of rows, such as only cities with temperature values below 32, we can set up the query accordingly. Alternatively, we can receive the result as a groovy.sql.DataSet to filter data. Let’s examine this further.

The dataSet method of the Sql class takes a table’s name and returns a virtual proxy—it does not fetch the actual rows until we iterate. We can then iterate over the rows using the each method of the DataSet (like the eachRow method of Sql). In the following code, however, we’ll use the findAll method to filter the results to obtain only cities with below-freezing temperatures. When ...

Get Programming Groovy 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.