您的位置:首页 > 其它

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

2014-12-19 12:36 489 查看
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即可
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: