2
votes

I have set up a user DSN ODBC datasource in WinXP (Version: Excel 97-2000) for a very simple excel spreadsheet:

A_NUMBER  A_DATE      A_STRING
1001      10/1/2012   Red
1002      10/2/2012   Green
1003      10/3/2012   Blue

When I run the following groovy script (Groovy Version: 1.7.8 JVM: 1.6.0_10) to read the data

import groovy.sql.Sql

def static main(def args) {
    def dbParameters = [url: 'jdbc:odbc:mySpreadSheet', user:'', password:'', driver: 'sun.jdbc.odbc.JdbcOdbcDriver']          
    def sql = Sql.newInstance(dbParameters)

    sql.eachRow('select * from [Sheet1$]') { row ->
        println "${row.A_NUMBER} ${row.A_DATE} ${row.A_STRING}"         
    }

    sql.close()

    println "done???"
}

it produces the following output:

1001.0 2012-10-01 00:00:00.0 Red
1002.0 2012-10-02 00:00:00.0 Green
1003.0 2012-10-03 00:00:00.0 Blue
done???

but it never exits!

I have tried running it from a windows command prompt and a cygwin bash shell and in both cases it hangs until I kill with a ctrl-c

I can force the script to exit by adding

throw new RuntimeException("force exit")

after the println but that seems pretty extreme.

Any idea why the script is hanging?

2
Have you tried not using the "def static main" and putting all your code outside of the method? - djangofan
Yes. Same result. Also tried making it a real class and creating a new instance of it in main. No luck :( - klassek
I have also "tried turning it off and on again" and re-creating the ODBC datasource. - klassek
System.exit(0) will be a little bit nicer... - rdmueller
Thanks Ralf. I was skeptical but I tried it and works. Much cleaner than throwing a Runtime exception. Still curious about why it hangs without the System.exit(0) call... - klassek

2 Answers

2
votes

Never really found an answer but the best suggestion was from Ralf which was to add the line

System.exit(0)

at the end of the the main method.

1
votes

I setup this test myself using your code, exactly as it is and I did not have any problem with the script exiting when it was done. I think it has something to do with your Cygwin shell. If you run it in a regular DOS shell, you will probably find that it exits properly.

import groovy.sql.Sql
def static main(def args) {
    def dbParameters = [url: 'jdbc:odbc:RefList10000', user:'', password:'', driver: 'sun.jdbc.odbc.JdbcOdbcDriver']          
    def sql = Sql.newInstance(dbParameters)
    sql.eachRow('select * from [referenceList10000$]') { row ->
        println "${row.RefListName} ${row.Value}"         
    }
    sql.close()
    println "done"
}

Lauched by:

@echo off
:: Microsoft Windows [Version 6.1.7601]
SET JAVA_HOME=C:\Java\jdk1.6.0_33_x32
SET PATH=%JAVA_HOME%\bin;%PATH%
groovy SQL.gv
pause