从Grails服务调用PLSQL存储过程

时间:2013-05-07 19:46:02

标签: grails groovy plsql oracle11g

需要一点帮助,关闭部分,也许还有一点。我正试图从我的Grails服务调用Oracle 11g中的存储过程。

到目前为止:

import java.sql.*
import groovy.sql.Sql
import oracle.jdbc.driver.OracleTypes

class DummyService {

def dataSource

def serviceMethod() {
}

def listPeople(){
    Sql sql = new groovy.sql.Sql(dataSource)
    def resultList = []

    sql.call("BEGIN mypackage.p_get_people(?); END;",
            [Sql.resultSet(OracleTypes.CURSOR)]) {cursorResults -> 

            if(cursorResults.next()) {
                results = cursorResults.getAt(1);
            }
    }
    return resultList
}

好吧,所以这会返回第一行数据,并且根据传递给getAt()方法的内容,我可以获取该列。我在这里找到ORACLE STORED PROCS IN GRAILS

我真正想要的是返回结果集并将其放入列表中,我只是不知道该怎么做。

当我尝试{cursorResults -> println cursorResults}时会抛出错误

Message: org.apache.commons.dbcp.DelegatingCallableStatement with Address: "oracle.jdbc.driver.T4CCallableStatement@...."is closed

直接在Oracle中运行此过程,光标中有457行,如果这有帮助的话。

编辑1: 对dmahapatro的回应,这是NPE

| Error 2013-05-07 14:16:05,123 [http-bio-8080-exec-1] ERROR errors.GrailsExceptionResolver  - NullPointerException occurred when processing request: [GET] /testapp/messages/list
Stacktrace follows:
Message: null
Line | Method
->>  15 | list     in testapp.MessagesController$$EO5AzzAw
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   195 | doFilter in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|    63 | doFilter in grails.plugin.cache.web.filter.AbstractFilter
|   886 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
|   908 | run . .  in     ''
^   662 | run      in java.lang.Thread

现在第15行只是println dummyService.listPeople()

2 个答案:

答案 0 :(得分:1)

sql.call结果为GroovyResultSet。您应该能够对结果集执行eachRow并将其推送到列表中。

def listPeople(){
    Sql sql = new groovy.sql.Sql(dataSource)
    def resultList = []

    sql.call("BEGIN mypackage.p_get_people(?); END;",
            [Sql.resultSet(OracleTypes.CURSOR)]) {cursorResults -> 
            cursorResults.eachRow{result ->
                resultList << result
            }
    }
    return resultList
}

修改

或者使用sql.eachRow

    sql.eachRow("BEGIN mypackage.p_get_people(?); END;",
            [Sql.resultSet(OracleTypes.CURSOR)]) {row -> 
                resultList << row
    }

答案 1 :(得分:0)

唯一对我有用的是:

def array = []
sql.call("begin SCHEMA.PKG_NAME.PROCEDURE_NAME(${personId},${Sql.resultSet OracleTypes.CURSOR}); end;") 
            {rset -> 
                rset.eachRow(){                        
                    array.add(["NAME":it.getString("TABLE_COLUMN_NAME"),"DESC":it.getString("TABLE_COLUMN_DESC")])
                }
            }

当我没有使用it.getString(&#34; TABLE_COLUMN_NAME&#34;)时,它会在我尝试的所有内容上给我一个NULL错误&#34; println&#34;