java.sql.SQLException:无法转换为内部表示:将ArrayList传递给Oracle.sql.ARRAY

时间:2014-11-21 06:24:32

标签: java sql arrays oracle arraylist

嗨,这是我的代码

List<String> listA = new ArrayList<String>(0);
String[] arrA = new String[100];
listA.add("aaa");
listA.add("bbb");
arrA[0]="aaa";
arrA[1]="bbb";
conn = DriverManager.getConnection(URL, USER, PASS);
oracle.jdbc.OracleConnection oraConn = (oracle.jdbc.OracleConnection) conn;
ArrayDescriptor des = ArrayDescriptor.createDescriptor("ARRAY_TABLE", oraConn);
//*****************
ARRAY array_to_pass1 = new ARRAY(des, conn, arrA );//* exception
CallableStatement cstm = (OracleCallableStatement) conn.prepareCall("{call xyz.procedure1(?,?)}");
cstm.setArray(1, array_to_pass1);
cstm.setInt(2, 1);

这工作正常,但是当我将listA而不是arrA传递给ARRAY构造函数时..它正在抛出sql异常..我想传递Arraylist而不是string [] array

3 个答案:

答案 0 :(得分:2)

在列

之前将列表转换为数组
ARRAY array_to_pass1 = new ARRAY(des, conn, arrA );

您可以在此之前添加/删除列表中的项目。

String[] arrA= new String[listA.size()];
arrA= listA.toArray(arrA);

答案 1 :(得分:1)

Oracle关于ARRAY的文档不完整,我发现它在所有地方都传递了string []数组。

所以我建议您将ArrayList更改为String []数组:

String[] arrA = listA.toArray(new String[listA.size()]);  

答案 2 :(得分:1)

尝试这种方法

List list = new ArrayList();
callableStatement = 
          (OracleCallableStatement)connection.prepareCall("{call yourprocedure(?)}");
            StructDescriptor structDescriptor = 
                StructDescriptor.createDescriptor("TYPE_ARRAY", 
                                                  connection);
            STRUCT[] structs = new STRUCT[list.size()];
            for (int i = 0; i < list.size(); ++i) {

                Yourclass str = 
                    (Yourclass)list.get(i);
                Object[] objects =

                    new Object[] { str.getProdId(), 
                                   str.getName()
                                 };
                STRUCT struct = 
                    new STRUCT(structDescriptor, connection, objects);
                structs[i] = struct;
            }
            ArrayDescriptor arrayDescriptor = 
                ArrayDescriptor.createDescriptor("ARRAY_MY", 
                                                 connection);

            ARRAY array = new ARRAY(arrayDescriptor, connection, structs);
            callableStatement.setArray(1, array);
            callableStatement.executeUpdate();