为了减少连接Oracle数据库的数量,需要将多条数据作为变量一次传入Oracle的存储过程中。方法如下:?
步骤一:定义对象类型。?
CREATE TYPE department_type AS OBJECT (?
DNO NUMBER (10),?
NAME VARCHAR2 (50),?
LOCATION VARCHAR2 (50)?
);?
步骤二:定义一个对象类型的数组对象。?
CREATE TYPE dept_array AS TABLE OF department_type;?
步骤三:定义存储过程来插入数据。?
CREATE OR REPLACE PACKAGE objecttype AS?
? PROCEDURE insert_object (d dept_array);?
END objecttype;?
CREATE OR REPLACE PACKAGE BODY objecttype?
AS?
PROCEDURE insert_object (d dept_array)?
AS?
BEGIN?
FOR i IN d.FIRST..d.LAST?
LOOP?
INSERT INTO department_teststruct?
VALUES (d(i).dno,d(i).name,d(i).location);?
END LOOP;?
END insert_object;?
END objecttype;?
步骤四(可选步骤,即可以不做):定义一个Java class来映射对象中类型。?
步骤五:定义Java方法来调用存储过程。?
import java.sql.Connection;?
import java.sql.DriverManager;?
import oracle.jdbc.OracleCallableStatement;?
import oracle.sql.ARRAY;?
import oracle.sql.ArrayDescriptor;?
import oracle.sql.STRUCT;?