在Oracle DB中:
我有以下存储过程:
procedure getInfo ( p_ids IN IDS_TABLE, p_details OUT cursor )
类型IDS_TABLE是:
create or replace type IDS_TABLE as table of IDS create or replace type IDS as object ( id1 NUMBER, id2 NUMBER, id3 NUMBER )
如何在Java中调用getInfo?
手动设置Oracle SQL对象和java对象之间的链接不是一件容易的事。特别是,从用户定义对象的数组(或嵌套表)到Java到Oracle的传递比标准数据类型的数组更复杂。换句话说,调用带有签名的过程会更容易:
(TABLE OF NUMBER, TABLE OF NUMBER, TABLE OF NUMBER)`
而不是签名为:
(TABLE OF (NUMBER, NUMBER, NUMBER)) <- your case
你可以在过程周围编写包装器,以将第二种情况转换为第一种情况。
话虽如此,映射你的程序到目前为止并非没有可能。以下示例在很大程度上受到Tom Kyte的帖子的启发。汤姆(Tom)描述了如何映射TABLE OF NUMBER使用oracle.sql.ARRAY。在你的情况下,我们还必须使用oracle.sql.STRUCT映射IDSSQL对象。
TABLE OF NUMBER
oracle.sql.ARRAY
你可能还需要浏览Oracle JDBC文档,特别是使用Oracle对象类型一章。
首先是与你的设置类似的设置:
SQL> CREATE OR REPLACE TYPE IDS AS OBJECT ( id1 NUMBER, id2 NUMBER, id3 NUMBER ); 2 / Type created SQL> CREATE OR REPLACE TYPE IDS_TABLE AS TABLE OF IDS; 2 / Type created SQL> CREATE OR REPLACE PROCEDURE getInfo(p_ids IN IDS_TABLE) IS 2 BEGIN 3 FOR i IN 1 .. p_ids.COUNT LOOP 4 dbms_output.put_line(p_ids(i).id1 5 || ',' || p_ids(i).id2 6 || ',' || p_ids(i).id3); 7 END LOOP; 8 END getInfo; 9 / Procedure created
这是java过程:
SQL> CREATE OR REPLACE 2 AND COMPILE JAVA SOURCE NAMED "ArrayDemo" 3 as 4 import java.io.*; 5 import java.sql.*; 6 import oracle.sql.*; 7 import oracle.jdbc.driver.*; 8 9 public class ArrayDemo { 10 11 public static void passArray() throws SQLException { 12 13 Connection conn = 14 new OracleDriver().defaultConnection(); 15 16 17 StructDescriptor itemDescriptor = 18 StructDescriptor.createDescriptor("IDS",conn); 19 20 Object[] itemAtributes = new Object[] {new Integer(1), 21 new Integer(2), 22 new Integer(3)}; 23 STRUCT itemObject1 = new STRUCT(itemDescriptor,conn,itemAtributes); 24 25 itemAtributes = new Object[] {new Integer(4), 26 new Integer(5), 27 new Integer(6)}; 28 STRUCT itemObject2 = new STRUCT(itemDescriptor,conn,itemAtributes); 29 30 STRUCT[] idsArray = {itemObject1,itemObject2}; 31 32 ArrayDescriptor descriptor = 33 ArrayDescriptor.createDescriptor( "IDS_TABLE", conn ); 34 35 ARRAY array_to_pass = 36 new ARRAY( descriptor, conn, idsArray ); 37 38 OraclePreparedStatement ps = 39 (OraclePreparedStatement)conn.prepareStatement 40 ( "begin getInfo(:x); end;" ); 41 42 ps.setARRAY( 1, array_to_pass ); 43 ps.execute(); 44 45 } 46 } 47 / Java created
我们称之为:
SQL> CREATE OR REPLACE 2 PROCEDURE show_java_calling_plsql 3 AS LANGUAGE JAVA 4 NAME 'ArrayDemo.passArray()'; 5 / Procedure created SQL> exec show_java_calling_plsql ; 1,2,3 4,5,6 PL/SQL procedure successfully completed