2014-02-10 31 views
0

我有一个oracle程序返回两个参数,第一个是数组对象类型,第二个是数字。如何从oracle程序中获取java中的对象类型解析数组

我的程序是:

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 ; 
/

我的Java代码是:

....... 
....... 
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); 
} 
......... 
......... 

我得到2nd返回参数是返回数组大小。我有很多R & D用于解析oracle数组。我得到了单个数组解析,这是由oracle程序返回的。但我不知道如何解析数组oracle对象。

在此先感谢。

回答

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); 
        } 

      } 

    } 



} 
相关问题