Saturday, August 11, 2007

GDBC: easy queries with Groovy

I have blogged before about peeking at an OODB with Groovy. It was first "wow" factor with the language.

Here's an easy illustration of using Groovy and JDBC. Though my team uses a full-strength DB query tool, I find it useful to have a minimalist program to either (a) ping the DB or (b) perform a sanity check query.


// Be sure that the driver jar is in the classpath
// note the cool closure!

import groovy.sql.Sql

sql = Sql.newInstance("jdbc:oracle:thin:@machine:1521:THE_DB",
"user","pwd", "oracle.jdbc.driver.OracleDriver")

sql.eachRow("select * from a_table") {
row -> println "value = " + row.a_column
}


Final score: Groovy 1 Uncertainty 0

No comments:

Post a Comment