如何在java中处理oracle中的对象数组?
如何在java中处理oracle中的对象数组?
在java中处理oracle中的对象数组(嵌套表):
sp文件:
-------------------------------CreateObjectType----------------------------------
createorreplacetypeo_tmpasobject(
useridvarchar2(10),
usernamevarchar2(20)
);
/
---------------------------CreateNestedTableTypeOnBaseObjectType------------------------
createorreplacetypetmp_arrayistableofo_tmp;
/
----------------------------CreateaPackageFile-----------------------------------------------------------
createorreplacepackagebodytest_object
as
proceduregetObject(obj_arrouttmp_array)
is
tmp_objo_tmp;
test_arrtmp_array:=tmp_array();
nnumber:=1;
begin
loop
exitwhenn>5;
tmp_obj:=o_tmp('test1'||n,'fuyue'||n);
test_arr.extend;
test_arr(n):=tmp_obj;
n:=n+1;
endloop;
obj_arr:=test_arr;
forninobj_arr.first..obj_arr.lastloop
tmp_obj:=obj_arr(n);
dbms_output.put_line(tmp_obj.userid||'---'||tmp_obj.username);
endloop;
endgetObject;
endtest_object;
/
createorreplacetypeo_tmpasobject(
useridvarchar2(10),
usernamevarchar2(20)
);
/
---------------------------CreateNestedTableTypeOnBaseObjectType------------------------
createorreplacetypetmp_arrayistableofo_tmp;
/
----------------------------CreateaPackageFile-----------------------------------------------------------
createorreplacepackagebodytest_object
as
proceduregetObject(obj_arrouttmp_array)
is
tmp_objo_tmp;
test_arrtmp_array:=tmp_array();
nnumber:=1;
begin
loop
exitwhenn>5;
tmp_obj:=o_tmp('test1'||n,'fuyue'||n);
test_arr.extend;
test_arr(n):=tmp_obj;
n:=n+1;
endloop;
obj_arr:=test_arr;
forninobj_arr.first..obj_arr.lastloop
tmp_obj:=obj_arr(n);
dbms_output.put_line(tmp_obj.userid||'---'||tmp_obj.username);
endloop;
endgetObject;
endtest_object;
/
java文件:
importjava.sql.Array;
importjava.sql.CallableStatement;
importjava.sql.DriverManager;
importjava.sql.SQLException;
importoracle.jdbc.OracleConnection;
importoracle.jdbc.driver.OracleTypes;
importoracle.sql.Datum;
importoracle.sql.STRUCT;
publicclassObjArray{
privatestaticStringstrDriver="oracle.jdbc.driver.OracleDriver";
privatestaticStringstrUrl="jdbc:oracle:thin:@192.160.10.14:1521:avex";
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
test();
}
privatestaticvoidtest(){
try{
Class.forName(strDriver);
OracleConnectioncon=(OracleConnection)DriverManager
.getConnection(strUrl,"avex","avex");
CallableStatementstmt=null;
stmt=con.prepareCall("{calltest_object.getObject(?)}");
stmt.registerOutParameter(1,OracleTypes.ARRAY,"TMP_ARRAY");
stmt.execute();
Arrayoutparam=stmt.getArray(1);
Object[]obj=(Object[])outparam.getArray();
for(inti=0;i<obj.length;i++){
STRUCTstruct=(STRUCT)obj[i];
Datum[]dt=struct.getOracleAttributes();
for(intn=0;n<dt.length;n++){
System.out.println(dt[n]);
}
}
}catch(ClassNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
importjava.sql.CallableStatement;
importjava.sql.DriverManager;
importjava.sql.SQLException;
importoracle.jdbc.OracleConnection;
importoracle.jdbc.driver.OracleTypes;
importoracle.sql.Datum;
importoracle.sql.STRUCT;
publicclassObjArray{
privatestaticStringstrDriver="oracle.jdbc.driver.OracleDriver";
privatestaticStringstrUrl="jdbc:oracle:thin:@192.160.10.14:1521:avex";
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
test();
}
privatestaticvoidtest(){
try{
Class.forName(strDriver);
OracleConnectioncon=(OracleConnection)DriverManager
.getConnection(strUrl,"avex","avex");
CallableStatementstmt=null;
stmt=con.prepareCall("{calltest_object.getObject(?)}");
stmt.registerOutParameter(1,OracleTypes.ARRAY,"TMP_ARRAY");
stmt.execute();
Arrayoutparam=stmt.getArray(1);
Object[]obj=(Object[])outparam.getArray();
for(inti=0;i<obj.length;i++){
STRUCTstruct=(STRUCT)obj[i];
Datum[]dt=struct.getOracleAttributes();
for(intn=0;n<dt.length;n++){
System.out.println(dt[n]);
}
}
}catch(ClassNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}