1

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

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

返回一个结果集: 1 表数据的结果

返回两个结果集: 2 表数据的结果

我在这里面临的问题是我正在使用 DbUtilsBeanListHandler将结果转换为UserInfobean 列表。

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

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

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

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

4

2 回答 2

1

老实说,如果该存储过程在一次执行中返回 2 个结果集,那么您就会遇到更大的问题。理想情况下,您希望从 SP 将 2 个结果作为单个表结果返回,然后您应该没问题。

1) 尝试联系有权访问 SP 的人,并让他们注意您的案件。让他们创建一个临时表来存储返回的 2 个结果中的所有记录,然后只返回该临时表中的所有内容。

2)如果您没有该选项,您可以尝试本文中概述的过程retrieve-data-from-stored-procedure-which-has-multiple-result-sets如果您无法获得任何移动结果从 1)

高温高压

戴夫

于 2016-04-15T14:20:36.703 回答
1

对对象进行子类化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;
    }

}
于 2016-05-19T21:12:20.740 回答