Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

I modified the code to get it to work with Oracle and for readability.
The Sql.newInstance will connect to an Oracle database SID called XE installed on localhost
at port 1521. The database user is "username" and the password is "password".
In order to get this to work you will have to install the Oracle ojdb14the latest jdbc.jar file from your databaseOracle's website.  You need to install the .jar file into the
lib directory into the in your GROOVY_HOME lib directory:

Code Block
import groovy.sql.Sql

sql = Sql.newInstance( "jdbc:oracle:thin:@localhost:1521:XE", "username", "password", "oracle.jdbc.OracleDriver" )

createTable	= 'CREATE TABLE TableA ( FirstName varchar2( 40 ), LastName varchar2( 40 ) )'
insertIntoTable = 'INSERT INTO TableA ( FirstName, LastName ) values ( ?, ? )'
selectStatement = 'SELECT * FROM TableA'

sql.execute( createTable )
sql.execute( insertIntoTable, [ 'Bill', 'Lyons' ] )

// the eachRow method is an Iterator
sql.eachRow( selectStatement ) {

  println "TableA row: ${ it.firstName }, ${ it.lastName }" }
}

...