问题
Need a little help with the closure part of this, and maybe a bit more. I'm trying to call a stored procedure in Oracle 11g from my Grails service.
So far:
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
}
Alright, so this returns the first rows data, and depending on what is passed to the getAt() method, I can grab that column. Which I found here ORACLE STORED PROCS IN GRAILS
What I really want is to return the result set and put it into a list, I'm just not sure how to do it.
When I try {cursorResults -> println cursorResults}
it throws an error
Message: org.apache.commons.dbcp.DelegatingCallableStatement with Address: "oracle.jdbc.driver.T4CCallableStatement@...."is closed
Running this procedure directly in Oracle there is 457 rows in the cursor, if that helps at all.
Edit 1: Response to dmahapatro, this is the 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
And right now line 15 is simply println dummyService.listPeople()
回答1:
sql.call
results to GroovyResultSet
. You should be able to do an eachRow
on the resultset and push it to the list.
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
}
EDIT:
Alternatively using sql.eachRow
sql.eachRow("BEGIN mypackage.p_get_people(?); END;",
[Sql.resultSet(OracleTypes.CURSOR)]) {row ->
resultList << row
}
回答2:
the only thing that did work for me is this:
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")])
}
}
When I wasn't using it.getString("TABLE_COLUMN_NAME") it'd threw me a NULL error on everything I try to "println"
来源:https://stackoverflow.com/questions/16427270/calling-plsql-stored-proc-from-grails-service