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

java调用存储过程 返回结果集

2017-03-28 18:26 399 查看
Java代码调用存储过程:
public Map<String, Object> rankInfo(Map<String, Object> rankMap,String start,String end, String userId,String officeId, String rankType,String timeType){
try {
//排名前十的用户答题详情
List<RecodeRank> topTenUsers = new ArrayList<RecodeRank>();
//当前用户在部门、当前用户在单位、当前用户在系统、用户部门在系统、用户单位在系统答题总数、答题正确数
Connection conn = GetDBConnection.getDbConnection();//获取数据库连接
try {
CallableStatement cs = conn.prepareCall("{call getUserTrain(?,?,?,?)}");//调用存储过程
//1-4为存储过程中输入参数赋值
cs.setString(1, start);
cs.setString(2, end);
cs.setString(3, officeId);
cs.setString(4, rankType);
boolean rs = cs.execute();//执行存储过程,执行过程中创建表,将汇总数据存放在表中
try{
//从存储过程创建的表中取出统计结果,利用java代码取出前十名和当前用户的排名信息(此处省略) 
............
 rankMap.put("topTenUsers", topTenUsers);
}finally{
cs.close();
}
}finally {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return rankMap;
}

存储过程:

CREATE DEFINER=`root`@`localhost` PROCEDURE `getUserTrain`(IN `startDate` varchar(64), IN `endDate` varchar(64), IN `officeId` varchar(64), IN `rankType` varchar(6))
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT '前台培训能力评估正确率统计'

BEGIN 

DECLARE realName varchar(24);/*姓名*/

DECLARE officeName varchar(100);/*所属单位*/

DECLARE totalAnswer int;/*答题总数*/

DECLARE rightNum int;/*正确数量*/

DECLARE stuId varchar(64);/*学员ID*/

DECLARE done int DEFAULT FALSE;/*遍历游标结束标记*/

/*定义游标*/

DECLARE one_in_dept CURSOR FOR SELECT id,real_name FROM et_student WHERE del_flag='0' and deptment=officeId;

DECLARE one_in_unit CURSOR FOR SELECT id,real_name FROM et_student WHERE del_flag='0' and office_id=officeId;

DECLARE one_in_sys CURSOR FOR SELECT id,real_name FROM et_student WHERE del_flag='0';

DECLARE dept_in_sys CURSOR FOR SELECT id,name FROM et_stu_office WHERE type='2';

DECLARE unit_in_sys CURSOR FOR SELECT id,name FROM et_stu_office WHERE type='1';

/*将游标结束标记与游标绑定*/

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = true;

/*删除临时表*/

/*delete from temp_train_capacity;*/

DROP TABLE IF EXISTS temp_train_capacity;

/*创建临时表*/

CREATE TABLE  IF NOT EXISTS temp_train_capacity(

   stuId_td VARCHAR(64),
realName_td VARCHAR(32),
officeName_td VARCHAR(64),
totalAnswer_td INT,
rightNum_td INT

);   

 

   IF rankType = '1'

  THEN

     /*传入参数机构ID不为空*/
 IF officeId IS NOT NULL THEN SET officeName = (SELECT name FROM et_stu_office f WHERE f.id = officeId AND f.del_flag='0');
  OPEN one_in_dept;/*打开游标*/
  read_loop:LOOP/*开始循环游标*/
  FETCH one_in_dept INTO stuId,realName;/*
从游标中一次提取一条记录*/
  IF done THEN LEAVE read_loop;/**/
  END IF;
/*答题总数*/  

  select count(1) into totalAnswer from et_train_recode_detail d where d.recode_id in(
select t.id from et_train_recode t where t.del_flag='0' and t.user_id = stuId and t.train_type='1')
and date_format(d.create_date,'%Y-%m-%d %H:%m:%s') between date_format(startDate,'%Y-%m-%d %H:%m:%s') and date_format(endDate,'%Y-%m-%d %H:%m:%s');
    /*正确题目总数*/
  select count(1) into rightNum from et_train_recode_detail d where d.recode_id in(
select t.id from et_train_recode t where t.del_flag='0' and t.user_id = stuId and t.train_type='1') and d.if_get ='0'
and date_format(d.create_date,'%Y-%m-%d %H:%m:%s') between date_format(startDate,'%Y-%m-%d %H:%m:%s') and date_format(endDate,'%Y-%m-%d %H:%m:%s');
/*将数据保存至临时表*/
  INSERT INTO temp_train_capacity(stuId_td,realName_td,officeName_td,totalAnswer_td,rightNum_td)VALUES(stuId,realName,officeName,totalAnswer,rightNum);
END LOOP;/*结束游标循环*/
/*关闭游标*/
  CLOSE one_in_dept;
 END IF; 

   

 /*个人在单位排名*/

  ELSEIF rankType = '2'

  THEN

        /*传入参数机构ID不为空*/
IF officeId IS NOT NULL THEN SET officeName = (SELECT name FROM et_stu_office f WHERE f.id = officeId AND f.del_flag='0');
  OPEN one_in_unit;/*打开游标*/
  read_loop:LOOP/*开始循环游标*/
  FETCH one_in_unit INTO stuId,realName;/*从游标中一次提取一条记录*/
  IF done THEN LEAVE read_loop;/**/
  END IF;
 /*答题总数*/  

  select count(1) into totalAnswer from et_train_recode_detail d where d.recode_id in(
select t.id from et_train_recode t where t.del_flag='0' and t.user_id = stuId and t.train_type='1')
and date_format(d.create_date,'%Y-%m-%d %H:%m:%s') between date_format(startDate,'%Y-%m-%d %H:%m:%s') and date_format(endDate,'%Y-%m-%d %H:%m:%s');
    /*正确题目总数*/
  select count(1) into rightNum from et_train_recode_detail d where d.recode_id in(
select t.id from et_train_recode t where t.del_flag='0' and t.user_id = stuId and t.train_type='1') and d.if_get ='0'
and date_format(d.create_date,'%Y-%m-%d %H:%m:%s') between date_format(startDate,'%Y-%m-%d %H:%m:%s') and date_format(endDate,'%Y-%m-%d %H:%m:%s');
/*将数据保存至临时表*/
  INSERT INTO temp_train_capacity(stuId_td,realName_td,officeName_td,totalAnswer_td,rightNum_td)VALUES(stuId,realName,officeName,totalAnswer,rightNum);
END LOOP;/*结束游标循环*/
/*关闭游标*/
  CLOSE one_in_unit;
 END IF;

   ELSE OPEN unit_in_sys;
 read_loop:LOOP
 FETCH unit_in_sys INTO stuId,realName;
 IF done THEN LEAVE read_loop;
 END IF;
     SET officeName = realName;
     
     /*答题总数*/
      select count(1) into totalAnswer from et_train_recode_detail d where d.recode_id in(                                                                                   
       
select t.id from et_train_recode t where t.del_flag='0' and t.train_type='1' and t.user_id in( select id from et_student where del_flag='0' and office_id=stuId))
 and date_format(d.create_date,'%Y-%m-%d %H:%m:%s') between date_format(startDate,'%Y-%m-%d %H:%m:%s') and date_format(endDate,'%Y-%m-%d %H:%m:%s');
   
/*正确题目总数*/
      select count(1) into rightNum from et_train_recode_detail d where d.recode_id in(                                                                                                       
       
select t.id from et_train_recode t where t.del_flag='0' and t.train_type='1' and t.user_id in( select id from et_student where del_flag='0' and office_id=stuId) ) and d.if_get ='0'
 and date_format(d.create_date,'%Y-%m-%d %H:%m:%s') between date_format(startDate,'%Y-%m-%d %H:%m:%s') and date_format(endDate,'%Y-%m-%d %H:%m:%s');
       

      INSERT INTO temp_train_capacity(stuId_td,realName_td,officeName_td,totalAnswer_td,rightNum_td)VALUES(stuId,realName,officeName,totalAnswer,rightNum);
 END LOOP;
CLOSE unit_in_sys;

 

END IF; 

        select * from temp_train_capacity order by totalAnswer_td desc, rightNum_td desc;

END

--------------------------------------------------------------------------------------------------------------------------------------------

在存储过程中创建临时表,返回临时表数据时,在java代码中接收时发现无法接收结果集,额,经查资料说是5.0以下版本的mysql中存储过程不支持,才修改成了创建单独的表专门 用来存放存储过程中统计结果,如有看官知道如何返回最后的结果集(临时表中所有数据)请告知,谢谢!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  存储 java