How to fix deprecated oracle.sql.ArrayDescriptor, oracle.sql.STRUCT and oracle.sql.StructDescriptor

六眼飞鱼酱① 提交于 2019-12-29 06:55:08

问题


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();

回答1:


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.




回答2:


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();          


来源:https://stackoverflow.com/questions/33347134/how-to-fix-deprecated-oracle-sql-arraydescriptor-oracle-sql-struct-and-oracle-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!