- 浏览: 107670 次
- 性别:
- 来自: 上海
-
文章分类
最新评论
-
zhuchao_ko:
...
Java Web 的 Security Constraint 配置 -
fsweicaixia:
java.lang.IllegalArgumentExcept ...
Exception in thread "main" java.lang.IllegalArgumentException: attempt to create -
fsweicaixia:
...
Exception in thread "main" java.lang.IllegalArgumentException: attempt to create -
Mr.Sun:
...
流程监控(去除节假日和双休日) -
hailang163:
不错,在理啊!为了这个回复,答了一堆问题!
本地缓存->静态页面
3.4 select into不可乎视的问题
我们知道在pl/sql中要想从数据表中向变量赋值,需要使用select into 子句。
但是它会带动来一些问题,如果查询没有记录时,会抛出no_data_found异常。
如果有多条记录时,会抛出too_many_rows异常。
这个是比较糟糕的。一旦抛出了异常,就会让过程中断。特别是no_data_found这种异常,没有严重到要让程序中断的地步,可以完全交给由程序进行处理。
- create or replace procedure procexception(p varchar2)
- as
- v_postype varchar2(20);
- begin
- select pos_type into v_postype from pos_type_tbl where 1=0;
- dbms_output.put_line(v_postype);
- end;
create or replace procedure procexception(p varchar2) as v_postype varchar2(20); begin select pos_type into v_postype from pos_type_tbl where 1=0; dbms_output.put_line(v_postype); end;
执行这个过程
- SQL> exec procexception('a');
- 报错
- ORA-01403: no data found
- ORA-06512: at "LIFEMAN.PROCEXCEPTION", line 6
- ORA-06512: at line 1
SQL> exec procexception('a'); 报错 ORA-01403: no data found ORA-06512: at "LIFEMAN.PROCEXCEPTION", line 6 ORA-06512: at line 1
处理这个有三个办法
1. 直接加上异常处理。
- create or replace procedure procexception(p varchar2)
- as
- v_postype varchar2(20);
- begin
- select pos_type into v_postype from pos_type_tbl where 1=0;
- dbms_output.put_line(v_postype);
- exception
- when no_data_found then
- dbms_output.put_line('没找到数据');
- end;
create or replace procedure procexception(p varchar2) as v_postype varchar2(20); begin select pos_type into v_postype from pos_type_tbl where 1=0; dbms_output.put_line(v_postype); exception when no_data_found then dbms_output.put_line('没找到数据'); end;
这样做换汤不换药,程序仍然被中断。可能这样不是我们所想要的。
2. select into做为一个独立的块,在这个块中进行异常处理
- create or replace procedure procexception(p varchar2)
- as
- v_postype varchar2(20);
- begin
- begin
- select pos_type into v_postype from pos_type_tbl where 1=0;
- dbms_output.put_line(v_postype);
- exception
- when no_data_found then
- v_postype := '';
- end;
- dbms_output.put_line(v_postype);
- end;
create or replace procedure procexception(p varchar2) as v_postype varchar2(20); begin begin select pos_type into v_postype from pos_type_tbl where 1=0; dbms_output.put_line(v_postype); exception when no_data_found then v_postype := ''; end; dbms_output.put_line(v_postype); end;
这是一种比较好的处理方式了。不会因为这个异常而引起程序中断。
3.使用游标
- create or replace procedure procexception(p varchar2)
- as
- v_postype varchar2(20);
- cursor c_postype is select pos_type from pos_type_tbl where 1=0;
- begin
- open c_postype;
- fetch c_postype into v_postype;
- close c_postype;
- dbms_output.put_line(v_postype);
- end;
create or replace procedure procexception(p varchar2) as v_postype varchar2(20); cursor c_postype is select pos_type from pos_type_tbl where 1=0; begin open c_postype; fetch c_postype into v_postype; close c_postype; dbms_output.put_line(v_postype); end;
这样就完全的避免了no_data_found异常。完全交由程序员来进行控制了。
第二种情况是too_many_rows 异常的问题。
Too_many_rows 这个问题比起no_data_found要复杂一些。
给一个变量赋值时,但是查询结果有多个记录。
处理这种问题也有两种情况:
1. 多条数据是可以接受的,也就是说从结果集中随便取一个值就行。这种情况应该很极端了吧,如果出现这种情况,也说明了程序的严谨性存在问题。
2. 多条数据是不可以被接受的,在这种情况肯定是程序的逻辑出了问题,也说是说原来根本就不会想到它会产生多条记录。
对于第一种情况,就必须采用游标来处理,而对于第二种情况就必须使用内部块来处理,重新抛出异常。
多条数据可以接受,随便取一条,这个跟no_data_found的处理方式一样,使用游标。
我这里仅说第二种情况,不可接受多条数据,但是不要忘了处理no_data_found哦。这就不能使用游标了,必须使用内部块。
- create or replace procedure procexception2(p varchar2)
- as
- v_postype varchar2(20);
- begin
- begin
- select pos_type into v_postype from pos_type_tbl where rownum < 5;
- exception
- when no_data_found then
- v_postype :=null;
- when too_many_rows then
- raise_application_error(-20000,'对v_postype赋值时,找到多条数据');
- end;
- dbms_output.put_line(v_postype);
- end;
create or replace procedure procexception2(p varchar2) as v_postype varchar2(20); begin begin select pos_type into v_postype from pos_type_tbl where rownum < 5; exception when no_data_found then v_postype :=null; when too_many_rows then raise_application_error(-20000,'对v_postype赋值时,找到多条数据'); end; dbms_output.put_line(v_postype); end;
需要注意的是一定要加上对no_data_found的处理,对出现多条记录的情况则继续抛出异常,让上一层来处理。
总之对于select into的语句需要注意这两种情况了。需要妥当处理啊。
3.5 在存储过程中返回结果集
我们使用存储过程都是返回值都是单一的,有时我们需要从过程中返回一个集合。即多条数据。这有几种解决方案。比较简单的做法是写临时表,但是这种做法不灵活。而且维护麻烦。我们可以使用嵌套表来实现.没有一个集合类型能够与java的jdbc类型匹配。这就是对象与关系数据库的阻抗吧。数据库的对象并不能够完全转换为编程语言的对象,还必须使用关系数据库的处理方式。
- create or replace package procpkg is
- type refcursor is ref cursor;
- procedure procrefcursor(p varchar2, p_ref_postypeList out refcursor);
- end procpkg;
- create or replace package body procpkg is
- procedure procrefcursor(p varchar2, p_ref_postypeList out refcursor)
- is
- v_posTypeList PosTypeTable;
- begin
- v_posTypeList :=PosTypeTable();--初始化嵌套表
- v_posTypeList.extend;
- v_posTypeList(1) := PosType('A001','客户资料变更');
- v_posTypeList.extend;
- v_posTypeList(2) := PosType('A002','团体资料变更');
- v_posTypeList.extend;
- v_posTypeList(3) := PosType('A003','受益人变更');
- v_posTypeList.extend;
- v_posTypeList(4) := PosType('A004','续期交费方式变更');
- open p_ref_postypeList for select * from table(cast (v_posTypeList as PosTypeTable));
- end;
- end procpkg;
create or replace package procpkg is type refcursor is ref cursor; procedure procrefcursor(p varchar2, p_ref_postypeList out refcursor); end procpkg; create or replace package body procpkg is procedure procrefcursor(p varchar2, p_ref_postypeList out refcursor) is v_posTypeList PosTypeTable; begin v_posTypeList :=PosTypeTable();--初始化嵌套表 v_posTypeList.extend; v_posTypeList(1) := PosType('A001','客户资料变更'); v_posTypeList.extend; v_posTypeList(2) := PosType('A002','团体资料变更'); v_posTypeList.extend; v_posTypeList(3) := PosType('A003','受益人变更'); v_posTypeList.extend; v_posTypeList(4) := PosType('A004','续期交费方式变更'); open p_ref_postypeList for select * from table(cast (v_posTypeList as PosTypeTable)); end; end procpkg;
在包头中定义了一个游标变量,并把它作为存储过程的参数类型。
在存储过程中定义了一个嵌套表变量,对数据写进嵌套表中,然后把嵌套表进行类型转换为table,游标变量从这个嵌套表中进行查询。外部程序调用这个游标。
所以这个过程需要定义两个类型。
create or replace type PosType as Object ( posType varchar2(20), description varchar2(50) );
create or replace type PosTypeTable is table of PosType;
需要注意,这两个类型不能定义在包头中,必须单独定义,这样java层才能使用。
在外部通过pl/sql来调用这个过程非常简单。
- set serveroutput on;
- declare
- type refcursor is ref cursor;
- v_ref_postype refcursor;
- v_postype varchar2(20);
- v_desc varchar2(50);
- begin
- procpkg.procrefcursor('a',v_ref_postype);
- loop
- fetch v_ref_postype into v_postype,v_desc;
- exit when v_ref_postype%notfound;
- dbms_output.put_line('posType:'|| v_postype || ';description:' || v_desc);
- end loop;
- end;
set serveroutput on; declare type refcursor is ref cursor; v_ref_postype refcursor; v_postype varchar2(20); v_desc varchar2(50); begin procpkg.procrefcursor('a',v_ref_postype); loop fetch v_ref_postype into v_postype,v_desc; exit when v_ref_postype%notfound; dbms_output.put_line('posType:'|| v_postype || ';description:' || v_desc); end loop; end;
注意:对于游标变量,不能使用for循环来处理。因为for循环会隐式的执行open动作。而通过open for来打开的游标%isopen是为true的。也就是默认打开的。Open一个已经open的游标是错误的。所以不能使用for循环来处理游标变量。
我们主要讨论的是如何通过jdbc调用来处理这个输出参数。
- conn = this.getDataSource().getConnection();
- CallableStatement call = conn.prepareCall("{call procpkg.procrefcursor(?,?)}");
- call.setString(1, null);
- call.registerOutParameter(2, OracleTypes.CURSOR);
- call.execute();
- ResultSet rsResult = (ResultSet) call.getObject(2);
- while (rsResult.next()) {
- String posType = rsResult.getString("posType");
- String description = rsResult.getString("description");
- ......
- }
conn = this.getDataSource().getConnection(); CallableStatement call = conn.prepareCall("{call procpkg.procrefcursor(?,?)}"); call.setString(1, null); call.registerOutParameter(2, OracleTypes.CURSOR); call.execute(); ResultSet rsResult = (ResultSet) call.getObject(2); while (rsResult.next()) { String posType = rsResult.getString("posType"); String description = rsResult.getString("description"); ...... }
这就是jdbc的处理方法。
Ibatis处理方法:
1.参数配置
- <parameterMap id="PosTypeMAP" class="java.util.Map">
- <parameter property="p" jdbcType="VARCHAR" javaType="java.lang.String" />
- <parameter property="p_ref_postypeList" jdbcType="ORACLECURSOR" javaType="java.sql.ResultSet" mode="OUT" typeHandler="com.palic.elis.pos.dayprocset.integration.dao.impl.CursorHandlerCallBack" />
- </parameterMap>
- 2.调用过程
- <procedure id ="procrefcursor" parameterMap ="PosTypeMAP">
- {call procpkg.procrefcursor(?,?)}
- </procedure>
- 3.定义自己的处理器
- public class CursorHandlerCallBack implements TypeHandler{
- public Object getResult(CallableStatement cs, int index) throws SQLException {
- ResultSet rs = (ResultSet)cs.getObject(index);
- List result = new ArrayList();
- while(rs.next()) {
- String postype =rs.getString(1);
- String description = rs.getString(2);
- CodeTableItemDTO posTypeItem = new CodeTableItemDTO();
- posTypeItem.setCode(postype);
- posTypeItem.setDescription(description);
- result.add(posTypeItem);
- }
- return result;
- }
- 4. dao方法
- public List procPostype() {
- String p = "";
- Map para = new HashMap();
- para.put("p",p);
- para.put("p_ref_postypeList",null);
- this.getSqlMapClientTemplate().queryForList("pos_dayprocset.procrefcursor", para);
- return (List)para.get("p_ref_postypeList");
- }
<parameterMap id="PosTypeMAP" class="java.util.Map"> <parameter property="p" jdbcType="VARCHAR" javaType="java.lang.String" /> <parameter property="p_ref_postypeList" jdbcType="ORACLECURSOR" javaType="java.sql.ResultSet" mode="OUT" typeHandler="com.palic.elis.pos.dayprocset.integration.dao.impl.CursorHandlerCallBack" /> </parameterMap> 2.调用过程 <procedure id ="procrefcursor" parameterMap ="PosTypeMAP"> {call procpkg.procrefcursor(?,?)} </procedure> 3.定义自己的处理器 public class CursorHandlerCallBack implements TypeHandler{ public Object getResult(CallableStatement cs, int index) throws SQLException { ResultSet rs = (ResultSet)cs.getObject(index); List result = new ArrayList(); while(rs.next()) { String postype =rs.getString(1); String description = rs.getString(2); CodeTableItemDTO posTypeItem = new CodeTableItemDTO(); posTypeItem.setCode(postype); posTypeItem.setDescription(description); result.add(posTypeItem); } return result; } 4. dao方法 public List procPostype() { String p = ""; Map para = new HashMap(); para.put("p",p); para.put("p_ref_postypeList",null); this.getSqlMapClientTemplate().queryForList("pos_dayprocset.procrefcursor", para); return (List)para.get("p_ref_postypeList"); }
这个跟jdbc的方式非常的相似.
我们使用的是ibatis的2.0版本,比较麻烦。
如果是使用2.2以上版本就非常简单的。
因为可以在parameterMap中定义一个resultMap.这样就无需要自己定义处理器了。
可以从分析2.0和2.0的dtd文件知道。
发表评论
-
流程监控(去除节假日和双休日)
2010-10-19 16:27 1187流程监控(去除节假日和双休日),有问题请留言 -
存储过程常用技巧4
2009-10-27 16:41 703上面的两种方式都是非常的复杂,如果仅仅是需要返回一个结果集,那 ... -
存储过程常用技巧2
2009-10-27 16:31 7732. 存储过程内部块 2.1 内部块 我们知道了存储过程的结构 ... -
存储过程常用技巧1
2009-10-27 16:28 985我们在进行pl/sql编程时打交道最多的就是存储过程了。存储过 ... -
oracle 相关工具以及基础知识
2009-08-14 15:47 662oracle 相关工具以及基础知识
相关推荐
Oracle存储过程常用技巧 Oracle存储过程是一种强大的数据库对象,它可以帮助开发者简化复杂的业务逻辑,并提高数据库的安全性和性能。在 Oracle 中,存储过程是一种特殊的 PL/SQL 程序,它可以接受输入参数,执行...
介绍oracle存储过程的常用使用技巧
存储过程是在大型数据库系统中存储过程在数据库中经过第一次编译后就不需要再次编译,用户通过指定存储过程的名字并给出参数来,通过本篇文章带领大家去学习oracle存储过程常用的技巧,感兴趣的朋友一起来学习吧
VB常用技巧合集 chm全集,比如ActiveX .Exe .Dll Server的多执行绪,ADO/DAO Bolb资料的存取,ADO/RDO Concurrency for SQL 的比较,ADO 连线的其他注意事项,ADO阶层式资料库表达,ADO设定独占性的资料库,Check两...
流水线操作是一种常用的数据处理技巧,通过将数据处理过程分解为多个阶段,每个阶段完成特定的数据处理任务,实现高速数据流的处理。流水线操作可以应用于各种数据处理场景,例如在数字信号处理中,将信号处理过程...
例如,整数1234567分解后,数组digit[10]将依次存储7,6,5,4,3,2,1。 2. 数字的条件判断:为了检查一个数字的各个数位是否都不相同,可以创建一个计数数组times,遍历数字的每一位并累加对应的计数。如果某个计数...
您可能感兴趣的文章:Oracle存储过程游标用法分析oracle存储过程常用的技巧(详)asp.net中调用oracle存储过程的方法C#调用Oracle存储过程的方法oracle 存储过程详细介绍(创建,删除存储过程,参数传递等)Oracle存储...
java调用Oracle的存储过程 Oracle实现分页
主要内容: Framework Manager 分层设计思想、存储过程运用 Report Studio 报表类型选择、条件变量运用、聚合方式选择、报表数据处理 RIDE 特殊参数设计、报表调用
Asp.net页面内传参数方法,调用存储过程(两种方法比较),调用存储过程通用类DBHelper,加密解密,SQL常用DBHelper,Asp.net存储过程无限分类,TreeView无限分类,无限分类MVC,荧光棒效果 获取控件上全选,回车转换...
乒乓操作的处理流程为:输入数据流通过“输入数据选择单元”将数据流等时分配到两个数据缓冲区,数据缓冲模块可以为任何存储模块,比较常用的存储单元为双口 RAM(DPRAM) 、单口 RAM(SPRAM) 、 FIFO 等。在第一个缓冲...
PL/SQL是Oracle数据库的过程语言,可以用来编写存储过程、函数和触发器。优化PL/SQL代码可以提高整个数据库的性能。可以使用以下技巧来优化PL/SQL代码: * 使用缓存机制 * 限制循环的次数 * 使用数组代替游标 4. ...
3.ETL过程(Extract, Transform, Load):ETL过程是数据仓库建模中的一个关键步骤,用于从不同的来源抽取数据,经过转换和清洁后,加载到数据仓库中。 数据仓库建模的五步法: 第一步:确定主题 确定主题是数据...
主要介绍了MySQL存储过程概念、原理与常见用法,结合实例形式详细分析了mysql存储过程的概念、原理、创建、删除、调用等各种常用技巧与相关注意事项,需要的朋友可以参考下
主要介绍了MySQL数据库常用操作技巧,结合实例形式总结分析了mysql查询、存储过程、字符串截取、时间、排序等常用操作技巧,需要的朋友可以参考下
11:查看与某一个表相关的视图、存储过程、函数 12:查看当前数据库中所有存储过程 13:查询用户创建的所有数据库 14:查询某一个表的字段和数据类型 15:不同服务器数据库之间的数据操作 SQL Server基本函数 ...
调用存储过程(两种方法比较) 调用存储过程通用类DBHelper 加密解密 SQL常用DBHelper Asp.net存储过程无限分类 TreeView无限分类 无限分类MVC 荧光棒效果 获取控件上全选 回车转换成Tab ...