Java Reference
In-Depth Information
Listing 8.6. A
ProductDAO
implementation using the
groovy.sql.Sql
class
The
groovy.sql.Sql
classcontainsastaticfactorymethodcalled
newInstance
that
returns a new instance of the class. The method is overloaded for a variety of parameters;
see the GroovyDocs for details.
The
execute
method takes an SQL string and, naturally enough, executes it. Here I'm
using a multiline string to make the
create table
and
insert into
statements easier
to read. The
Sql
class takes care of opening a connection and closing it when finished.
The Sql class
The
groovy.sql.Sql
class does everything raw JDBC does, and handles resource
management as well.
The same
execute
method can be used to delete products: