美文网首页
使用JdbcTemplate调用SqlServer存储过程返回结

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

作者: JerryNing | 来源:发表于2018-08-16 04:09 被阅读0次

    代码示例:

        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

    相关文章

      网友评论

          本文标题:使用JdbcTemplate调用SqlServer存储过程返回结

          本文链接:https://www.haomeiwen.com/subject/txzgbftx.html