2013-03-21 136 views
0

我有一類「用戶」如下的對象:
使具有陣列Oracle存儲過程

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


} 

欲用戶類型的對象傳遞給我的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); 

請讓我知道我要去哪裏錯了。

+0

Java和Oracle類型是不等價的。您需要將Java類的元素作爲單獨參數傳遞給您的過程,或者將您的存儲過程寫入Java。 – 2013-03-21 11:21:26

回答

0

首先,我認爲你應該得到一個ArrayOutOfBoundException,因爲你已經聲明瞭一個Object [2]並試圖將第三個元素放入它中。考慮到這是一個錯字。

對於原來的問題,而不是使用的setObject可以使用 * JDBCUtil.setStruct(康涅狄格州,cstmt所,1, 「USER_SEARCH_OBJ」,userProperties); *