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中存储过程不支持,才修改成了创建单独的表专门 用来存放存储过程中统计结果,如有看官知道如何返回最后的结果集(临时表中所有数据)请告知,谢谢!!!
相关文章
- Entity Framework 调用返回标量值的存储过程
- asp + sqlserver 执行存储过程返回记录集报对象关闭时不允许操作
- [原创]java WEB学习笔记79:Hibernate学习之路--- 四种对象的状态,session核心方法:save()方法,persist()方法,get() 和 load() 方法,update()方法,saveOrUpdate() 方法,merge() 方法,delete() 方法,evict(),hibernate 调用存储过程,hibernate 与 触发器协同工作
- 《Entity Framework 6 Recipes》中文翻译系列 (14) -----第三章 查询之查询中设置默认值和存储过程返回多结果集
- TSQL:从另一个存储过程调用存储过程并读取结果
- Java+sql server+CallableStatement调用存储过程三种情况 (转)
- java 调用oracle的存储过程返回记录集
- java调用oracle存储过程返回多条结果集
- Java调用SQL Server存储过程
- Java调用Oracle存储过程返回结果集