MySQL數據庫中有兩個表,一個student,一個teacher
其中student表結構如下
teacher表如下
有存儲過程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即可