數(shù)據(jù)做為存儲過程參數(shù)在JAVA中的調(diào)用
--定義數(shù)組,該數(shù)據(jù)不能定義在包中
CREATE OR REPLACE TYPE idArray AS TABLE OF integer;
--包頭
CREATE OR REPLACE PACKAGE pg_data_acquire IS
PROCEDURE p_test( ids IN idArray, exist OUT NUMBER);
END pg_data_acquire;
--包體
CREATE OR REPLACE PACKAGE BODY pg_data_acquire IS
PROCEDURE p_test(
ids IN idArray,
exist OUT NUMBER) AS
v_Index BINARY_INTEGER;
BEGIN
v_Index:= ids.FIRST;
LOOP
dbms_output.put_line(ids(v_Index));
EXIT WHEN v_Index=ids.LAST;
v_Index:= ids.NEXT(v_Index);
END LOOP;
exist :=v_Index;
END p_test;
END pg_data_acquire;
--JAVA程序調(diào)用
public class TestArrays
{
public static void main(String[] args)
{
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@sr0002sz:1521:orcl","ora10g","ora10.g");
CallableStatement cstmt = con.prepareCall("{ call pg_data_acquire.p_test(?,?) }");
ArrayDescriptor desc = null;
ARRAY bookIdArray = null;
int count = 0;
String[] bookIds = {"1","2","3"};
//desc = ArrayDescriptor.createDescriptor("IDARRAY", con);//pg_public.tt_type
desc = ArrayDescriptor.createDescriptor("PG_PUBLIC.TT_TYPE", con);//pg_public.tt_type
bookIdArray = new ARRAY(desc, con, bookIds);
cstmt.setObject(1, bookIdArray, oracle.jdbc.OracleTypes.ARRAY);
cstmt.registerOutParameter(2, OracleTypes.VARCHAR);
cstmt.execute();
count = cstmt.getInt(2);
cstmt.close();
con.close();
}catch (Exception e){
System.out.println(e);
}
}
}
想了解更多?現(xiàn)在就開始免費體驗