使用表类型调用过程。创建Struct时出现异常:java.lang.AbstractMethodError:oracle.jdbc.driver.T4CConnection.createStruct

时间:2018-06-04 16:04:06

标签: java oracle struct

我尝试创建Struct元素数组以将其传递给Oracle过程。当我尝试设置数组的第一个元素时,我收到一个异常:

  

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

可能是什么原因以及如何纠正这个问题? 我的例子:

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

}

我以前的相同操作版本(使用ojdbc6),但它正确地将数字保存到数据库但所有字符串都是空的,所以我尝试找到另一种方法。以前的版本:

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

}

我的pom.xml具有以下依赖关系: enter image description here

1 个答案:

答案 0 :(得分:0)

这是由于 ojdbc14.jar 。当我将其更改为 ojdbc6(11.2.0.3)时,异常消失了。 但是在这种情况下,您应该将方法createArrayOf()更改为此方法:

import oracle.jdbc.OracleConnection;

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