Jul 07 2008
Databases with Groovy
GORM is cool, but groovy Sql is for the simpler cases were you just need to interact with the database.
groovy.sql.Sql is just an amazing Groovy class. See this link for a mind-opener level of ease when dealing with databases and this is an interesting article from IBM Developer works : Practically Groovy: JDBC programming with Groovy
Watch :
Define your database sql object (aka database swiss-nife)
With Oracle
sql = groovy.sql.Sql.newInstance("jdbc:oracle:thin:@myoraclehost:1521:mydbinstance", "myuser", "mypassword", "oracle.jdbc.driver.OracleDriver")
With MySql
sql = groovy.sql.Sql.newInstance("jdbc:mysql://localhost:3306/mydb", "user", "pswd", "com.mysql.jdbc.Driver")
and voala, you’ve got a database connection that you use to conduct most needed db operations.
Remember to place your Oracle JDBC driver (in my case ojdbc14.jar) and/or your mysql JDBC driver (mysql-connector-java-5.1.6-bin.jar) into ~/.groovy/lib
Did I mention that you can easily call Oracle Stored procedure?
Three input parameters, two output parameters, and one single line of code
import groovy.sql.Sql sql.call ("{ call MY_SP ( 'My in param', ${Sql.inout(Sql.INTEGER(context))}, 'my in param2', 'myin params3', ${Sql.inout(Sql.VARCHAR(result))}) }") {context,result -> println "$context and $result" }