设为首页 加入收藏

TOP

关于存储过程和函数异常出现和Java捕获简单处理方案
2014-11-24 00:14:59 来源: 作者: 【 】 浏览:4
Tags:关于 存储 过程 函数 异常 出现 Java 捕获 简单 处理 方案

我的做法


1.函数写法如下

create or replace function GET_PERFORMANCE_COEFFICIENT(PERFORMANCE_SCORE number) return
number is --获取绩效系数
coefficient number(10,2);
maxScore number(10,2);
begin
select max(hign_score) into maxScore from PERFORMANCE_RULE;
select p.coefficient into coefficient from PERFORMANCE_RULE p
where
(PERFORMANCE_SCORE=p.lower_score)
or (PERFORMANCE_SCORE = maxScore and p.hign_score = maxScore)
;
return coefficient;
exception
when no_data_found then
RAISE_APPLICATION_ERROR(-20002, '未找到绩效系数');
end;


上面的做法就可以让我们的JAVA程序捕获到异常了


2.JAVA端

throws SQLException
public void executeSpGenerateSalary(int year,int month,Double holidayBase) throws SQLException {
String sql="{ CALL sp_generate_salary( , , ) }";
Connection conn = getSession().connection();
CallableStatement cs = conn.prepareCall(sql);
cs.setInt(1, year);
cs.setInt(2, month);
cs.setDouble(3, holidayBase);
cs.execute();
cs.close();
}



然后在controller捕获

@RequestMapping("/generateSalary")
@ResponseBody
public Map generateSalary(SalaryEntity salaryEntity){

Map mapResult = new HashMap();
try{
salaryService.generateSalary(salaryEntity);
mapResult.put("result", true);
}
catch(java.sql.SQLException e){
mapResult.put("result", false);
mapResult.put("message", e.getMessage());
e.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
mapResult.put("result", false);
mapResult.put("message", e.getMessage());
}
return mapResult;
}


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Linux下C如何调用动态库 下一篇Java异常与事物回滚探究

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: