java JdbcTemplate 多个结果集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16149672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:03:21  来源:igfitidea点击:

JdbcTemplate multiple result sets

javajdbcspring-jdbc

提问by The Cat

I am trying to find an easy way to deal with Stored Procedures / SQL returning multiple result sets. I have been using the SimpleJdbcOperations#queryForList()method however this will only return the first result set as a List<Map<String, Object>>. I need to be able to get multiple result sets, ideally as a Collectionof List<Map<String, Object>>or something. The program I am writing is a middleware component so I don't know what the SQL will be, or the form of the result set.

我试图找到一种简单的方法来处理返回多个结果集的存储过程/SQL。我一直在使用该SimpleJdbcOperations#queryForList()方法,但是这只会将第一个结果集返回为List<Map<String, Object>>. 我需要能够得到多个结果集,理想状态为CollectionList<Map<String, Object>>什么的。我正在编写的程序是一个中间件组件,所以我不知道 SQL 将是什么,或者结果集的形式。

I think I have to use the JdbcOperationsclass which gives me access to more methods, including execute(CallableStatementCreator csc, CallableStatementCallback<T> action)but now I am stuck.

我想我必须使用JdbcOperations该类,它使我可以访问更多方法,包括 execute(CallableStatementCreator csc, CallableStatementCallback<T> action)但现在我被卡住了。

    CallableStatementCallback<T> callback = new CallableStatementCallback<T>() {
       @Override
       public T doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException
       {
           boolean results = cs.execute(request);
           while(results)
           {
               ResultSet result = cs.getResultSet();
               results = cs.getMoreResults();
           }
           return null;
        }
};

I am not really sure how to use the method though, or what to do with the ResultSetto get my generic List<Map<String, Object>>s.

我不太确定如何使用该方法,或者如何处理ResultSet以获得我的通用List<Map<String, Object>>s。

采纳答案by The Cat

I managed to get a Set<ResultSet>using this code,

我设法Set<ResultSet>使用此代码,

private Set<ResultSet> executeProcedure(final String sql)
{
    return jdbc.execute(new CallableStatementCreator() {
        @Override
        public CallableStatement createCallableStatement(Connection con) throws SQLException
        {
            return con.prepareCall(sql);
        }
    }, new CallableStatementCallback<Set<ResultSet>>() {
        @Override
        public Set<ResultSet> doInCallableStatement(CallableStatement cs) throws SQLException
        {
            Set<ResultSet> results = new HashSet<>();

            boolean resultsAvailable = cs.execute();

            while (resultsAvailable)
            {
                results.add(cs.getResultSet());
                resultsAvailable = cs.getMoreResults();
            }
            return results;
        }
    });
}

Just going to look at translating a ResultSetinto List<Map<String, Object>>.

只是打算看看将 a 翻译ResultSetList<Map<String, Object>>.

回答by dannrob

You can use the resultSet.getMetaData() method to work out what columns are in the data:

您可以使用 resultSet.getMetaData() 方法计算出数据中的列:

ResultSetMetaData meta = resultSet.getMetaData();
int colcount = meta.getColumnCount();
for (int i = 1; i <= colcount; i++) 
{
    String name = meta.getColumnLabel(i); // This is the name of the column
    int type = meta.getColumnType(i);     // from java.sql.Types
   // Maybe add to a Map,List, etc...
}

You can then do as the other commentors have mentioned do a loop through the ResultSet pulling out the data you need:

然后,您可以像其他评论者提到的那样通过 ResultSet 进行循环,以提取您需要的数据:

while (resultSet.hasNext())
{
     resultSet.next();
     // Find the columns you want to extract (via the above method maybe) and add to your row.
}

回答by TaherT

I have used below method to get List of ResultSet in form of List<Map<String, Object>>

我使用下面的方法以以下形式获取结果集列表 List<Map<String, Object>>

public List<List<Map<String, Object>>> executeProcedure(final String sql) {
        return jdbcTemplate.execute(new CallableStatementCreator() {
            @Override
            public CallableStatement createCallableStatement(Connection con) throws SQLException {
                return con.prepareCall(sql);
            }
        }, new CallableStatementCallback<List<List<Map<String, Object>>>>() {
            @Override
            public List<List<Map<String, Object>>> doInCallableStatement(CallableStatement cs) throws SQLException {
                boolean resultsAvailable = cs.execute();
                List<List<Map<String, Object>>> list = new ArrayList<List<Map<String, Object>>>();
                while (resultsAvailable) {
                    ResultSet resultSet = cs.getResultSet();
                    List<Map<String, Object>> subList = new ArrayList<Map<String, Object>>();
                    while (resultSet.next()) {
                        ResultSetMetaData meta = resultSet.getMetaData();
                        int colcount = meta.getColumnCount();
                        Map<String, Object> map = new HashMap<String, Object>();
                        for (int i = 1; i <= colcount; i++) {
                            String name = meta.getColumnLabel(i);
                            map.put(name, resultSet.getString(i));
                        }
                        subList.add(map);
                    }
                    list.add(subList);
                    resultsAvailable = cs.getMoreResults();
                }
                return list;
            }
        });
    }