簡體   English   中英

如何使用Java填充和檢索記錄表

[英]how can I populate and retrieve table of record using 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異常,任何人都可以幫助我解決此異常。

還有一個問題,如何在Java檢索SQL記錄表? 我的第二個問題是此代碼ARRAY arr = ((OracleCallableStatement)st).getARRAY(4);

獲取table of record是否有效?

我認為您無法使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM