Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

GSQL

 Show me a simple GSQL application?

Here you are:

The output should be:

TableA row: Stan, Juka

Why does my statement doesn't work? (GString Special Treatment)

Why the INSERT in this code fails?

Because the insertSQL is a GString.  if you make it a String like this

 it will work.  Or you can do it like this:

In the last code snippet, GSQL will automatically create a prepared statement for you and run it.


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 ojdb14.jar file from your database's
lib directory into the GROOVY_HOME lib directory:

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: $

Unknown macro: { it.firstName }

, $

Unknown macro: { it.lastName }

"

}

Does the following:

1. Create's TableA in username's schema
2. Inserts a row into TableA
3. Performs a SELECT * FROM TableA
4. Returns "TableA row: Bill Lyons"

  • No labels