2014-10-01 109 views
5

我有一個存儲過程,它看起來像從Java訪問存儲過程%ROWTYPE

PROCEDURE get_curx( p_buf  IN ni_imsi%ROWTYPE, 
         p_bufx IN ni_imsi%ROWTYPE, 
         p_cur  OUT CurTyp, 
         p_where IN VARCHAR2 DEFAULT '', 
         p_orderby IN VARCHAR2 DEFAULT '', 
         p_max  IN NUMBER DEFAULT 0, 
         p_lock IN NUMBER DEFAULT 0, 
         p_hint IN VARCHAR2 DEFAULT 'NONE', 
         p_debug IN NUMBER DEFAULT 0, 
         p_count IN BOOLEAN DEFAULT FALSE); 

我打電話從Java程序這個過程是這樣

CallableStatement cs = connection.prepareCall("{call ni_imsi_pkg.get_curx(?,?,?,?,?,?)}"); 

      cs.setObject(1, ?); // i have no clue what to mention here 
      cs.setObject(2, ?); //i have no clue what to mention here 

      cs.registerOutParameter(3, OracleTypes.CURSOR); 
      cs.setString(4, " WHERE current_state = 60000 AND rownum <= 2 "); 
      cs.setString(5, " ORDER BY imsi_number"); 
      cs.setInt(6, 5); 

但我不知道如何設置前兩個參數。請幫助我。謝謝

+0

你想要在這些變量中放置什麼,或者更確切地說它們來自哪裏以及它們在Java端是什麼類型的對象? – 2014-10-01 13:37:36

+0

我懷疑你可以從JDBC調用它。 RowType是PL/SQL特定的。它可能在某個地方被定義在軟件包中。您可能需要在PL/SQL中創建/調用一個幫助程序,並使用適當的參數爲您調用此類。 – Mike 2014-10-01 14:04:10

回答

4

正如Mike所說,由於行類型只在PL/SQL中有效,驅動程序使用的所有類型必須在SQL級別定義,因此不能在JDBC調用中直接引用行類型。

您可以定義自己的SQL對象類型,它會影響您的表結構(如果表更改,您必須記住更新),以及一個包含該類型並將其轉換爲對您的調用的包裝過程真正的程序。這是基於雙演示,因爲我不知道你的真實的表結構:

create type ni_imsi_rowtype as object (dummy varchar2(1)) -- use your real table's columns/types 
/

create package ni_imsi_pkg as 
    procedure get_curx(p_buf dual%rowtype, p_cur out sys_refcursor); 
    procedure get_curx_wrapper(p_buf ni_imsi_rowtype, p_cur out sys_refcursor); 
end ni_imsi_pkg; 
/

create package body ni_imsi_pkg as 
    -- original procedure, simplified for demo 
    procedure get_curx(p_buf dual%rowtype, p_cur out sys_refcursor) is 
    begin 
    open p_cur for select * from dual where dummy = p_buf.dummy; 
    end; 

    -- wrapper procedure taking new type instead of rowtype 
    procedure get_curx_wrapper(p_buf ni_imsi_rowtype, p_cur out sys_refcursor) is 
    l_buf dual%rowtype; 
    begin 
    l_buf.dummy := p_buf.dummy; 
    get_curx(l_buf, p_cur); 
    end; 
end ni_imsi_pkg; 
/

然後在Java端,你可以填寫併發送這個作爲一個結構:

// Object array containing the values corresponding to your row type 
Object[] rowObj = { "X" }; 
// Struct based on the SQL type you created 
StructDescriptor structDesc = StructDescriptor.createDescriptor("NI_IMSI_ROWTYPE", conn); 
STRUCT rowStruct = new STRUCT(structDesc, conn, rowObj); 

// Call wrapper function instead of real one 
cs = conn.prepareCall("{ call ni_imsi_pkg.get_curx_wrapper(?,?) }"); 
// Pass the struct defined earlier 
cs.setObject(1, rowStruct); 
cs.registerOutParameter(2, OracleTypes.CURSOR); 
// and other arguments for your real calll 

如果您不能修改你的真實包裝,那麼你可以創建一個新的包裝,或一個簡單的過程;或者,你甚至可以做一個匿名塊轉換,儘管這使得Java代碼更復雜:

cs = (OracleCallableStatement) conn.prepareCall(
    "declare l_typ ni_imsi_rowtype; l_buf dual%rowtype; " 
     + "begin l_typ := ?; l_buf.dummy := l_typ.dummy; ni_imsi_pkg.get_curx(l_buf, ?); " 
     + "end;" 
); 

...還結合相同的結構等等,仍需要SQL類型。只有聲明發生了變化,但現在可以在沒有包裝的情況下調用原始過程。