java oracl 儲存過程的使用
1、oracle plsql 建立儲存過程:例
CREATE OR REPLACE PROCEDURE PR_CHECK_DETAIL_IMPNEW(
-- 輸出資料遊標
ODS_CURSOR out SYS_REFCURSOR,
-- 申請單號
var_app_no in varchar2 := NULL,
-- 批覆單號
var_ver_no in varchar2 := NULL,
-- 商品number
var_material_number in varchar2 := NULL,
-- 商品code
var_material_code in varchar2 := NULL,
-- 商品名稱
var_material_name in varchar2 := NULL,
-- 企業ID
var_enterprise_id in varchar2 := NULL,
--國家ID
var_country_id in varchar2 := NULL,
-- 報關口岸
var_custom_id in varchar2 := NULL,
-- 用途
var_use_name in varchar2 := NULL,
-- 申請單狀態
var_app_status in varchar2 := NULL,
-- 配額年度
var_qta_year in varchar2 := NULL,
-- 初審人
var_first_account in varchar2 := NULL,
-- 複審人
var_second_account in varchar2 := NULL,
-- 公示人
var_board_account in varchar2 := NULL,
-- 終審人
var_third_account in varchar2 := NULL,
-- 建立時間-開始
var_create_start_time in varchar2 := NULL,
-- 建立時間-結束
var_create_end_time in varchar2 := NULL,
-- 批准時間-開始
var_app_start_time in varchar2 := NULL,
-- 批准時間-結束
var_app_end_time in varchar2 := NULL,
-- 清關時間-開始
var_cus_start_time in varchar2 := NULL,
-- 清關時間-結束
var_cus_end_time in varchar2 := NULL,
-- 起始頁
var_page_start in number := 0,
-- 每頁記錄數
var_page_count in number := 10,
-- 連線型別
join_type in varchar2 := NULL
) AS
/*
業務核查下:顯示詳細資料
*/
var_sql varchar2(30000);
var_sql_header varchar2(2000);
var_sql_tail varchar2(2000);
var_sql_ods varchar2(2000);
var_sql_lic varchar2(2000);
var_sql_order varchar2(1000);
var_sql_join varchar2(1000);
-- 申請單號查詢sql
var_sql_app_no varchar2(1000);
var_sql_app_no_null varchar2(1000);
-- 批覆單號查詢sql
var_sql_ver_no varchar2(1000);
var_sql_ver_no_null varchar2(1000);
-- 商品ID查詢sql
var_sql_material varchar2(2000);
var_sql_material_null varchar2(2000);
-- 商品code查詢sql
var_sql_material_code varchar2(2000);
var_sql_material_code_null varchar2(2000);
-- 商品name查詢sql
var_sql_material_name varchar2(2000);
var_sql_material_name_null varchar2(2000);
-- 企業查詢sql
var_sql_enterprise varchar2(2000);
var_sql_enterprise_null varchar2(2000);
-- 國家查詢sql
var_sql_country varchar2(2000);
var_sql_country_null varchar2(2000);
-- 報關口岸查詢sql
var_sql_custom varchar2(2000);
var_sql_custom_null varchar2(2000);
-- 用途查詢sql
var_sql_use_name varchar2(2000);
var_sql_use_name_null varchar2(2000);
-- 申請單狀態查詢sql
var_sql_app_status varchar2(2000);
var_sql_app_status_null varchar2(2000);
-- 配額年度
var_sql_qta_year varchar2(1000);
var_sql_qta_year_null varchar2(1000);
-- 初審人查詢sql
var_sql_first_account varchar2(2000);
var_sql_first_account_null varchar2(2000);
-- 複審人查詢sql
var_sql_second_account varchar2(2000);
var_sql_second_account_null varchar2(2000);
-- 公示人查詢sql
var_sql_board_account varchar2(2000);
var_sql_board_account_null varchar2(2000);
-- 終審人查詢sql
var_sql_third_account varchar2(2000);
var_sql_third_account_null varchar2(2000);
-- 建立時間
var_sql_create_time varchar2(2000);
var_sql_create_time_null varchar2(2000);
-- 批准時間
var_sql_app_time varchar2(2000);
var_sql_app_time_null varchar2(2000);
-- 清關時間
var_sql_cus_time varchar2(2000);
var_sql_cus_time_null varchar2(2000);
/*-------------------------------------------------------------------------*/
-- 許可證號狀態: AA為有效證
var_lic_status varchar2(10) := 'AA';
var_join_type varchar2(10);
BEGIN
var_sql_order := ' order by a.app_no desc ';
var_sql_tail := ' on a.ver_no = b.doc_nub group by ' ||
' a.app_no, a.ver_no, a.material_name, a.material_code, ' ||
' a.imp_amt, a.country_name, ' ||
' a.state, a.app_create_time ' ;
-- 返回資料列
var_sql_header := 'select a.app_no, ' ||
' a.ver_no, ' ||
' a.material_name, ' ||
' a.material_code, ' ||
' round(a.imp_amt / 1000, 4) appAmt , ' ||
' a.country_name, ' ||
' a.state, ' ||
' a.app_create_time, ' ||
' sum(b.licSum) licSum, ' ||
' round(sum(b.top_qty / 1000), 4) topAmt, ' ||
' round(sum(b.cus_qty / 1000), 4) clearAmt ' ||
' from ';
-- 連線型別
var_join_type := join_type;
IF var_join_type IS NULL THEN
var_join_type := 'left';
END IF;
var_sql_join := ' ' || var_join_type || ' join ';
/*-------------------------------------條件查詢sql-----------------------------------------*/
-- 建立時間條件查詢sql
var_sql_create_time := ' and substr(a.app_create_time, 1, 8) >= :var_create_start_time ' ||
' and substr(a.app_create_time, 1, 8) <= :var_create_end_time ';
var_sql_create_time_null := ' and (:var_create_start_time is null or 1 = 1) ' ||
' and (:var_create_end_time is null or 1 = 1) ';
-- 批准時間條件查詢sql
var_sql_app_time := ' and substr(a.app_time, 1, 8) >= :var_app_start_time ' ||
' and substr(a.app_time, 1, 8) <= :var_app_end_time ';
var_sql_app_time_null := ' and (:var_app_start_time is null or 1 = 1) ' ||
' and (:var_app_end_time is null or 1 = 1) ';
-- 清關時間條件查詢sql
var_sql_cus_time := ' and a.snd_dte is not null and substr(a.snd_dte, 1, 8) >= :var_cus_start_time ' ||
' and substr(a.snd_dte, 1, 8) <= :var_cus_end_time ';
var_sql_cus_time_null := ' and (:var_cus_start_time is null or 1 = 1) ' ||
' and (:var_cus_end_time is null or 1 = 1) ';
-- 商品number條件查詢sql
var_sql_material := ' and cd.dic_number in ' ||
' (select t.dic_number from ods_dic_material t where t.dic_code in ' ||
' (select distinct dic_code from ods_dic_material where dic_number = :var_material_number )) ';
var_sql_material_null := ' and (:var_material_number is null or 1 = 1) ';
-- 商品code條件查詢sql
var_sql_material_code := ' and cd.dic_code like :var_material_code ';
var_sql_material_code_null := ' and (:var_material_code is null or 1 = 1) ';
-- 商品name條件查詢sql
var_sql_material_name := ' and cd.dic_name like :var_material_name ';
var_sql_material_name_null := ' and (:var_material_name is null or 1 = 1) ';
-- 申請單號條件查詢sql
var_sql_app_no := ' and a.app_no like :var_app_no ';
var_sql_app_no_null := ' and (:var_app_no is null or 1 = 1) ';
-- 批覆單號條件查詢sql
var_sql_ver_no := ' and a.ver_no like :var_ver_no ';
var_sql_ver_no_null := ' and (:var_ver_no is null or 1 = 1) ';
-- 企業條件查詢sql
var_sql_enterprise := ' and a.enterprise_id = :var_enterprise_id ';
var_sql_enterprise_null := ' and (:var_enterprise_id is null or 1 = 1) ';
-- 國家條件查詢sql
var_sql_country := ' and a.country_id = :var_country_id ';
var_sql_country_null := ' and (:var_country_id is null or 1 = 1) ';
-- 報關口岸查詢sql
var_sql_custom := ' and a.custom_id = :var_custom_id ';
var_sql_custom_null := ' and (:var_custom_id is null or 1 = 1) ';
-- 申請單狀態查詢sql
var_sql_app_status := ' and w.state = :var_app_status ';
var_sql_app_status_null := ' and (:var_app_status is null or 1 = 1) ';
-- 配額年度查詢sql
var_sql_qta_year := ' and a.qta_year = :var_qta_year ';
var_sql_qta_year_null := ' and (:var_qta_year is null or 1 = 1) ';
-- 用途查詢sql
var_sql_use_name := ' and a.userplace = :var_use_name ';
var_sql_use_name_null := ' and (:var_use_name is null or 1 = 1) ';
-- 初審人查詢sql
var_sql_first_account := ' and info.fir_ods_account = :var_first_account ';
var_sql_first_account_null := ' and (:var_first_account is null or 1 = 1) ';
-- 複審人查詢sql
var_sql_second_account := ' and info.sec_ods_account = :var_second_account ';
var_sql_second_account_null := ' and (:var_second_account is null or 1 = 1) ';
-- 公示人查詢sql
var_sql_board_account := ' and info.fou_ods_account = :var_board_account ';
var_sql_board_account_null := ' and (:var_board_account is null or 1 = 1) ';
-- 終審人查詢sql
var_sql_third_account := ' and info.thi_ods_account = :var_third_account ';
var_sql_third_account_null := ' and (:var_third_account is null or 1 = 1) ';
/*------------------------------------ODS業務sql------------------------------------------*/
-- ODS資料部分
var_sql_ods := ' (select * ' ||
' from (select t.app_create_time, t.app_no, t.ver_no, t.imp_amt, ' ||
' t.state, t.country_name, t.material_name, t.material_code ' ||
' from (select a.imp_amt, a.app_create_time, a.app_no, a.ver_no, w.state, ' ||
' cny.dic_cn_name country_name, cd.dic_name material_name, ' ||
' cd.dic_code material_code ' ||
' from ods_app_imp a, ods_dic_material cd, ods_app_imp_info info ' ||
' , ods_dic_country cny, ods_workflow w ' ||
' where a.material_id = cd.dic_id ' ||
' and a.app_no = w.app_no ' ||
' and a.app_no = info.app_no and a.country_id = cny.dic_id ';
/*------------------------------------條件查詢sql------------------------------------------*/
-- 商品number查詢條件
IF var_material_number IS NOT NULL THEN
var_sql_ods := var_sql_ods || var_sql_material;
ELSE
var_sql_ods := var_sql_ods || var_sql_material_null;
END IF;
-- 商品code查詢條件
IF var_material_code IS NOT NULL THEN
var_sql_ods := var_sql_ods || var_sql_material_code;
ELSE
var_sql_ods := var_sql_ods || var_sql_material_code_null;
END IF;
-- 商品name查詢條件
IF var_material_name IS NOT NULL THEN
var_sql_ods := var_sql_ods || var_sql_material_name;
ELSE
var_sql_ods := var_sql_ods || var_sql_material_name_null;
END IF;
-- 申請單號查詢條件
IF var_app_no IS NOT NULL THEN
var_sql_ods := var_sql_ods || var_sql_app_no;
ELSE
var_sql_ods := var_sql_ods || var_sql_app_no_null;
END IF;
-- 批覆單號查詢條件
IF var_ver_no IS NOT NULL THEN
var_sql_ods := var_sql_ods || var_sql_ver_no;
ELSE
var_sql_ods := var_sql_ods || var_sql_ver_no_null;
END IF;
-- 企業查詢條件
IF var_enterprise_id IS NOT NULL THEN
var_sql_ods := var_sql_ods || var_sql_enterprise;
ELSE
var_sql_ods := var_sql_ods || var_sql_enterprise_null;
END IF;
-- 國家查詢條件
IF var_country_id IS NOT NULL THEN
var_sql_ods := var_sql_ods || var_sql_country;
ELSE
var_sql_ods := var_sql_ods || var_sql_country_null;
END IF;
-- 報關口岸查詢條件
IF var_custom_id IS NOT NULL THEN
var_sql_ods := var_sql_ods || var_sql_custom;
ELSE
var_sql_ods := var_sql_ods || var_sql_custom_null;
END IF;
-- 申請單狀態查詢條件
IF var_app_status IS NOT NULL THEN
var_sql_ods := var_sql_ods || var_sql_app_status;
ELSE
var_sql_ods := var_sql_ods || var_sql_app_status_null;
END IF;
-- 配額年度查詢條件
IF var_qta_year IS NOT NULL THEN
var_sql_ods := var_sql_ods || var_sql_qta_year;
ELSE
var_sql_ods := var_sql_ods || var_sql_qta_year_null;
END IF;
-- 用途查詢條件
IF var_use_name IS NOT NULL THEN
var_sql_ods := var_sql_ods || var_sql_use_name;
ELSE
var_sql_ods := var_sql_ods || var_sql_use_name_null;
END IF;
-- 初審人查詢條件
IF var_first_account IS NOT NULL THEN
var_sql_ods := var_sql_ods || var_sql_first_account;
ELSE
var_sql_ods := var_sql_ods || var_sql_first_account_null;
END IF;
-- 複審人查詢條件
IF var_second_account IS NOT NULL THEN
var_sql_ods := var_sql_ods || var_sql_second_account;
ELSE
var_sql_ods := var_sql_ods || var_sql_second_account_null;
END IF;
-- 公示人查詢條件
IF var_board_account IS NOT NULL THEN
var_sql_ods := var_sql_ods || var_sql_board_account;
ELSE
var_sql_ods := var_sql_ods || var_sql_board_account_null;
END IF;
-- 終審人查詢條件
IF var_third_account IS NOT NULL THEN
var_sql_ods := var_sql_ods || var_sql_third_account;
ELSE
var_sql_ods := var_sql_ods || var_sql_third_account_null;
END IF;
-- 建立時間查詢條件
IF var_create_start_time IS NOT NULL and var_create_end_time IS NOT NULL THEN
var_sql_ods := var_sql_ods || var_sql_create_time;
ELSE
var_sql_ods := var_sql_ods || var_sql_create_time_null;
END IF;
-- 審批時間查詢條件
IF var_app_start_time IS NOT NULL and var_app_end_time IS NOT NULL THEN
var_sql_ods := var_sql_ods || var_sql_app_time;
ELSE
var_sql_ods := var_sql_ods || var_sql_app_time_null;
END IF;
var_sql_ods := var_sql_ods ||
' ) t ) t where t.imp_amt is not null) a ';
/*------------------------------------許可證局業務sql------------------------------------------*/
-- 許可證局資料部分
var_sql_lic := ' (select * ' ||
' from (select sum(t.top_qty) top_qty, sum(t.cus_qty) cus_qty, count(1) licSum, t.doc_nub ' ||
' from (select a.top_qty, a.cus_qty, a.doc_nub ' ||
' from lic_dtls_import a where a.stau = :var_lic_status ';
-- 清關時間查詢條件
IF var_cus_start_time IS NOT NULL and var_cus_end_time IS NOT NULL THEN
var_sql_lic := var_sql_lic || var_sql_cus_time;
ELSE
var_sql_lic := var_sql_lic || var_sql_cus_time_null;
END IF;
var_sql_lic := var_sql_lic || ' ) t group by t.doc_nub) t) b ';
/*------------------------------------合併sql------------------------------------------*/
var_sql := var_sql_header || var_sql_ods || var_sql_join || var_sql_lic ||
var_sql_tail || var_sql_order;
-- 分頁
var_sql := 'select * from(select t.*,rownum rn from(select * from ( ' || var_sql ||
')) t where rownum <= :var_page_count + :var_page_start) where rn > :var_page_start';
OPEN ODS_CURSOR FOR var_sql
USING var_material_number, '%'|| var_material_code||'%', '%'|| var_material_name||'%'
, '%'|| var_app_no||'%' ,'%'|| var_ver_no||'%'
, var_enterprise_id, var_country_id, var_custom_id
, var_app_status, var_qta_year
, var_use_name, var_first_account, var_second_account, var_board_account
, var_third_account
, var_create_start_time, var_create_end_time, var_app_start_time, var_app_end_time
, var_lic_status, var_cus_start_time, var_cus_end_time
, var_page_count, var_page_start, var_page_start;
END PR_CHECK_DETAIL_IMPNEW;
建立完成重新整理一下即可
2.Java呼叫儲存過程
PageUtil<ImpCompanyQueryModel> page = new PageUtil<ImpCompanyQueryModel>();
page.setPageNo(Integer.parseInt(vo.getPageNo()));
List<ImpCompanyQueryModel> list = new ArrayList<ImpCompanyQueryModel>();
DecimalFormat df = new DecimalFormat("#0.0000");
CallableStatement cstmt = null;
ResultSet rs = null;
try {
cstmt = conn
.prepareCall("{call PR_CHECK_DETAIL_IMPNEW(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}");
// 這裡的返回的是一個結果集,在在過程中是遊標,所以註冊型別為 OracleTypes.CURSOR
cstmt.registerOutParameter(1, OracleTypes.CURSOR);
cstmt.setString(2, vo.getAppNo());
cstmt.setString(3, vo.getVerNo());
cstmt.setString(4, vo.getMaterialNumber());
cstmt.setString(5, vo.getMaterialCode());
cstmt.setString(6, vo.getMaterialName());
cstmt.setString(7, vo.getEnterpriseId());
cstmt.setString(8, vo.getCountryId());
cstmt.setString(9, vo.getCustomId());
cstmt.setString(10, null);
cstmt.setString(11, vo.getStatus());
cstmt.setString(12, null);
cstmt.setString(13, null);
cstmt.setString(14, null);
cstmt.setString(15, null);
cstmt.setString(16, null);
cstmt.setString(17, vo.getStartCreateDate());
cstmt.setString(18, vo.getEndCreateDate());
cstmt.setString(19, vo.getStartApplyDate());
cstmt.setString(20, vo.getEndApplyDate());
cstmt.setString(21, vo.getStartClearDate());
cstmt.setString(22, vo.getEndClearDate());
cstmt.setInt(23, page.getStartCount());
cstmt.setLong(24, page.getPagecount());
cstmt.setString(25, vo.getJoinType());
// 執行
cstmt.executeQuery();
// 獲得結果集,強制轉換
rs = (ResultSet) cstmt.getObject(1);
// 和以前一樣遍歷結果集
while (rs.next()) {
ImpCompanyQueryModel model = new ImpCompanyQueryModel();
model.setAppNo(rs.getString("app_no"));
model.setVerNo(rs.getString("ver_no"));
model.setMaterialName(rs.getString("material_name"));
model.setMaterialCode(rs.getString("material_code"));
model.setApplyAmount(df.format(rs.getDouble("appAmt")));
model.setCountryName(rs.getString("country_name"));
model.setStatus(rs.getString("state"));
Map<String, Object> map=new HashMap<String, Object>();
if("1".equals(rs.getString("state"))){
map.put("1", "待上報");
}else if("2".equals(rs.getString("state"))){
map.put("2", "待初審");
}else if("6".equals(rs.getString("state"))){
map.put("6", "初審駁回");
}else if("10".equals(rs.getString("state"))){
map.put("10", "待公示");
}else if("3".equals(rs.getString("state"))){
map.put("3", "待複審");
}else if("7".equals(rs.getString("state"))){
map.put("7", "複審駁回");
}else if("8".equals(rs.getString("state"))){
map.put("8", "待終審");
}else if("16".equals(rs.getString("state"))){
map.put("16", "終審駁回");
}else if("14".equals(rs.getString("state"))){
map.put("14", "帶蓋章");
}else {
map.put("17", "蓋章通過");
}
model.setStateName(map);
String createTime = rs.getString("app_create_time");
if (StringUtils.isNotBlank(createTime)) {
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = sf.parse(createTime);
sf = new SimpleDateFormat("yyyy-MM-dd");
model.setApplyCreateDate(sf.format(date));
} else {
model.setApplyCreateDate("");
}
String sum = rs.getString("licSum");
if (StringUtils.isNotBlank(sum)) {
model.setLicSum(sum);
} else {
model.setLicSum("0");
}
model.setTopAmount(df.format(rs.getDouble("topAmt")));
model.setClearAmount(df.format(rs.getDouble("clearAmt")));
// 審批清關率
if (rs.getFloat("appAmt") != 0) {
double rate = (rs.getFloat("clearAmt") / rs
.getFloat("appAmt")) * 100;
model.setApplyRate(CalculateUtil.round(rate, "#0.00"));
} else {
model.setApplyRate("0.00");
}
// 證面清關率
if (rs.getFloat("appAmt") != 0) {
double rate = (rs.getFloat("topAmt") / rs
.getFloat("appAmt")) * 100;
model.setLicRate(CalculateUtil.round(rate, "#0.00"));
} else {
model.setLicRate("0.00");
}
list.add(model);
}
} finally {
DataBaseRelease.release(cstmt, rs);
}
page.setResult(list);
return page;