在Java中调用Oracle存储过程的总结

今天在工作中又用到了存储过程,很多东西,都生疏了,花了点时间才写出来

,这里就记下来,以免下次再用时,又要到网上到处去找资料。
首先是在Oracle中写存储过程,今天就碰到这样一个问题:
在写一个update语句时,还是用+去连接两个字段值,如:
update core_department set full_title=rootDeptName+'-'+vparentdeptname

+'-'+vcurrentdeptname,
    unicode = to_char(maxUniCode, 'FM999990999999999'),
    Priority = maxPriority,dept_alias_id=rootAliasId+'.'+maxDeptId
    where dept_id = maxDeptId;
这个时候如果你的存储过程其它地方写的没有错误时,当创建这个存储过程时,它不会

报错,但是到运行这个过程时就会老是执行失败,报出的原因也不明确。这里得把+ 换

成|| 来连接才OK:
update core_department set full_title=rootDeptName||'-'||

vparentdeptname||'-'||vcurrentdeptname,
    unicode = to_char(maxUniCode, 'FM999990999999999'),
    Priority = maxPriority,dept_alias_id=rootAliasId||'.'||maxDeptId
    where dept_id = maxDeptId;

这里,今天还学会了一个新的东西,那就是用plsql developer来调试我们的存储过程,

具体做法是:
在plsql developer的左边窗口,点击要调试的存储过程,右键选择TEST,再在右键菜单

中选择Add debug information.在右边窗口的下边有输入参数的选项,如果有参数就输

入相应的值,再点/按start debugger(F9)开始我们的测试,Run(Ctrl+N)  随时在

varible List中输入我们想查看的变量 其它:  Step into(Ctrl+N):单步调试,进入

下一步  Step over(Ctrl+O):结束该语句  Step out(Ctrl+T):跳出该过程  Run to

next exception:进入下一个断点Breakpoint  Toggle Breakpoint设置断点:光标指

向一行,Ctrl+B;或鼠标点中该行的行号边缘
这样基本就可以进行调试了。
这里贴出我自己写的一个存储过程:
create or replace procedure add_core_department_pro(
      vpid in core_department.dept_id%type,
      vparentdeptname in core_department.dept_name%type,
      vcurrentdeptname in core_department.dept_name%type,
      vrecode out varchar2,
      vremsg out varchar2

)as

--rootDeptName 根机构的部门名称
    rootDeptName core_department.dept_name%type;
    maxUniCode core_department.unicode%type;
    maxPriority core_department.priority%type;
    maxDeptId core_department.dept_id%type;
    rootAliasId core_department.dept_alias_id%type;
begin
    --取得当前插入的数据的部门id
    select max(dept_id) into maxDeptId from core_department;
    --取得上级部门中最大的唯一标识符并加1
    SELECT MAX(UNICODE)+1 into maxUniCode FROM CORE_DEPARTMENT WHERE DEPT_PARENT=vpid;
    --取得上级部门的dept_alias_id值
    select dept_alias_id into rootAliasId from core_department where dept_id = vpid;
    --取得上级部门的最大排序号并加1
    select max(Priority)+1 into maxPriority from core_department where dept_parent = vpid;
    --取得当前部门所在的最上级机构的名称
    select dept_name into rootDeptName from core_department where dept_alias_id = substr((rootAliasId),1,7);
    --修改刚刚插入数据的部分字段值
    update core_department set full_title=rootDeptName||'-'||vparentdeptname||'-'||vcurrentdeptname,
    unicode = to_char(maxUniCode, 'FM999990999999999'),
    Priority = maxPriority,dept_alias_id=rootAliasId||'.'||maxDeptId
    where dept_id = maxDeptId;
    vrecode :=0;
    vremsg:='执行成功';
    commit;
    EXCEPTION
    WHEN OTHERS THEN
        vrecode :=-1;
        vremsg:='执行失败';
        dbms_output.put_line(SQLERRM);
        rollback;
end;


完了之后就是Java对这个过程的调用:
Connection con = null;
CallableStatement  call = null;
con = getSession().connection();
call = con.prepareCall("{ call add_core_department_pro(?,?,?,?,?) }");
call.setLong(1, pid);
call.setString(2, parentDeptName);
call.setString(3, currentDeptName);
call.registerOutParameter(4, Types.VARCHAR);
call.registerOutParameter(5, Types.VARCHAR);
call.execute();
  String returnCode = call.getString(4);
这里为call对象设置参数时必须从1开始,而不是。当要设置输出参数时要用call.registerOutParameter方法
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值