2013-10-02 77 views
0

包,在我宣布我的数据类型我怎么能填充和使用Java

create or replace package pkg_var is 
type array_table is table of varchar2(50); 
type array_int is table of number; 
type p_arr_rec is record(p_no number,p_val varchar2(50)); 
type array_record is table of p_arr_rec; 
end pkg_var; 
/

我的过程来填充记录检索记录的表

create or replace procedure proc1(p_array in pkg_var.array_table, 
arr_int in pkg_var.array_int,len out number,arr_rec out 
pkg_var.array_record) 
as 
v_count number; 
begin 
len := p_array.count; 
v_count :=0; 
for i in 1..p_array.count 
loop 
--dbms_output.put_line(p_array(i)); 
arr_rec(i).p_no := arr_int(i); 
arr_rec(i).p_val := p_array(i); 
v_count := v_count+1; 
end loop; 
end; 
/

这是我class调用以上程序将数组填入记录表中

public class TestDatabase { 

    public static void passArray() 
    { 
     try{ 
      dbcon conn = new dbcon(); 
      Connection con = conn.dbstate().getConnection(); 

      String str_array[] = {"one", "two", "three","four"}; 
      int int_array[] = {1, 2, 4,8}; 
      ArrayDescriptor str_des = ArrayDescriptor.createDescriptor("SCOTT.PKG_VAR.ARRAY_TABLE", con); 
      ARRAY arr_str = new ARRAY(str_des,con,str_array); 
      ArrayDescriptor des = ArrayDescriptor.createDescriptor("SCOTT.PKG_VAR.ARRAY_INT", con); 
      ARRAY arr_int = new ARRAY(des,con,int_array); 

      CallableStatement st = con.prepareCall("call SCOTT.proc1(?,?,?)"); 

      // Passing an array to the procedure - 
      st.setArray(1, arr_str); 
      st.setArray(2, arr_int); 
      st.registerOutParameter(3, Types.INTEGER); 
      st.registerOutParameter(4,OracleTypes.ARRAY,"SCOTT.PKG_VAR.ARRAY_RECORD"); 
      st.execute(); 

      System.out.println("size : "+st.getInt(3)); 

      // Retrieving array from the resultset of the procedure after execution - 
      ARRAY arr = ((OracleCallableStatement)st).getARRAY(4); 
      BigDecimal[] recievedArray = (BigDecimal[])(arr.getArray()); 

      for(int i=0;i<recievedArray.length;i++) 
       System.out.println("element" + i + ":" + recievedArray[i] + "\n"); 

     } catch(Exception e) { 
      System.out.println(e); 
     } 
    } 

    public static void main(String args[]){ 
     passArray(); 
    } 
} 

我得到java.sql.SQLException: invalid name pattern: SCOTT.PKG_VAR.ARRAY_TABLE异常,任何人都可以帮助我解决这个异常。

还有一个问题,我该如何检索中SQL的记录表格?我的第二个问题是这个代码 ARRAY arr = ((OracleCallableStatement)st).getARRAY(4);

这是一个有效的代码table of record

回答

0

我不认为你可以做你想做与PLSQL数组类型是什么,你需要在数据库中创建“数组类型”,如:

create or replace type rectype as object(col1 varchar2(10),col2 varchar2(10)); 
/
create or replace type rectypetab as table of rectype; 
/

我介绍如何使用这一切都与Java这里:http://betteratoracle.com/posts/32-passing-arrays-of-record-types-between-oracle-and-java

+0

在上面的链接,我找不到检索表 – Ravi

+0

的记录我没有一个拉回记录数组的完整示例。我展示了如何检索一个简单的数组http://betteratoracle.com/posts/26-passing-arrays-between-java-and-oracle-procedures以及如何检索一个非数组记录 - http:// betteratoracle。 com/posts/31-passing-record-types-between-oracle-and-java如果你把它和上面的其他链接放在一起,你应该可以解决它。基本上,您从Oracle检索记录数组,然后将其转换回Java数组(然后是Oracle结构数组),然后将每个结构转换回Java数组。 –