a我考网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 73|回复: 0

[综合] Array传入存储过程的示例

[复制链接]
发表于 2012-8-4 13:54:49 | 显示全部楼层 |阅读模式
Oracle中把Array类型作为参数传入存储过程并用Java对其进行调用的示例是本文我们主要要介绍的内容,接下来我们就开始介绍这一过程。     create or replace procedure test1(arr in varchar_array) is  begin/ Y: F: e, w. v! |0 W) B, i
    FOR i IN arr.first  arr.last LOOP  insert into tb values(arr(i));4 r. E( I! M; P3 p' i
    END LOOP;  end test1;
3 _- P  S9 n! a    String driver = "oracle.jdbc.driver.OracleDriver";
$ G  E& N4 e* b# j# C/ j" s9 h- X* |0 o    String strUrl = "jdbc:oracle:thin:@192.168.1.199:1521:orcl";
; E) A$ H/ d9 |/ ^  {+ ~6 @    Statement stmt = null;  ResultSet rs = null;1 Z; d5 w' b6 A4 [( y
    Connection conn = null; try {  Class.forName(driver);% p+ |- W' ], `) A/ r, Q% Q. \
    conn = DriverManager.getConnection(strUrl, "user", "pass");
. p+ A" m9 @6 D    CallableStatement proc = null;
- Y( n3 |# L0 G  t) ]4 p7 N    proc = conn.prepareCall("{ call test1(?) }");
0 W  P) `* M0 G    //调用存储过程test1  //不一样的地方,获得上面创建的自定义的类型,注意大小写
; e1 l2 D! h7 a    ArrayDescriptor descriptor = ArrayDescriptor.createDescriptor("VARCHAR_ARRAY",   conn);8 q& Y- `. O' v: R6 ^
    List list = new ArrayList();  list.add("a");
( w7 P$ r' Y( C! E" Q# w( D    list.add("b");中华考试网
7 E  [8 F. x) K# V5 F$ j1 U2 |8 ^    //把list中的元素转换成自定义的类型  ARRAY array = new ARRAY(descriptor, conn, list.toArray());
) r7 a* a( ~4 C" [$ Z4 h    proc.setArray(1, array); proc.execute();
# m& e3 h1 A- U/ K" ~    catch (Exception ex) {  ex.printStackTrace();
) P0 @4 ~7 ?! n) T& N    } finally {  …… 各种 close ……  }
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Woexam.Com ( 湘ICP备18023104号 )

GMT+8, 2024-5-15 04:33 , Processed in 0.310033 second(s), 21 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表