簡體   English   中英

Apache DbUtils:處理從存儲過程返回的多個結果集

[英]Apache DbUtils : Handling multiple result sets returned from Stored Procedure

使用DbUtils從SQL Server中的存儲過程檢索結果時遇到問題。

對於特定的輸入值,在SQL Server Management Studio中執行時,存儲過程將返回兩個單獨的結果集,但對於其他值,則僅返回一個結果集。 下圖說明了此問題:

返回一個結果集: 1個表格數據的結果

返回了兩個結果集: 2個表格數據的結果

我在這里面臨的問題是我正在使用DbUtils BeanListHandler將結果轉換為UserInfo bean的列表。

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

當存儲過程僅返回一個結果集時,它工作正常。 但是,在返回兩個結果集的情況下,它僅給出第一個結果集的列表。

我認為通過使用JDBC我們可以使用多個ResultSet但是我不確定如何處理此DbUtils。

有人可以提供見解嗎? 如果需要任何其他信息,請更新我將提供的信息。

老實說,如果該存儲過程一次執行返回2個結果集,那么您將遇到更大的問題。 理想情況下,您希望將2個結果作為單表結果從SP中返回,然后就可以了。

1)嘗試聯系有權使用SP的人員,並引起他們的注意。 讓他們創建一個臨時表來存儲返回的2個結果中的所有記錄,然后只返回該臨時表中的所有內容。

2)如果沒有該選項,則可以嘗試本文中概述的過程,如果無法進行任何移動, 則從具有多個結果集的存儲過程中檢索數據以獲得結果從1開始

HTH

戴夫

子類化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;
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM