Skip to content

Instantly share code, notes, and snippets.

@jlmelville
Last active March 7, 2020 13:01
Show Gist options
  • Save jlmelville/57af8e92a2211189178a to your computer and use it in GitHub Desktop.
Save jlmelville/57af8e92a2211189178a to your computer and use it in GitHub Desktop.
JDBC drivers and Groovy
// Oracle example
@GrabResolver(name='nexus', root='http://path/to/some/nexus/server')
@GrabConfig(systemClassLoader=true)
@Grab(group='oracle', module='ojdbc6', version='11.2.0.4')
import groovy.sql.Sql
def hostname = "hostname of Oracle DB here"
def port = "port of the server here"
def db = "service name here"
def url = "jdbc:oracle:thin:@//${hostname}:${port}/${db}"
def username = "username"
def password = "password"
def driver = "oracle.jdbc.driver.OracleDriver"
def validationQuery = "select sysdate from dual"
Sql.withInstance(url, username, password, driver) { sql ->
sql.eachRow(validationQuery) { row ->
println row
}
}
// Postgres
@Grab(group='org.postgresql', module='postgresql', version='9.3-1101-jdbc41')
def url = "jdbc:postgresql://${hostname}:${port}/${db}"
def driver = "org.postgresql.Driver"
def validationQuery = "select 1"
// H2
@Grab(group='com.h2database', module='h2', version='1.3.173')
def url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=100000;DB_CLOSE_ON_EXIT=FALSE" // Grails dev DB
def driver = "org.h2.Driver"
def validationQuery = "SELECT 1"
@jlmelville
Copy link
Author

I will never remember the extra @Grab gyrations necessary to load the JDBC drivers at the right time; hence this gist.

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