How to get array of object type parse in java from oracle procedure

4.6k views Asked by At

I have a oracle procedure which return two parameter, 1st is array object type and 2nd is number.

My procedure is :

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

oracle type object :

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

oracle type array :

create or replace type xx_wf_svc_ntf_array is table of xx_wf_svc_ntf_record ;
/

My java code is :

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

I get 2nd out parameter which is return array size. I have many R&D for parsing oracle type array. I got single array parsing which is return by oracle procedure. But I do not know how to parse array oracle type object.

Thanks in advance.

2

There are 2 answers

2
Jorge_B On BEST ANSWER

Being an output parameter, your code can get mangled by any connection pool you are using if you try to use Oracle types and classes. I suggest you read it as a plain java.sql.Array made of instances of java.sql.Struct.

Maybe you can get it to work by simply doing:

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

And then you can simply get the attributes of every Struct in that array.

0
kels On

I have found my answer of my question.

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

            }

    }



}