Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从groovy运行返回多个结果集的存储过程_Groovy - Fatal编程技术网

如何从groovy运行返回多个结果集的存储过程

如何从groovy运行返回多个结果集的存储过程,groovy,Groovy,我在网上找不到这样做的好例子 有人可以演示如何从groovy运行存储过程(返回多个结果集)吗 基本上,我只是想确定存储过程返回多少个结果集。所有Java类都可以从Groovy中使用。如果Groovy没有为您提供一种方法,那么您可以使用Java方法来实现。我刚刚偶然发现了一种可能解决您的问题的方法,如果您想要的是一个示例,看一看我已经编写了一个帮助程序,它允许我使用返回单个结果集的存储过程,其方式类似于使用groovy.sql.sql查询。这可以很容易地适应于处理多个结果集(我假设每个结果集都需要

我在网上找不到这样做的好例子

有人可以演示如何从groovy运行存储过程(返回多个结果集)吗


基本上,我只是想确定存储过程返回多少个结果集。

所有Java类都可以从Groovy中使用。如果Groovy没有为您提供一种方法,那么您可以使用Java方法来实现。

我刚刚偶然发现了一种可能解决您的问题的方法,如果您想要的是一个示例,看一看

我已经编写了一个帮助程序,它允许我使用返回单个结果集的存储过程,其方式类似于使用
groovy.sql.sql
查询。这可以很容易地适应于处理多个结果集(我假设每个结果集都需要自己的闭包)

用法:

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}"
}
代码:

类SqlHelper{
私有Sql;
SqlHelper(Sql){
this.sql=sql;
}
public void eachSprocRow(字符串查询、列表参数、闭包){
sql.cacheConnection{Connection con->
CallableStatement proc=con.prepareCall(查询)
试一试{
parameters.eachWithIndex{param,i->
过程设置对象(i+1,参数)
}
布尔结果=proc.execute()
布尔值=false
而(!found){
如果(结果){
ResultSet rs=proc.getResultSet()
ResultSetMetaData md=rs.getMetaData()
int columnCount=md.getColumnCount()
while(rs.next()){
//不区分大小写的映射
映射行=新树映射(字符串。不区分大小写\u顺序)
对于(int i=0;i
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()
            }
        }
    }
}