简体   繁体   中英

Call procedure with a table type. Exception while create Struct: java.lang.AbstractMethodError: oracle.jdbc.driver.T4CConnection.createStruct

I try to create array of Struct elements to pass it to Oracle procedure. When I try to set first element of the array I receive an Exception:

java.lang.AbstractMethodError: oracle.jdbc.driver.T4CConnection.createStruct

What might be the reason and how to correct this? My example:

import java.sql.Array;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Struct;

public class TypesApp {
    public static void main(String[] args) throws Exception {
        Class.forName("oracle.jdbc.OracleDriver");
        Connection connection =
                DriverManager.getConnection("jdbc:oracle:thin:@10.0.0.20:1521:dw", "user", "password");

    final String typeName = "T_DEMO_OBJECT";
    final String typeTableName = "T_DEMO_OBJECTS";

    Struct[] structs = new Struct[2];

    //Here I get the Exception
    structs[0] = connection.createStruct(typeName, new Object[]{23, "Testobject 1"});
    structs[1] = connection.createStruct(typeName, new Object[]{40, "Testobject 2"});

    CallableStatement callableStatement = connection.prepareCall("{call p_receive_demo_objects(?)}");

    Array dataArray = connection.createArrayOf(typeTableName, structs);
    callableStatement.setArray(1, dataArray);

    callableStatement.executeUpdate();
}

}

My previous version of the same operation works (with ojdbc6), however it saves numbers to database correctly but all strings are empty, so I try to find another way. Previous version:

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

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

public class InitMethod2 {

    public static void main(String[] args) throws Exception {
        Class.forName("oracle.jdbc.OracleDriver");
        Connection connection =
                DriverManager.getConnection("jdbc:oracle:thin:@10.0.0.20:1521:dw", "user", "password");

        final String typeName = "T_DEMO_OBJECT";
        final String typeTableName = "T_DEMO_OBJECTS";

        final StructDescriptor structDescriptor = StructDescriptor.createDescriptor(typeName.toUpperCase(), connection);

        final ArrayDescriptor arrayDescriptor =
                ArrayDescriptor.createDescriptor(typeTableName.toUpperCase(), connection);
        final ARRAY demoObjectsFromJava = new ARRAY(arrayDescriptor, connection, new STRUCT[]{
                // STRUCTS are created with the struct descriptor and a generic object array containing the values of the
                // attributes of the T_DEMO_OBJECT
                new STRUCT(structDescriptor, connection, new Object[]{23123, "Testobject 1"}),
                new STRUCT(structDescriptor, connection, new Object[]{42123, "Testobject 2"})});
        CallableStatement cs = connection.prepareCall("{call p_receive_demo_objects(?)}");
        cs.setObject(1, demoObjectsFromJava, Types.ARRAY);
        cs.execute();
        cs.close();

        connection.close();
    }

}

My pom.xml has the following dependency: 在此处输入图片说明

It was due to ojdbc14.jar . When I change it to ojdbc6 (11.2.0.3) the exception is gone. However in this case you should change method createArrayOf() to this one:

import oracle.jdbc.OracleConnection;

Array dataArray = ((OracleConnection)connection).createOracleArray(typeTableName, structs);

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