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中儲存過程不支援,才修改成了建立單獨的表專門 用來存放儲存過程中統計結果,如有看官知道如何返回最後的結果集(臨時表中所有資料)請告知,謝謝!!!