1. 程式人生 > >java呼叫儲存過程 返回結果集

java呼叫儲存過程 返回結果集

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呼叫儲存過程 返回結果

Java程式碼呼叫儲存過程:public Map<String, Object> rankInfo(Map<String, Object> rankMap,String start,String end, String userId,String o

JAVA呼叫儲存過程--返回結果(傳入基本型別引數,返回基本型別和結果

1  建立儲存過程 create or replace procedure examplepro( inparm1 in varchar2,inparm2 in number,outparm1 out number,result out type_cursor)  ...

關於oracle呼叫儲存過程返回結果

這兩天因為公司要求,去學習了下以前一直模模糊糊的儲存過程,記錄下關於返回結果集的問題。 要返回結果集,首先你必須有個遊標,它是記錄你的查詢集的。而定義一個遊標有好多方法: 1,你可以在包中定義。然後將遊標變數作為儲存過程引數的型別。 CREATE OR REPLACE P

[Oracle]高效的PL/SQL程式設計(五)--呼叫儲存過程返回結果

            Oracle.DataAccess.Client.OracleConnection oracleConnection1=new OracleConnection("data source=precolm2;user id=colmtest;password=colmtest");   

MyBatis呼叫儲存過程返回結果

儲存過程: create or replace procedure get_result_by_sql(p_sql in varchar2,p_result out sys_refcursor,p_msg out varchar2) is begin

C#呼叫ORACLE儲存過程返回結果

Oracle中scott使用者下建立儲存過程: (注:從9i開始有了sys_refcursor這種型別,在以前的Oracle版本中需要使用REF CURSOR,並且還需放在一個程式包中) create or replace procedure sp_getdept (result

python 呼叫mysql儲存過程返回結果

儲存過程: delimiter | create procedure get_product_info(in imid int(10),int iuser varchar(20)) begin select * from tb_test where mid = i

mybatis呼叫Oracle儲存過程返回結果

在開發中,有時需要關聯幾張表來進行一些複雜的計算,此時可採用建立一張臨時表,將每次的資料計算後先存入臨時表,然後通過Oracle的遊標返回。 在mybatis中的呼叫如下: //呼叫語法格式,需要構造一個resultMap,用來接收返回的結果集 <select id

mybatis呼叫mysql儲存過程返回結果

儲存過程中經常需要返回結果集。Mysql中直接用select即可返回結果集。而oracle則需要使用遊標來返回結果集。這一點Mysql相對比較方便,如下程式碼即可實現輸出結果集: 儲存過程定義: D

SSM-Mybatis呼叫Oracle儲存過程返回結果(遊標)示例

1.建立一個包 此處建立一個包,是為了建立儲存過程時,用遊標作為out輸出引數時宣告為遊標型別用的. --建立一個包 create or replace package types as type empListCursor is ref cursor

oracle 儲存過程返回結果 (轉載)

好久沒上來了, 難道今天工作時間稍有空閒, 研究了一下oracle儲存過程返回結果集.        配合oracle臨時表, 使用儲存過程來返回結果集的資料讀取方式可以解決海量資料表與其他表的連線問題. 在儲存過程中先根據過濾條件從海量資料表中選出符合條件的記錄並存放到臨

Oracle的儲存過程返回結果

Oracle儲存過程: CREATE OR REPLACE PROCEDURE getcity ( citycode IN VARCHAR2, ref_cursor O

oracle 儲存過程返回 結果 table形式

--sys_refcursor 和 cursor 優缺點比較優點比較優點一:sys_refcursor,可以在儲存過程中作為引數返回一個table格式的結構集(我把他認為是table型別,容易理解,其實是一個遊標集), cursor 只能用在儲存過程,函式,包等的實現體中,不

javaEE--ibatis--Sping+flex4 呼叫儲存過程返回List結果

1. 這段時間在看儲存過程的優劣性並嘗試提高程式的執行效能, 儲存過程能夠: a:降低網路流量 b:執行計劃,儲存過程在首次執行是將產生一個執行計劃。 c:使用儲存過程能夠增強對執行計劃的重複使用,從而也可以通過使用遠端過程呼叫RPC處理伺服器上的儲存過程而提高效能,

Mysql儲存過程——多結果返回java獲取

delimiter $$ CREATE PROCEDURE demoSp(IN inputParam VARCHAR(255), INOUT inOutParam varchar(255)) BEGIN     SELECT CONCAT('zyxw---', inputParam) into inOu

Java獲取儲存過程返回的多個結果

第一步:寫你的儲存過程  delimiter //  create procedure test_proc ()  begin      select * from test_table1 where id=1;      select * from test_table

儲存過程呼叫 遍歷返回結果

//java呼叫儲存過程 使用遊標遍歷結果集 public void getCallableStatement(){ CallableStatement cs=null; Connection

java獲取儲存過程返回結果(多個結果)

在.net中,DataSet可以自動接收多個結果集,形成DataTable陣列。 使用JAVA時,也想要實現這個效果,百度一頓查詢,終於找到解決的辦法。 核心方法:CallableStatement     ResultSet 具體實現程式碼: import java.s

java呼叫儲存過程無法取得返回引數

環境:資料庫sql server2005,jdk1.6 ,myeclipse,驅動jdts1.2.2 執行以下程式碼,報錯: String querySQL = "{?=call p_sys_manager_csReport(?,?,?,?,?)}"; cstmt = con

ibatis呼叫儲存過程:返回NUMBER,對應java型別的解決辦法

我想有很多朋友會像我一樣遇到這樣的問題 首先,我們建立一個儲存過程 create or replace procedure pro_app_test(        p_userid in number,        p_pid out number,        p_