java - Apache DbUtils : Handling multiple result sets returned from Stored Procedure

标签 java sql-server stored-procedures jdbc apache-commons-dbutils

我在使用 DbUtils 从 SQL Server 中的存储过程检索结果时遇到问题。

在 SQL Server Management Studio 中执行时,存储过程在针对特定输入值执行时返回两个单独的结果集,但对于其他值,它仅返回一个结果集。下图说明了该问题:

返回一个结果集: Results with 1 Table Data

返回两个结果集: Result with 2 Table Data

我在这里面临的问题是我使用 DbUtils BeanListHandler 将结果转换为 UserInfo beans 列表。

List<UserInfo> userList = (List<UserInfo>) run.query(STORED_PROC, new BeanListHandler(UserInfo.class), refId);

当存储过程仅返回一个结果集时,它工作正常。 但是,在返回两个结果集的情况下,仅给出第一个结果集的列表。

我认为通过使用 JDBC,我们可以使用多个 ResultSet,但我不确定如何处理这个 DbUtils。

有人可以提供见解吗?如果需要任何其他信息,请更新我,我将提供。

最佳答案

QueryRunner 对象进行子类化,然后调整适当的 query 方法来处理多个结果集是非常简单的。通过以下代码,我能够使用

检索UserInfo对象的完整列表
ResultSetHandler<List<UserInfo>> h = new BeanListHandler<UserInfo>(UserInfo.class);
MyQueryRunner run = new MyQueryRunner(ds);
String sql = 
        "EXEC dbo.Gain_Web_GetCompanyRepByIndRefID @RefID=?";
List<UserInfo> result = run.query(sql, h, 2);

其中 MyQueryRunner 位于

package com.example.so36623732;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

public class MyQueryRunner extends QueryRunner {

    public MyQueryRunner(DataSource ds) {
        super(ds);
    }

    /**
     * Executes the given SELECT or EXEC SQL query and returns a result object.
     * The <code>Connection</code> is retrieved from the
     * <code>DataSource</code> set in the constructor.
     * @param <T> The type of object that the handler returns
     * @param sql The SQL statement to execute.
     * @param rsh The handler used to create the result object from
     * the <code>ResultSet</code>.
     * @param params Initialize the PreparedStatement's IN parameters with
     * this array.
     * @return An object generated by the handler.
     * @throws SQLException if a database access error occurs
     */
    public <T> T query(String sql, ResultSetHandler<T> rsh, Object... params) throws SQLException {
        Connection conn = this.prepareConnection();

        return this.<T>query(conn, true, sql, rsh, params);
    }

    /**
     * Calls query after checking the parameters to ensure nothing is null.
     * @param conn The connection to use for the query call.
     * @param closeConn True if the connection should be closed, false otherwise.
     * @param sql The SQL statement to execute.
     * @param params An array of query replacement parameters.  Each row in
     * this array is one set of batch replacement values.
     * @return The results of the query.
     * @throws SQLException If there are database or parameter errors.
     */
    @SuppressWarnings("unchecked")
    private <T> T query(Connection conn, boolean closeConn, String sql, ResultSetHandler<T> rsh, Object... params)
            throws SQLException {
        if (conn == null) {
            throw new SQLException("Null connection");
        }

        if (sql == null) {
            if (closeConn) {
                close(conn);
            }
            throw new SQLException("Null SQL statement");
        }

        if (rsh == null) {
            if (closeConn) {
                close(conn);
            }
            throw new SQLException("Null ResultSetHandler");
        }

        PreparedStatement stmt = null;
        ResultSet rs = null;
        T result = null;
        List<T> allResults = null;

        try {
            stmt = this.prepareStatement(conn, sql);
            this.fillStatement(stmt, params);
            rs = this.wrap(stmt.executeQuery());
            allResults = (List<T>)rsh.handle(rs);
            while (stmt.getMoreResults()) {
                rs = stmt.getResultSet();
                result = rsh.handle(rs);
                allResults.addAll((List<T>)result);
            }

        } catch (SQLException e) {
            this.rethrow(e, sql, params);

        } finally {
            try {
                close(rs);
            } finally {
                close(stmt);
                if (closeConn) {
                    close(conn);
                }
            }
        }

        return (T) allResults;
    }

}

关于java - Apache DbUtils : Handling multiple result sets returned from Stored Procedure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36623732/

相关文章:

Java:从过程中读取元数据

sql-server - 在多个服务器上的多个数据库上执行存储过程 SQL Server

java - 在 Java WebBrowser 控件中显示 PDF 时如何隐藏 Adob​​e Reader 工具栏?

java - 将图像从java服务器发送到android

java - 按字母顺序从数组中插入排序字符串

sql-server - 如何在 SQL Server 列中存储超过 varchar(max) 的非常大的数据?

面向 Clojure 用户的 Java

sql - XML 到 SQL Server 中的几个具有相同名称的元素到不同的列中

SQL Server 检查大小写敏感吗?

sql-server - SQL 性能 : Recompile versus drop and re-apply for stored procedure