JdbcTemplate multiple result sets

荒凉一梦 提交于 2019-12-05 06:53:21

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

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 ResultSet into List<Map<String, Object>>.

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

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:

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

I have used below method to get List of ResultSet in form of 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;
            }
        });
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!