Groovy supports a few neat ways to work with SQL more easily and to make SQL more Groovy.
You can perform queries and SQL statements, passing in variables easily with proper handling of statements, connections and exception handling thanks to closures.
In the above example, you can refer to the various columns by name, using the property syntax on the row variable (e.g. it.name) or you can refer to the columns by their index (e.g. it0) For example
Or you can create a DataSet which allows you to query SQL using familar closure syntax so that the same query could work easily on in memory objects or via SQL. e.g.
The source code can be found here :
Examples
Here's an example of using Groovy SQL along with
GroovyMarkup .
This could generate, dynamically something like
There's an example
test case which demonstrates all of these query mechanisms in action.
Stored procedure support
SqlGeneratorTest Example: