存储过程返回多个结果集处理

来源:互联网 发布:苹果电脑系统备份软件 编辑:程序博客网 时间:2024/05/21 16:28
 1     public static Map<String,Object> getAll(){ 2         Connection conn=null; 3         CallableStatement cs=null; 4         ResultSet rs=null; 5         Map<String,Object> map=new HashMap<String, Object>(); 6         Map<String,String> temp=null; 7         List<Map<String,String>> list=null; 8         try { 9             conn=DBCon.getInstance();10             cs=conn.prepareCall("{call checkAll()}");11                 cs.execute();12                 rs=cs.getResultSet();13                 if(rs!=null){14                     list=new ArrayList<Map<String,String>>();15                     while(rs.next()){16                         temp=new HashMap<String, String>();17                         temp.put("id", rs.getInt("id")+"");18                         temp.put("birthday", rs.getDate("birthday")+"");19                         temp.put("name", rs.getString("name")+"");20                         temp.put("title", rs.getString("title")+"");21                         list.add(temp);22                     }23                     map.put("teacher", list);24                     if(cs.getMoreResults()){25                         rs=cs.getResultSet();26                         list=new ArrayList<Map<String,String>>();27                         while(rs.next()){28                             temp=new HashMap<String, String>();29                             temp.put("id", rs.getInt("id")+"");30                             temp.put("name", rs.getString("name"));31                             temp.put("age", rs.getInt("age")+"");32                             list.add(temp);33                         }34                         map.put("student", list);35                     }36                 }37         } catch (Exception e) {38             e.printStackTrace();39         }40         finally{41             DbUtils.closeQuietly(conn, cs, rs);42         }43         return map;44     }

若是数据库是SqlServer,存储过程涉及到表的更新(增、删、改)的话会出错,可以在存储过程里面加上一句:set nocount on即可

0 0