通过bboss persistent提供的callable调用接口,可以非常方便地实现数据库存储过程的调用,本博客之前介绍了通过位置变量绑定的方法设置过程参数的方法,本文介绍通过命名变量绑定参数的方法调用存储过程。
bboss persistent 下载地址:https://sourceforge.net/project/showfiles.php?group_id=238653&package_id=302766&release_id=647144
- java代码段如下:
import com.frameworkset.common.poolman.CallableDBUtil;
。。。。
CallableDBUtil callableDBUtil = new CallableDBUtil();
try
{
callableDBUtil.prepareCallable("{call test_p(?,?,?,?)}");
//不允许的操作: Ordinal binding and Named binding cannot be combined!
callableDBUtil.setInt("id", 10);
callableDBUtil.registerOutParameter("name", java.sql.Types.VARCHAR);
callableDBUtil.registerOutParameter("name1", java.sql.Types.VARCHAR);
callableDBUtil.registerOutParameter("test", java.sql.Types.INTEGER);
callableDBUtil.executeCallable();
System.out.println("name1:" + callableDBUtil.getString("name"));
System.out.println("name2:" + callableDBUtil.getString("name1"));
System.out.println("test:" + callableDBUtil.getInt("test"));
}
catch(Exception e)
{
e.printStackTrace();
}
- 存储过程定义如下:
CREATE OR REPLACE PROCEDURE test_p(id in number,
name out varchar2
,name1 out varchar2,test out number) IS
BEGIN
--tmpVar := 0;
name := 'hello name';
name1 := 'hello name1';
test := id;
insert into test(id,name) values(SEQ_TEST.nextval,'name1');
commit;
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
WHEN OTHERS THEN
-- Consider logging the error and then re-raise
RAISE;
END test_p;