gpt4 book ai didi

grails - 从Grails服务调用PLSQL存储的proc

转载 作者:行者123 更新时间:2023-12-02 15:33:08 24 4
gpt4 key购买 nike

在关闭部分方面需要一点帮助,也许还需要更多帮助。我正在尝试从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()

最佳答案

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
}

关于grails - 从Grails服务调用PLSQL存储的proc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16427270/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com