16

I use the below JDBC code to call an Oracle stored procedure which takes an Array input.

But the the below three classes are deprecated. How to replace this ?

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

Java code

        Object[] reportArray = new Object[3]; 
        STRUCT[] struct = new STRUCT[reports.size()];

        ArrayDescriptor arrayDescriptor = new ArrayDescriptor(new SQLName("T_REPORT_TABLE", (OracleConnection) connection), connection);
        StructDescriptor structDescriptor = StructDescriptor.createDescriptor("R_REPORT_OBJECT", connection);

        int arrayIndex = 0;
        for (Report data : reports) {
            reportArray[0] = data.getXXX();
            reportArray[1] = data.getYYY();
            reportArray[2] = data.getZZZ();

            struct[arrayIndex++] = new STRUCT(structDescriptor, connection, reportArray);
        }

        oracle.sql.ARRAY reportsArray = new oracle.sql.ARRAY(arrayDescriptor, connection, struct);
        callableStatement.setArray("T_REPORT_IN", reportsArray);

        callableStatement.executeUpdate();
Jay
  • 9,189
  • 12
  • 56
  • 96

2 Answers2

21

Thanks UUIUI, I now removed the deprecated classes and the fixed code looks as below if anyone needs it later.

    Object[] reportArray = new Object[3]; 
    Struct[] struct = new Struct[reports.size()];

    int arrayIndex = 0;
    for (Report data : reports) {
        reportArray[0] = data.getXXX();
        reportArray[1] = data.getYYY();
        reportArray[2] = data.getZZZ();

        struct[arrayIndex++] = connection.createStruct("R_REPORT_OBJECT", reportArray);
    }

    Array reportsArray = ((OracleConnection) connection).createOracleArray("T_REPORT_TABLE", struct);
    callableStatement.setArray("T_REPORT_IN", reportsArray);

    callableStatement.executeUpdate();          
Jay
  • 9,189
  • 12
  • 56
  • 96
  • 2
    ok, but if you use a connection pool, you cannot just type cast the connection. Then you have to use the unwrap method. see https://stackoverflow.com/a/15483625/1436741 – stackunderflow May 11 '20 at 05:49
20

From oracle API documentation.

ArrayDescriptor

Use factory method OracleConnection.createOracleArray to create an instance of java.sql.Array directly.

STRUCT

Use java.sql.Struct interface for declaration instead of using concrete class oracle.sql.STRUCT.

StructDescriptor

Use factory method Connection.createStruct to create an instance of java.sql.Struct directly.

Here are the full list of Deprecated Classes mentioned in the oracle API documentation.

Bacteria
  • 8,406
  • 10
  • 50
  • 67
  • 2
    Ok,that's cute - so how am I supposed to get an instance of `OracleConnection` when I'm working against a `DataSource` instance that may or may not be doing various fancy proxy/wrapping tricks? – demaniak Sep 06 '16 at 12:16
  • @demaniak casting the generic connection object to oracleconnection should work above java 6 – yolob 21 Nov 04 '19 at 19:26