JDBC calls Oracle Collections

Keywords: Java Database SQL Oracle

Use Java to access and manipulate Oracle custom types through database connection (JDBC).
That's roughly what this page says. https://docs.oracle.com/cd/E1...

Oracle supports defining data formats in databases. (Nested data formats)
When invoked in JAVA, the data in the corresponding format can be passed in.
Find the corresponding information and record it.
Just found a simple feasible solution, because someone wrote PLSQL specifically to process logic, so the logic is all placed in the database processing, JAVA side of the simple code to transfer data on the line.

CREATE TYPE TDC_SUPPLIER_ADDR AS OBJECT
            (ADDRESS VARCHAR2(240),ADDRESS_ID NUMBER);
            
CREATE TYPE TDC_SUPPLIER_ADDR_L AS TABLE OF TDC_SUPPLIER_ADDR;
 
CREATE TYPE TDC_SUPPLIER AS OBJECT
            (NAME VARCHAR2(50),SUPPLIER_ID NUMBER, ADDR TDC_SUPPLIER_ADDR_L);
            
CREATE TABLE TDC_TEST (
TEXT VARCHAR2(2000));
 
CREATE OR REPLACE PACKAGE TDC_TEST_PKG IS
 
  PROCEDURE CREATE_SUPPLIER (
    SUP_OBJ IN TDC_SUPPLIER);
END TDC_TEST_PKG;
 
CREATE OR REPLACE PACKAGE BODY TDC_TEST_PKG IS
 
  PROCEDURE CREATE_SUPPLIER (
    SUP_OBJ IN TDC_SUPPLIER) IS 
    
    
  BEGIN
    INSERT INTO TDC_TEST VALUES (SUP_OBJ.NAME);
    
    COMMIT;
  END CREATE_SUPPLIER;
END TDC_TEST_PKG;

The above SQL is a simple example.
A data type TDC_SUPPLIER is declared. When calling CREATE_SUPPLIER, TDC_SUPPLIER type data need to be passed in as a parameter.
It converts TDC_SUPPLIER to json, roughly in the following format

{
"NAME":"",
"SUPPLIER_ID":0,
"ADDR": [
  {
    "ADDRESS": "",
    "ADDRESS_ID": 0
  },
  {
    "ADDRESS": "",
    "ADDRESS_ID": 0
  }
]
}

It defines a TDC_SUPPLIER_ADDR data type with ADDRESS and ADDRESS_ID attributes.
A TDC_SUPPLIER_ADDR_L array is defined, which contains many TDC_SUPPLIER_ADDR.
TDC_SUPPLIER_ADDR_L is taken as an attribute of TDC_SUPPLIER.

Refer to the link to implement the code

package com.hktdc.po.api.approval.test;

import oracle.sql.STRUCT;
import oracle.sql.StructDescriptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Test2 {

    private final Logger logger = LoggerFactory.getLogger(Test1.class);

    public static void main(String[] args)
            throws ClassNotFoundException, SQLException {
        DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
        // Open database connection
        String url = "jdbc:oracle:thin:@//127.0.0.1:1521/DB";
        Connection conn = DriverManager.getConnection(url, "apps", "apps");
        conn.setAutoCommit(false);

        // StructDescriptor.createDescriptor indicates that an object corresponding to the database is to be created in JAVA. The first parameter is the type name in the database.
        StructDescriptor addDesc = StructDescriptor.createDescriptor("TDC_SUPPLIER_ADDR", conn);
        /*
        Through the new STRUCT method, the parameters in JAVA are converted into objects in database.
        The first parameter is the database type name object
        The second parameter is the database connection
        The third parameter is the object array, which represents the value in the object. Note that the type of the value in the object should be the same as the corresponding parameter type in the database.
         */
        Object[] addArr = {"Xie", 10001};
        STRUCT addRecord = new STRUCT(addDesc, conn, addArr);
        Object[] addArr2 = {"Longgang District", 10001};
        STRUCT addRecord2 = new STRUCT(addDesc, conn, addArr2);

        /*
        Build nested objects.
        StructDescriptor Simply define the type of CREATE TYPE ** AS OBJECT in the database.
        TDC_SUPPLIER_ADDR_L Representing multiple TDC_SUPPLIER_ADDR in the database does not need to be defined in JAVA, just need to convert multiple TDC_SUPPLIER_ADDR into an Object array.
         */
        StructDescriptor supDesc = StructDescriptor.createDescriptor("TDC_SUPPLIER", conn);
        Object[] addLData = {addRecord, addRecord2};
        Object[] data = {"Zhang San", 302, addLData};
        STRUCT supplier = new STRUCT(supDesc, conn, data);

        // Create a call statement
        CallableStatement stmt = conn.prepareCall("{ call TDC_TEST_PKG.CREATE_SUPPLIER(?) }");

        // Bind the input record to set the first parameter?
        stmt.setObject(1, supplier);
        // Implementation of SQL
        stmt.execute();

    }
}

Posted by Simmo on Tue, 01 Oct 2019 08:58:02 -0700