您的位置:首页 > 编程语言 > Java开发

Java调用存储过程返回多个结果集

2015-09-11 21:31 786 查看
第一次写博客,最近遇到了Java调用存储过程,返回多个结果集的问题,下面是我在网上找的一些资料,然后自己写了一个main方法,拿出来与大家分享一下,希望大家多多指点......

数据表结构及数据

SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for teacher
-- ----------------------------
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher` (
`id` int(11) NOT NULL,
`birthday` date NOT NULL,
`name` varchar(45) DEFAULT NULL,
`title` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- ----------------------------
-- Records of teacher
-- ----------------------------
INSERT INTO `teacher` VALUES ('1', '2015-09-16', 'zzz', 'test');
INSERT INTO `teacher` VALUES ('2', '2015-09-13', 'ccc', 'dddd');
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL,
`name` varchar(45) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('1', 'tom', '15');
INSERT INTO `student` VALUES ('2', 'lucy', '14');
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for joke
-- ----------------------------
DROP TABLE IF EXISTS `joke`;
CREATE TABLE `joke` (
`joke_id` int(8) NOT NULL AUTO_INCREMENT COMMENT '笑话id',
`joke_content` text COMMENT '笑话内容',
PRIMARY KEY (`joke_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COMMENT='笑话表';

-- ----------------------------
-- Records of joke
-- ----------------------------
INSERT INTO `joke` VALUES ('1', '一日一醉汉酒后打车回家,伸手拦一辆110巡警车,并且嚷嚷道:就算你是每公里一块一,也没必要写那么大嘛!');
INSERT INTO `joke` VALUES ('2', '公共汽车上老太太怕坐过站,逢站必问。汽车到一站,她一个劲的用雨伞捅司机“这是展览中心吗?”“不是,这是排骨!”');
INSERT INTO `joke` VALUES ('3', '课堂上老师点名:“刘华!” 结果下面一孩子大声回到:“yeah!” 老师很生气:“为什么不说‘到’?” 孩子说:“那个字念‘烨’……”。');
INSERT INTO `joke` VALUES ('4', '昨天被公司美女同事莫名的亲了一口,心里各种的爽。后来才知道人家玩真心话大冒险,是叫亲一个公司最丑的,最丑的!');
INSERT INTO `joke` VALUES ('5', '有个人第一次在集市上卖冰棍,不好意思叫卖,旁边有一个人正高声喊:“卖冰棍”,他只好喊道:“我也是”。');


下面是源码:

public class Test {
public static final String url = "jdbc:mysql://127.0.0.1/test";
public static final String name = "com.mysql.jdbc.Driver";
public static final String user = "root";
public static final String password = "123456";

public static Connection conn = null;
public static PreparedStatement pst = null;

public static CallableStatement cs=null;
public static ResultSet rs=null;

public static void main(String[] args) {

Map<String,Object> map=new HashMap<String, Object>();
Map<String,String> temp=null;
List<Map<String,String>> list=null;

try {
Class.forName(name);
conn = DriverManager.getConnection(url, user, password);
cs = conn.prepareCall("{call checkAll()}");
cs.execute();
rs = cs.getResultSet();

if(rs!=null){
list = new ArrayList<Map<String,String>>();
while(rs.next()){
temp = new HashMap<String, String>();
temp.put("id", rs.getInt("id")+"");
temp.put("birthday", rs.getDate("birthday")+"");
temp.put("name", rs.getString("name")+"");
temp.put("title", rs.getString("title")+"");
list.add(temp);
}

map.put("teacher", list);

if(cs.getMoreResults()){//判断是否有多个结果集
rs = cs.getResultSet();
list = new ArrayList<Map<String,String>>();
while(rs.next()){
temp = new HashMap<String, String>();
temp.put("id", rs.getInt("id")+"");
temp.put("name", rs.getString("name"));
temp.put("age", rs.getInt("age")+"");
list.add(temp);
}
map.put("student", list);
}

if(cs.getMoreResults()){
rs = cs.getResultSet();
list = new ArrayList<Map<String,String>>();
while(rs.next()){
temp = new HashMap<String, String>();
temp.put("joke_id", rs.getInt("joke_id")+"");
temp.put("joke_content", rs.getString("joke_content"));
list.add(temp);
}
map.put("joke", list);
}

List<Map<String,String>> listStudent = (List<Map<String, String>>) map.get("student");

for (Map<String, String> mapStudnet : listStudent) {
for(Map.Entry<String , String> entry:mapStudnet.entrySet()){
System.out.println(entry.getKey()+":"+entry.getValue());
}
System.out.println("----------");
}

System.out.println("=======================================================");

List<Map<String,String>> listTeacher = (List<Map<String, String>>) map.get("teacher");
for (Map<String, String> mapTeacher : listTeacher) {
for(Map.Entry<String , String> entry:mapTeacher.entrySet()){
System.out.println(entry.getKey()+":"+entry.getValue());
}
System.out.println("----------");
}
System.out.println("=======================================================");

List<Map<String,String>> listJoke = (List<Map<String, String>>) map.get("joke");
for (Map<String, String> mapTeacher : listJoke) {
for(Map.Entry<String , String> entry:mapTeacher.entrySet()){
System.out.println(entry.getKey()+":"+entry.getValue());
}
System.out.println("----------");
}

}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
rs.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}


运行结果如下:

id:1
age:15
name:tom
----------
id:2
age:14
name:lucy
----------
=======================================================
id:1
birthday:2015-09-16
title:test
name:zzz
----------
id:2
birthday:2015-09-13
title:dddd
name:ccc
----------
=======================================================
joke_id:1
joke_content:一日一醉汉酒后打车回家,伸手拦一辆110巡警车,并且嚷嚷道:就算你是每公里一块一,也没必要写那么大嘛!
----------
joke_id:2
joke_content:公共汽车上老太太怕坐过站,逢站必问。汽车到一站,她一个劲的用雨伞捅司机“这是展览中心吗?”“不是,这是排骨!”
----------
joke_id:3
joke_content:课堂上老师点名:“刘华!” 结果下面一孩子大声回到:“yeah!” 老师很生气:“为什么不说‘到’?” 孩子说:“那个字念‘烨’……”。
----------
joke_id:4
joke_content:昨天被公司美女同事莫名的亲了一口,心里各种的爽。后来才知道人家玩真心话大冒险,是叫亲一个公司最丑的,最丑的!
----------
joke_id:5
joke_content:有个人第一次在集市上卖冰棍,不好意思叫卖,旁边有一个人正高声喊:“卖冰棍”,他只好喊道:“我也是”。
----------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: