0

我有一个“用户”类,如下所示:

public class User{
    private String firstname;
    private String lastname;
    private String[] departments;


}

我想将 User 类型的对象传递给我的 oracle 存储过程,该存储过程将对象类型作为参数。所以我这样做是为了实现我的解决方案:-

1. Created USER_SEARCH_OBJ as a type in database as follows:-
      create or replace
        TYPE USER_SEARCH_OBJ AS OBJECT (
              FIRST_NAME VARCHAR2(256),
              LAST_NAME VARCHAR2(256), 
              DEPARTMENTS TABLE_OF_VALUES,
        );

2. Created TABLE_OF_VALUES as a type in database as follows:-
      create or replace TYPE TABLE_OF_VALUES AS TABLE OF VARCHAR2(20);

3. Passed the User object from Java class as follows:-
       Object[] departments = {"1","2"};
       StructDescriptor objDescriptor = StructDescriptor.createDescriptor("USER_SEARCH_OBJ", conn.getMetaData().getConnection());
       ArrayDescriptor arrayDescriptor = ArrayDescriptor.createDescriptor("TABLE_OF_VALUES", conn.getMetaData().getConnection());
       ARRAY departmentArr = new ARRAY(arrayDescriptor, conn.getMetaData().getConnection(), departments );
       Object[] userProperties = new Object[2];
       userProperties [0] = "paras";//first_name
       userProperties [1] = "anand";//last_name
       userProperties [2] = departmentArr ;//department array
       STRUCT searchObj = new STRUCT(objDescriptor, conn.getMetaData().getConnection(), userProperties );
       CallableStatement cStmt = conn.prepareCall("PCK_SEARCH2.USER_SEARCH(?,?)");
       cStmt.setObject(1, searchObj);
       cStmt.registerOutParameter(2, OracleTypes.CURSOR);
       cStmt.execute();

但是当我运行这段代码时,我得到一个异常如下: -

java.sql.SQLException: Inconsistent java and sql object types
    at oracle.sql.StructDescriptor.toOracleArray(StructDescriptor.java:709)
    at oracle.sql.StructDescriptor.toArray(StructDescriptor.java:1296)
    at oracle.sql.STRUCT.<init>(STRUCT.java:165)
    at com.ensenda.lmp.web.controller.User.main(User.java:75)

此异常出现在以下代码行:-

STRUCT searchObj = new STRUCT(objDescriptor, conn.getMetaData().getConnection(), userProperties );

请让我知道我哪里出错了。

4

1 回答 1

0

首先我认为你应该得到一个ArrayOutOfBoundException因为你已经声明了一个 Object[2] 并试图将第三个元素放入其中。考虑到这是一个错字。

对于原始问题而不是使用 setObject 您可以使用 *JDBCUtil.setStruct(conn,cStmt,1,"USER_SEARCH_OBJ",userProperties);*

于 2014-03-25T06:16:20.187 回答