Skip to content

Instantly share code, notes, and snippets.

@mgdelacroix
Created July 16, 2014 12:18
Show Gist options
  • Save mgdelacroix/d663f654e150832a3b33 to your computer and use it in GitHub Desktop.
Save mgdelacroix/d663f654e150832a3b33 to your computer and use it in GitHub Desktop.
Groovy simple sql test
@GrabConfig(systemClassLoader=true)
@Grab('com.h2database:h2:1.4.180')
import groovy.sql.Sql
Sql sql = Sql.newInstance("jdbc:h2:/tmp/devDb", 'sa', '')
println "=================== TEST CONTENT"
println '>> You can invoke this script with two args to insert into test table (id, name)'
println '>> You can invoke this script with the argument CREATE or DESTROY to create or destroy the test table'
if(args.size() == 1 && args[0] == 'CREATE') {
sql.execute('create table test(id int primary key, name varchar)')
sql.execute("insert into test values(1, 'coca'), (2, 'cola')")
} else if(args.size() == 1 && args[0] == 'DESTROY') {
sql.execute('drop table test')
} else if (args.size() == 2) {
sql.execute("insert into test (id, name) values(${args[0]}, ${args[1]})")
} else {
sql.eachRow("select * from test") {
println '----'
println "ID: $it.id"
println "NAME: $it.name"
}
}
@mgdelacroix
Copy link
Author

You need the @GrabConfig(systemClassLoader=true) to use the sql driver

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment