如何使用java填充和检索记录表

如何使用java填充和检索记录表,java,sql,arrays,oracle,record,Java,Sql,Arrays,Oracle,Record,包,我在其中声明了我的数据类型 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 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;
/
这是我的
调用上述过程将数组填充到记录表中

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();
    }
}
公共类测试数据库{
公共静态无效通道()
{
试一试{
dbcon conn=新的dbcon();
连接con=conn.dbstate().getConnection();
字符串str_数组[]={“一”、“二”、“三”、“四”};
int_数组[]={1,2,4,8};
ArrayDescriptor str_des=ArrayDescriptor.createDescriptor(“SCOTT.PKG_VAR.ARRAY_TABLE”,con);
数组arr_str=新数组(str_des、con、str_数组);
ArrayDescriptor des=ArrayDescriptor.createDescriptor(“SCOTT.PKG_VAR.ARRAY_INT”,con);
数组arr_int=新数组(des、con、int_数组);
CallableStatement st=con.prepareCall(“调用SCOTT.proc1(?,?)”;
//将数组传递给过程-
st.setArray(1,arr_str);
st.setArray(2个阵列);
st.registerOutParameter(3,Types.INTEGER);
st.registerOutParameter(4,OracleTypes.ARRAY,“SCOTT.PKG_VAR.ARRAY_RECORD”);
st.execute();
System.out.println(“尺寸:+st.getInt(3));
//执行后从过程的结果集检索数组-
数组arr=((OracleCallableStatement)st).getARRAY(4);
BigDecimal[]ReceiveDarray=(BigDecimal[])(arr.getArray());

对于(int i=0;i我认为PLSQL数组类型无法满足您的需要,您需要在数据库上创建“数组类型”,例如:

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

我在这里描述了如何在Java中使用这一切:

在上面的链接中,我找不到检索表的记录我没有一个完整的例子来提取记录数组。我展示了如何检索一个简单的数组,以及如何检索一个非数组记录-如果你把它与上面的另一个链接放在一起,你应该能够在基本上,从Oracle检索记录数组,将其转换回Java数组(然后是Oracle结构数组),然后将每个结构转换回Java数组。