简体   繁体   中英

Setting a user-defined SQL TYPE in Java

I have to invoke a Stored Procedure which has an IN parameter as "Type" from Java. Please see below. How to set the " CusInfoList IN CUSTOMER_INFO_LIST -- TYPE " using java?

SQL> create or replace procedure insert_XXXX
  2  (
  3   L_TXN_ID    VARCHAR2
  4  ,L_RESOLUTION_ID    NUMBER
  5  ,L_COMPENSATION_TYPE       VARCHAR2
  6  ,L_COMPENSATION_VALUE       NUMBER
  7  ,L_CLAIM_TYPE       VARCHAR2
  8  ,L_CLAIM_DETAIL    VARCHAR2
  9  ,L_VOUCHER_TYPE_CODE       VARCHAR2
 10  ,L_REMARKS    VARCHAR2
 11  ,L_OTH_REMARKS       VARCHAR2
 12  ,L_STATUS       VARCHAR2
 13  ,L_OVERRIDE_FLAG    CHAR
 14  ,L_EMP_NUMBER       VARCHAR2
 15  ,L_AGENT_LOCATION    VARCHAR2
 16  ,L_LST_UPDATED_TIMESTAMP    TIMESTAMP
 17  ,L_CREATED_TIMESTAMP       TIMESTAMP DEFAULT SYSDATE
 18  ,L_CODE    VARCHAR2
 19  ,L_FL_NO       NUMBER
 20  ,L_FL_DATE    DATE
 21  ,L_FL_STA    VARCHAR2
 22  ,L_FL_A_STA    VARCHAR2
 23  ,L_RECORD_LOCATOR         VARCHAR2
 24  ,L_RECORD_CREATE_DATE   DATE
 25  **,CusInfoList IN CUSTOMER_INFO_LIST -- TYPE**
 26  )
 27  IS
 28  begin
 29  SAVEPOINT ins_sp;

The type CUSTOMER_INFO_LIST is declared as follows:

CREATE OR REPLACE TYPE CUSTOMER_INFO_TYPE AS OBJECT (
  LAST_NAME VARCHAR2(20 CHAR), 
  FIRST_NAME VARCHAR2(20 CHAR),
  CUST_NUMBER NUMBER, 
  LOY_NUMBER VARCHAR2(10 CHAR),
  HEL_SCORE NUMBER,
  TIER_STATUS VARCHAR2(25 CHAR),
  EMAIL_ADDRESS VARCHAR2(50 CHAR),
  PHONE_NUMBER VARCHAR2(20 CHAR),
  MAILING_ADDRESS VARCHAR2(100 CHAR),
  CASE_NUMBER VARCHAR2(50 CHAR),
  CASE_STATUS VARCHAR2(20 CHAR)
);

CREATE OR REPLACE TYPE isolve.CUSTOMER_INFO_LIST AS VARRAY(25) OF ISOLVE.CUSTOMER_INFO_TYPE;

There are a few things you need to do.

Firstly, you need to create a class ( CustomerInfo , say) that implements SQLData and which corresponds to your CUSTOMER_INFO_TYPE class. The Oracle documentation contains an example of how such a class would look. I'm not going to write this class out in full for you as your type has quite a few properties.

You will also need to add a type mapping to the connection's type map. This is also something covered in the page I linked to.

Once you've done that, you can pass a CUSTOMER_INFO_LIST object as an array of CustomerInfo objects, using something like the following. Note that the precise code you need to use to create the array depends on which version of the Oracle JDBC driver JAR you are using:

CustomerInfo[] customerInfos = ... ; // get these from somewhere
Connection conn = ... ; // database connection

// if using ojdbc7.jar, requires 'import oracle.jdbc.OracleConnection'
Array array = ((OracleConnection)conn).createOracleArray("ISOLVE.CUSTOMER_INFO_LIST", customerInfos);

// if using ojdbc6.jar or earlier, requires 'import oracle.sql.ARRAY'
// and 'import oracle.sql.ArrayDescriptor'
ArrayDescriptor arrayDesc = ArrayDescriptor.createDescriptor("ISOLVE.CUSTOMER_INFO_LIST", conn);
ARRAY array = new ARRAY(arrayDesc, conn, customerInfos);

CallableStatement statement = conn.prepareCall("....");
statement.setString(1, transactionId);
// ... more parameters ...
statement.setArray(NNN, array); // replace NNN with parameter number

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM