Connecting to an SQL database
This recipe is about the various approaches we can use to connect to a database using Groovy and the groovy.sql.Sql
class that we briefly encountered in the Creating a database table recipe.
Getting ready
As mentioned in the first recipe of this chapter, we are going to reuse the DBUtil
class, which contains some helper methods to simplify our work with the database. Create a new Groovy script and add the following import
statements before adding the code presented in the next section:
@Grab('commons-dbcp:commons-dbcp:1.4') import static DBUtil.* import groovy.sql.Sql import org.apache.commons.dbcp.BasicDataSource
Do not forget to start the HyperSQL server by calling startServer
. As shown in the Creating a database table recipe, pass the -cp
argument to the groovy
command with the path to the folder with the DBUtil.groovy
script in order to execute the script containing the steps of this recipe.
How to do it...
The simplest way to connect to a running instance of...