2010-08-06 84 views
3

甲骨文對象我有這樣的Oracle對象:如何映射JDBC

CREATE OR REPLACE type employee_obj 
    AS 
     object (
     id NUMBER(10) , 
     ... 
    ) 

存儲過程

function get_employee_obj() return employee_obj is 
     l_employee  employee_obj; 
    begin 
     ... 
     return l_employee; 
    end; 

,我需要從Java代碼調用它:

final String QUERY = "begin ? := GET_EMPLOYEE_OBJ(); end;"; 
    Connection connection = getConnection(); 

    CallableStatement stmt = connection.prepareCall(QUERY); 
    stmt.registerOutParameter(1, <WHAT TO PUT HERE>); 

    stmt.execute(); 
    ResultSet rs = (ResultSet) stmt.getObject(1);  
    ... 

什麼SQL或Oracle類型我需要指定爲registerOutParameter參數從存儲函數讀取對象?我嘗試了幾次,但總是得到了PLS-00382:表達式是錯誤的類型錯誤。謝謝!

+0

也許這將有助於與「se從雙重「get_employee_obj()」:http://www.oracle.com/technology/sample_code/tech/java/sqlj_jdbc/files/advanced/ObjectOracleSample/ObjectOracleSample.java.html – 2010-08-06 09:52:37

+0

http://download.oracle.com/文檔/ CD/B19306_01/java.102/b14355/oraoot.htm#g1104293 – 2010-08-06 18:41:13

回答

3

Oracle對象的正確類型是java.sql.Types.STRUCToracle.jdbc.OracleTypes.STRUCT,具體取決於您所需的支持級別。

但是擺脫錯誤PLS-00382: expression is of wrong type的訣竅是指定模式類型名稱。它應該拼寫爲,全部大寫,否則您將獲得invalid name pattern例外。

鑑於你的榜樣,這樣的事情可能是適當的:具有execute

stmt.registerOutParameter(1, OracleTypes.STRUCT, "EMPLOYEE_OBJ"); 

stmt.registerOutParameter(1, Types.STRUCT, "EMPLOYEE_OBJ"); 

後倒是查詢,您可以訪問結果爲:

STRUCT result = (oracle.sql.STRUCT)stmt.getObject(1); 
Object[] attr = result.getAttributes(); 
// attr[0] if the first field 
// attr[1] the second 
// and so on