使用JdbcTemplate调用SqlServer存储过程返回结

2018-08-16  本文已影响0人  JerryNing

代码示例:

    public List<Map<String, Object>> getGoodShedule(String storeId, String code) throws SQLException {
        return (List<Map<String, Object>>) jdbcTemplate.execute(new CallableStatementCreator() {
            @Override
            public CallableStatement createCallableStatement(Connection con) throws SQLException {
                String storedProc = "{call up_get_gds_schedule (?, ?)}";
                CallableStatement cs = con.prepareCall(storedProc);
                cs.setString(1, code);
                cs.setString(2, storeId);
                return cs;
            }
        }, new CallableStatementCallback() {
            public Object doInCallableStatement(CallableStatement cs) throws SQLException {
                List<Map<String, Object>> list = new ArrayList<>();
                ResultSet rs = cs.executeQuery();
                ResultSetMetaData rmd = rs.getMetaData();
                int columnCount = rmd.getColumnCount();
                System.out.println("columnCount: " + columnCount);
                while (rs.next()) {
                    Map<String, Object> rowMap = new HashMap<>(columnCount);
                    for (int i = 1; i <= columnCount; i++) {
                        rowMap.put(rmd.getColumnName(i), rs.getObject(i));
                    }

                    list.add(rowMap);
                }
                rs.close();
                return list;

            }
        });
    }

出现下面异常信息:
com.microsoft.sqlserver.jdbc.SQLServerException: 该语句没有返回结果集
解决方案:
增加下面一行在存储过程
SET NOCOUNT ON

在Sql Server Management Studio修改存储过程,在查询窗口,点击执行即可。


image.png
上一篇下一篇

猜你喜欢

热点阅读