Wednesday, July 8, 2009

Using Scala with groovy.sql.Sql

Short example of Scala using Groovy's Sql class. I'm new to Scala so I don't know if this is idiomatic. I used asInstanceOf to fully specify the type of the iterator.


import groovy.sql.Sql

val db = Sql.newInstance("jdbc:oracle:thin:@myhost:1521:mysid",
"myuser", "mypassword", "oracle.jdbc.driver.OracleDriver")

val sql = "select * from accounthistory where entrydate > sysdate - 100 and amount <> 0"

val iter = db.rows(sql).iterator().asInstanceOf[java.util.Iterator[java.util.Map[String,String]]]

while(iter.hasNext) {
val row = iter.next
printf("%13s\t%12s\n", row.get("ACCOUNTID"), row.get("AMOUNT"))
}