简体   繁体   中英

Send & Retrieve array list to oracle stored procedure

I'm trying to push "Arraylist" to oracle stored procedure and after making necessary modification, the object it is returned back.

I have an oracle stored procedure with an inout parameter which is "AS TABLE OF TYPE".

I'm able to make the call using Mybatis by implements TypeHandler and overrides its method public void setParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException

But I'm facing issue while retrieving the object.

For retrieving I Override the below method.

public Object getResult(CallableStatement cs, int columnIndex) throws SQLException {

    ARRAY array_to_pass = ((OracleCallableStatement) cs).getARRAY(1);       

    /* showing content */
    Datum[] elements = array_to_pass.getOracleArray();

    for (int i = 0; i < elements.length; i++) {
        Object[] element = ((STRUCT) elements[i]).getAttributes();
        String value = (String) element[0];
        System.out.println("array(" + i + ").val=" + value);
    }

}

I'm getting the below error java.lang.IllegalAccessError: tried to access class oracle.jdbc.driver.OracleCallableStatement

Statement generating the error is




Any thoughts/help on how to retrieve table of type object from oracle .


I'm able to implement it successfully using Spring, Mybatis & Oracle. The reference code to implement is as below



CREATE OR REPLACE TYPE TypeName AS OBJECT(
Field1 varchar(6), Field2 varchar(6) )



CREATE OR REPLACE TYPE TableTypeName AS TABLE OF TypeName;







CALL StoredProcedureName( #{Parameter_in, javaType=Object, jdbcType=ARRAY, jdbcTypeName=TableTypeName , mode=INOUT, typeHandler=javaHandlername} )



public class javaHandlername implements TypeHandler {
    // Set data to oracle object
    @Override
    public void setParameter(PreparedStatement ps, int i, Object parameter,
        JdbcType jdbcType) throws SQLException 
    {
        List<TO> TOs = (List<TO>) parameter;

        StructDescriptor structDescriptor = StructDescriptor
        .createDescriptor("TypeName", ps.getConnection());
        STRUCT[] structs = null;
        structs = new STRUCT[TOs.size()];
        for (int index = 0; index < TOs.size(); index++) {
            TO to = TO.get(index);
            Object[] params = new Object[2];
            params[0] = fSVDisptchSchedDely.getField1();
            params[1] = fSVDisptchSchedDely.getField2();

            STRUCT struct = new STRUCT(structDescriptor,
            ps.getConnection(), params);
            structs[index] = struct;

        }

        ArrayDescriptor desc = ArrayDescriptor.createDescriptor(
        "TableTypeName", ps.getConnection());
        ARRAY oracleArray = new ARRAY(desc, ps.getConnection(), structs);
        ps.setArray(i, oracleArray);
    }

    // Set the result back to Java object
    public Object getResult(CallableStatement cs, int columnIndex)
    throws SQLException {

        List<TO> TOs= new ArrayList<TO>();
        Object[] structArray = (Object[]) cs.getArray(columnIndex).getArray();
        oracle.sql.STRUCT mystruct = null;
        for (Object structObj : structArray) {
            TO to= new TO();
            mystruct = (oracle.sql.STRUCT) structObj;
            Object[] structAttr = mystruct.getAttributes();
            to.setField1((String)structAttr[0]));
            to.setField2((String)structAttr[1]));            
            TOs.add(to);
        }
        return TOs;
    }
}





Enjoy... :)

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