JdbcTemplate多个结果集

时间:2013-04-22 14:30:35

标签: java jdbc spring-jdbc

我试图找到一种简单的方法来处理Stored Procedures / SQL返回多个结果集。我一直在使用SimpleJdbcOperations#queryForList()方法,但这只会将第一个结果集作为List<Map<String, Object>>返回。我需要能够获得多个结果集,理想情况下是Collection List<Map<String, Object>>或其他。我正在编写的程序是一个中间件组件,所以我不知道SQL将是什么,或结果集的形式。

我想我必须使用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;
        }
};

我不确定如何使用该方法,或者如何使用ResultSet来获取我的通用List<Map<String, Object>>

4 个答案:

答案 0 :(得分:2)

我设法使用此代码获得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;
        }
    });
}

试着将ResultSet翻译成List<Map<String, Object>>

答案 1 :(得分:1)

您可以使用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...
}

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

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

答案 2 :(得分:0)

我使用下面的方法以List<Map<String, Object>>

的形式获取ResultSet的List
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;
            }
        });
    }

答案 3 :(得分:0)

在大多数情况下,此代码可能更易于使用:

Map<String,Object> resultSets = new JdbcTemplate(dataSource)
            .call(con -> con.prepareCall(query), new ArrayList<>());