2014-02-10 2 views
0

두 개의 매개 변수를 반환하는 오라클 절차가 있습니다. 첫째는 배열 객체 유형이고 두 번째는 숫자입니다.오라클 프로 시저에서 자바에서 객체 유형의 배열을 얻는 방법

내 절차는 다음과 같습니다

CREATE OR REPLACE PROCEDURE APPS.xx_push_notification (
    p_user_name  IN   VARCHAR2, 
    p_hr_type   IN   VARCHAR2, 
    p_ret_array   OUT NOCOPY XX_WF_SVC_NTF_ARRAY, 
    p_ret_array_size  OUT NOCOPY NUMBER 
) 
AS 
    --l_item_type IN VARCHAR2,l_message_names IN VARCHAR2, 
    l_orig_system  VARCHAR2 (4000); 
    l_orig_system_id NUMBER; 
    l_ret_array  xx_wf_svc_ntf_array; 
    l_ret_array_size NUMBER; 
    l_item_type  VARCHAR2 (1000); 
    l_message_names VARCHAR2 (4000); 
    l_latest_ntf_id NUMBER; 


    CURSOR cur_hrms (
     cur_orig_system     VARCHAR2, 
     cur_orig_system_id     NUMBER 
    ) 
    IS 
     ...................... 
     ...................... 
     ...................... 
BEGIN 

     ...................... 
     ...................... 
     ...................... 
    p_ret_array := xx_wf_svc_ntf_array(); 

    IF p_hr_type = 'HR_ABSENCES' 
    THEN 
     FOR c1_rec IN cur_hrms (l_orig_system, l_orig_system_id) 
     LOOP 

     p_ret_array.EXTEND; 
     p_ret_array (p_ret_array.COUNT) := 
      xx_wf_svc_ntf_record (c1_rec.NOTIFICATION_ID, 
            c1_rec.CONTEXT, 
            c1_rec.FROM_USER, 
            c1_rec.TO_USER, 
            c1_rec.SUBTYPE); 
     END LOOP; 
     p_ret_array_size := p_ret_array.COUNT; 
    END IF; 
END; 
/

오라클 형 개체 :

create or replace type xx_wf_svc_ntf_record is object (NOTIFICATION_ID NUMBER, 
         CONTEXT   VARCHAR2(2000), 
         FROM_USER   VARCHAR2(320), 
         TO_USER   VARCHAR2(320), 
         SUBJECT   VARCHAR2(2000), 
         SUBTYPE   VARCHAR2(32)); 
/

오라클 형 배열 :

create or replace type xx_wf_svc_ntf_array is table of xx_wf_svc_ntf_record ; 
/

내 자바 코드는 다음과 같습니다

....... 
....... 
db = new DBConnectionManager(); 
conn=db.getConnection(); 
if(conn!=null) 
{ 
     cstmt = conn.prepareCall("{call xx_push_notification(?, ?, ?, ?)}"); 
     cstmt.setString(1, UserName); 
     cstmt.setString(2, NotificationType); 
     cstmt.registerOutParameter(3, OracleTypes.ARRAY,typeTableName); 
     cstmt.registerOutParameter(4, Types.INTEGER); 
     cstmt.execute(); 



     int newRecord=cstmt.getInt(4); 
      System.out.println("Total New Record : "+newRecord); 
} 
......... 
......... 
,536,913,632 10

반환 배열 크기 인 2nd out 매개 변수를 가져옵니다. 나는 오라클 타입 배열을 파싱하기 위해 많은 R & D를 가지고있다. 난 오라클 절차에 의해 반환됩니다 단일 배열 파싱있어. 하지만 배열 오라클 유형의 객체를 구문 분석하는 방법을 모르겠습니다.

미리 감사드립니다.

답변

1

출력 매개 변수가 있기 때문에 Oracle 유형 및 클래스를 사용하려고하면 사용중인 모든 연결 풀에서 코드가 변형 될 수 있습니다. java.sql.Struct 인스턴스로 구성된 일반 java.sql.Array으로 읽으시기 바랍니다.

java.sql.Struct records[] = 
    (java.sql.Struct[]) ((java.sql.Array) cstmt.getObject(3)).getArray() 

을 그리고 당신은 단순히 해당 배열의 모든 Struct의 속성을 얻을 수 있습니다 :

아마 당신은 단순히 수행하여 작업 할 수 있습니다.

+0

아무런 아이디어가 없습니다. 나는 java.sql.Struct에 대해 연구하고있다. – kels

+0

코드를 디버그하고'cstmt.getObject (3)'이 리턴하는 것을 볼 수 있습니다. 어떤면에서, java.sql.Array를 구현하는 것이면, 내가 제안한 코드를 사용해 볼 수있다. JDBC 드라이버는 항상'java.sql. *'인터페이스를 구현하는 것을 돌려 주어야하기 때문에 (또는 이와 비슷한) 어쨌든 작동해야합니다. –

0

내 질문에 대한 답을 찾았습니다.

package Mobile.test; 

import java.sql.Array; 
import java.sql.CallableStatement; 
import java.sql.Connection; 
import java.sql.ResultSet; 
import java.sql.ResultSetMetaData; 
import java.sql.SQLException; 
import java.sql.Statement; 
import java.sql.Struct; 
import java.sql.Types; 
import java.util.ArrayList; 

import oracle.sql.StructDescriptor; 

import com.db.DBConnectionManager; 
import com.test.PushNotification; 


public class SentNotification implements Runnable { 

    Connection conn =null; 
    ResultSet rset = null; 
    Statement stmt = null; 
    CallableStatement cstmt=null; 
    ArrayList<String> aryNotificationType = null; 
    DBConnectionManager db; 


    String UserName=null; 
    String NotificationType=null; 
    final String typeName = "xx_test"; 
    final String typeTableName = "xx_test_table"; 


    public SentNotification(String UserName,String NotificationType) 
    { 

     this.UserName=UserName; 
     this.NotificationType=NotificationType; 

    } 


    @Override 
    public void run() { 
     // TODO Auto-generated method stub 

     try 
     { 
      db = new DBConnectionManager(); 
      conn=db.getConnection(); 



      if(conn!=null) 
       { 
        final StructDescriptor structDescriptor = StructDescriptor.createDescriptor(typeName.toUpperCase(), conn);  
        final ResultSetMetaData metaData = structDescriptor.getMetaData(); 

        cstmt = conn.prepareCall("{call xx_push_test(?, ?, ?, ?)}"); 
        cstmt.setString(1, UserName); 
        cstmt.setString(2, NotificationType); 
        cstmt.registerOutParameter(3, Types.ARRAY, typeTableName.toUpperCase()); 
        cstmt.registerOutParameter(4, Types.INTEGER); 
        cstmt.execute(); 



        int newRecord=cstmt.getInt(4); 
        System.out.println("Total New Record : "+newRecord); 
        if(newRecord > 0) 
        { 

         Object[] data = (Object[]) ((Array) cstmt.getObject(3)).getArray(); 
         for(Object tmp : data) 
         { 
          Struct row = (Struct) tmp; 
          int i = 1; 
          for(Object attribute : row.getAttributes()) 
          {    
           if(metaData.getColumnName(i).equals("NOTIFICATION_ID")) 
            System.out.println(metaData.getColumnName(i) + " = " + attribute);           
           ++i; 
          } 
         } 
          PushNotification pn = new PushNotification(); 
          pn.sendPushNotification(UserName,""+newRecord,NotificationType); 
          System.out.println(UserName+" you have " + newRecord+" "+NotificationType); 
        } 

       } 
      else 
       { 
        System.out.println("Connecation is Null"); 

       } 


     } 
     catch (SQLException e) 
      { 
       e.printStackTrace(); 
      } 
     finally 
      { 
       try 
        { 
         cstmt.close(); 
         conn.close(); 


        } 
       catch (SQLException e) 
        { 
         e.printStackTrace(); 
         System.out.println("issue"+e); 
        } 

      } 

    } 



} 
관련 문제