Java获取存储过程返回的多个结果集

Java获取存储过程返回的多个结果集

MySQL数据库中有两个表,香港服务器,一个student,香港虚拟主机,一个teacher

其中student表结构如下

teacher表如下

有存储过程checkAll

BEGINselect * from teacher;SELECT * FROM student;END

Java代码如下

Map<String,Object> getAll(){ 2Connection conn=null; 3CallableStatement cs=null; 4ResultSet rs=null; 5Map<String,Object> map=new HashMap<String, Object>(); 6Map<String,String> temp=null; 7List<Map<String,String>> list=null; 8try { 9conn=DBCon.getInstance();10cs=conn.prepareCall(“{call checkAll()}”);11 cs.execute();12rs=cs.getResultSet();13if(rs!=null){14list=new ArrayList<Map<String,String>>();15while(rs.next()){16temp=new HashMap<String, String>();17temp.put(“id”, rs.getInt(“id”)+””);18temp.put(“birthday”, rs.getDate(“birthday”)+””);19temp.put(“name”, rs.getString(“name”)+””);20temp.put(“title”, rs.getString(“title”)+””);21 list.add(temp);22 }23map.put(“teacher”, list);24if(cs.getMoreResults()){25rs=cs.getResultSet();26list=new ArrayList<Map<String,String>>();27while(rs.next()){28temp=new HashMap<String, String>();29temp.put(“id”, rs.getInt(“id”)+””);30temp.put(“name”, rs.getString(“name”));31temp.put(“age”, rs.getInt(“age”)+””);32 list.add(temp);33 }34map.put(“student”, list);35 }36 }37} catch (Exception e) {38 e.printStackTrace();39 }40finally{41 DbUtils.closeQuietly(conn, cs, rs);42 }43return map;44}

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

posted on

那风再温柔。太深的流连便成了一种羁绊,

Java获取存储过程返回的多个结果集

相关文章:

你感兴趣的文章:

标签云: