3
A
回答
0
所有的Java类都是从Groovy中使用。如果Groovy没有给你一个方法去做,那么你可以使用JDBC callable statements来实现Java方式。
0
我只是碰到什么可能是你的问题的解决方案迷迷糊糊的,如果一个例子是,你是什么之后,看看the reply to this thread
2
我写了一个帮手,让我用存储过程的工作,以与使用groovy.sql.Sql
的查询类似的方式返回单个ResultSet。这可以很容易地适应处理多个ResultSet(我假设每个都需要它自己的闭包)。
用法:
Sql sql = Sql.newInstance(dataSource)
SqlHelper helper = new SqlHelper(sql);
helper.eachSprocRow('EXEC sp_my_sproc ?, ?, ?', ['a', 'b', 'c']) { row ->
println "foo=${row.foo}, bar=${row.bar}, baz=${row.baz}"
}
代码:
class SqlHelper {
private Sql sql;
SqlHelper(Sql sql) {
this.sql = sql;
}
public void eachSprocRow(String query, List parameters, Closure closure) {
sql.cacheConnection { Connection con ->
CallableStatement proc = con.prepareCall(query)
try {
parameters.eachWithIndex { param, i ->
proc.setObject(i+1, param)
}
boolean result = proc.execute()
boolean found = false
while (!found) {
if (result) {
ResultSet rs = proc.getResultSet()
ResultSetMetaData md = rs.getMetaData()
int columnCount = md.getColumnCount()
while (rs.next()) {
// use case insensitive map
Map row = new TreeMap(String.CASE_INSENSITIVE_ORDER)
for (int i = 0; i < columnCount; ++ i) {
row[md.getColumnName(i+1)] = rs.getObject(i+1)
}
closure.call(row)
}
found = true;
} else if (proc.getUpdateCount() < 0) {
throw new RuntimeException("Sproc ${query} did not return a result set")
}
result = proc.getMoreResults()
}
} finally {
proc.close()
}
}
}
}
相关问题
- 1. 如何阻止从存储过程返回结果集?
- 2. Oracle查询/存储过程返回多个结果集
- 3. 返回多个结果集的存储过程
- 4. 存储过程返回多个结果集
- 5. 从存储过程返回一个结果集
- 6. WCF从存储过程返回结果
- 7. 如何获得从存储过程的结果集“返回”的行数
- 8. Apache DbUtils:处理从存储过程返回的多个结果集
- 9. 存储过程不返回结果
- 10. 返回存储过程的结果sql
- 11. 在VB从一个存储过程返回多个数据集
- 12. Oracle存储过程返回多个结果到.NET
- 13. 从groovy readline返回结果
- 14. mybatis是否支持返回多于1个结果集的存储过程?
- 15. SQL Server JDBC在调用存储过程时返回多个结果集
- 16. 从存储过程得到结果集
- 17. 从存储过程合并结果集
- 18. 如何返回多个结果集?
- 19. 如何从结果集对象返回多个行发送servlet
- 20. 从多个结果集中返回单个结果集
- 21. 从SQL Server存储过程获得空结果存储过程,返回XML
- 22. 存储过程是否可以返回结果集?
- 23. 存储过程结果集与返回值
- 24. MS SQL存储过程返回的结果集与ODBC
- 25. MySQL - 存储过程不返回结果集
- 26. Oracle存储过程返回分页结果集
- 27. 为什么NHibernate需要Oracle存储过程返回结果集?
- 28. 存储过程返回多个结果集,但结果集的数量不固定
- 29. 如何将存储过程的多个结果存储到数据集中?
- 30. 如果SQL存储过程正常运行,如何让SAS返回一个值?