关于存储过程和函数异常出现和JAVA捕获简单处理方案
2014-03-26 02:21
661 查看
1.想法 在调用存储过程时候,如果出现出错,应该在页面弹出异常提示信息,快速定位错误原因,
也应该在存储过程和函数过程中尽可能的捕获概率发生较大的异常情况,并且在客户端页面通知(如果发生的话)
首先百度了,借鉴了下面这种方案
自定义oracle抛出异常解决
Caused by: java.sql.SQLException: ORA-02291: 违反完整约束条件 (xxx.FK_yyyy) - 未找到父项关键字 www.2cto.com
如何自定义oracle 抛出的异常?
使用RAISE_APPLICATION_ERROR 函数
该函数是将应用程序专有的错误从服务器端转达到客户端应用程序(其他机器上的SQLPLUS或者前台开发语言)
如何使用
RAISE_APPLICATION_ERROR( error_num IN NUMBER, error_msg IN VARCHAR2);
error_num :错误码,-20000到-20999 之间,这样就不会与ORACLE已有的的错误代码发生冲突。
error_msg :错误信息, 的长度不能超过 2k,否则截取 2k。
我的做法
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.hign_score and 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<String,Object> generateSalary(SalaryEntity salaryEntity){
Map<String,Object> mapResult = new HashMap<String,Object>();
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;
}
也应该在存储过程和函数过程中尽可能的捕获概率发生较大的异常情况,并且在客户端页面通知(如果发生的话)
首先百度了,借鉴了下面这种方案
自定义oracle抛出异常解决
Caused by: java.sql.SQLException: ORA-02291: 违反完整约束条件 (xxx.FK_yyyy) - 未找到父项关键字 www.2cto.com
如何自定义oracle 抛出的异常?
使用RAISE_APPLICATION_ERROR 函数
该函数是将应用程序专有的错误从服务器端转达到客户端应用程序(其他机器上的SQLPLUS或者前台开发语言)
如何使用
RAISE_APPLICATION_ERROR( error_num IN NUMBER, error_msg IN VARCHAR2);
error_num :错误码,-20000到-20999 之间,这样就不会与ORACLE已有的的错误代码发生冲突。
error_msg :错误信息, 的长度不能超过 2k,否则截取 2k。
我的做法
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.hign_score and 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<String,Object> generateSalary(SalaryEntity salaryEntity){
Map<String,Object> mapResult = new HashMap<String,Object>();
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;
}
相关文章推荐
- 存储过程中异常捕获处理
- 关于存储过程中一个参数表示多个查询条件使用方法的简单处理
- 无法捕获异常:Java关于在catch中抛出一个异常给外围函数却捕获不到该异常的思考
- 关于MySQL存储过程异常处理的一点心得
- Java学习过程中出现的异常问题与处理(最近更新20170915)
- 用代码简单介绍java中的异常的捕获及处理
- Sql Server存储过程之异常的简单处理
- 关于MAPGIS-IMS java工程出图时候出现COperResult : COperResult异常的处理
- 关于SQLSERVER 事物的运用(3)嵌套事物以及嵌套存储过程的异常的处理
- OracleSQL 中的异常处理、存储过程、函数和触发器
- 存储过程 务的概念 事务的特性 关于异常的处理 连接池 构JdbcUtil类
- 关于Map表循环过程删除操作出现的java.util.ConcurrentModificationException 异常
- Oracle,SqlServer,DB2存储过程中异常处理
- Java ibatis调用存储过程出现阻塞
- 关于java中判断为空出现空指针异常的问题
- oracle函数和存储过程简单实例
- 一道关于java异常处理的题目
- 存储过程中的简单事务处理---SQLServer2005系列
- java调用存储过程和函数
- db2 存储过程异常处理