用Oracle 10g调用firstRow的groovy.sql.Sql错误

时间:2013-03-25 14:08:43

标签: oracle groovy

我正在使用Groovy的groovy.sql.Sql来查询Oracle 10g数据库。它工作得非常好,除了firstRow我遇到的一个奇怪的问题。基本上,我每次使用它时都会得到java.sql.SQLException: Invalid column type - eachRow可以正常工作。

有没有其他人遇到这个?如果是这样,有解决方法吗?这种情况发生在Groovy 1.8.8和2.1.1中。到目前为止,我只在Windows XP上试过这个。

示例代码:

import groovy.sql.Sql

// ojdbc14_ver_10_2_0_4.jar and orai18n_ver_10_2_0_4.jar is in the classpath
// Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - 64bit

sql = Sql.newInstance("jdbc:oracle:thin:@dbhost:1500:DBNAME", "user", "pass", "oracle.jdbc.driver.OracleDriver")


// This should return only one row (verified by querying the database).
// You can use any query that returns only one row.
String query = "select ITEM_A, ITEM_B, ITEM_C from MY_TBL where ITEM_D = 1233"

Long itemA = 0L
Long itemB = 0L, itemC = 0L

Integer rowCount = 0
sql.eachRow(query) { row ->
    itemA = row.ITEM_A
    itemB = row.ITEM_B
    itemC = row.ITEM_C
    rowCount++
}
assert rowCount <= 1 // 0 or 1 rows returned

println "Item A = $itemA"
println "Item B = $itemB"
println "Item C = $itemC"
// works fine

// now we have a problem:
sql.firstRow(query) { row ->
    itemA = row.ITEM_A
    itemB = row.ITEM_B
    itemC = row.ITEM_C
}
println "Item A = $itemA"
println "Item B = $itemB"
println "Item C = $itemC"

这导致以下输出:

>>groovy firstRowTest.groovy
Item A = 640
Item B = 90
Item C = 90
25-Mar-2013 13:49:24 groovy.sql.Sql$AbstractQueryCommand execute
WARNING: Failed to execute:
                select ITEM_A, ITEM_B, ITEM_C from MY_TBL where ITEM_D = 1233
              because: Invalid column type
Caught: java.sql.SQLException: Invalid column type
java.sql.SQLException: Invalid column type
        at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
        at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
        at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:208)
        at oracle.jdbc.driver.OraclePreparedStatement.setObjectCritical(OraclePreparedStatement.java:9262)
        at oracle.jdbc.driver.OraclePreparedStatement.setObjectInternal(OraclePreparedStatement.java:8843)
        at oracle.jdbc.driver.OraclePreparedStatement.setObjectInternal(OraclePreparedStatement.java:9565)
        at oracle.jdbc.driver.OraclePreparedStatement.setObject(OraclePreparedStatement.java:9548)
        at firstRowTest.run(firstRowTest.groovy:30)

2 个答案:

答案 0 :(得分:2)

firstRow没有将Closure作为参数,你能看出这是否会产生同样的错误吗?

def frow = sql.firstRow(query)
itemA = frow.ITEM_A
itemB = frow.ITEM_B
itemC = frow.ITEM_C

我怀疑,它正在尝试call this method,并将Closure作为参数传递

答案 1 :(得分:1)

我相信你可以说:

sql.firstRow(query).with{row ->
    itemA = row.ITEM_A
    itemB = row.ITEM_B
    itemC = row.ITEM_C     
}

这允许您传递从firstRow方法返回的GroovyRowResult的闭包。