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

java

MySQL数据库中有两个表,一个student,一个teacher

其中student表结构如下

teacher表如下

存储过程" title="存储过程">存储过程checkAll

BEGIN

select * from teacher;

SELECT * FROM student;

END

Java代码如下

 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即可

以上是 Java获取存储过程返回的多个结果集 的全部内容, 来源链接: utcz.com/z/393018.html

回到顶部