Modifying data in an SQL database

The logical step after querying a database (see the Querying an SQL database recipe) is writing data to it. This recipe shows how a few lines of Groovy and very little boilerplate is needed to do the job of modifying data in a database table.

Getting ready

This recipe also uses the same table structure and the way to start a database server defined in the Creating a database table recipe.

How to do it...

As usual, create a new Groovy script named modifyDb.groovy. As for the previous recipes, in this case we also need to import the DBUtil class.

  1. Add the following code to the newly created script:
    import static DBUtil.*
    import groovy.sql.Sql
    
    def server = startServer()
    createSchema()
  2. The next logical step is to define the ...

Get Groovy 2 Cookbook 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.