0

0

I am trying to return array of an object from an Oracle stored procedure call in Java.

I have created one OBJECT type at DB. This is having 5 attributes

Created a TABLE type of the object.( as I would return more than one object)

I have managed to get the output in the Object array.

My challenges is , I am unable to refer each individual object from the array of objects.

stmt.registerOutParameter(8, Types.ARRAY,"XXXXX.T_LAC_TAB");

stmt.execute();

 ARRAY simpleArray = (ARRAY) stmt.getArray(8);

 //Map map1 = con.getTypeMap();

 //map1.put("XXXXX.T_LAC_TAB_O", ExpectedLacDataType.class);

 //con.setTypeMap(map1);

 System.out.println("Till here");

Object[] values = (Object[])simpleArray.getArray();//working fine

After I got the values array of objects, How I can move forward?

I have tried to create a custom java class and tried to setTypemap to the associated java class, but I am unable to get it working.

I cant paste the details of the code here , but , basically, I just need to know how ,

  1. I can fetch individual object from the returned array of objects and access each attributes of the retrieve objects
Arijit
  • 1
  • 1
  • The arrays appears to be an XY-problem as you've solved that part. For objects, this is probably a duplicate of https://stackoverflow.com/a/54347047/1509264 – MT0 Jun 27 '23 at 20:33
  • hi thanks a lot for replying and I actually tried to use the implementation you used in the above link , before posting my question :-) My issue is, you could register your out params as STRUCT, however as in my case, I am returning more than one row of objects( array of objects), I had to define the type as ARRAY in out param. Can you try to implement your original answer to check how to implement , if your procedure returns more than one row of the same objects. Trust me , I am looking up on so many blogs, but yet to get the final bit ..or the idea – Arijit Jun 28 '23 at 16:44
  • Hi, did you try it ? please let me know. – Arijit Jun 30 '23 at 11:14

1 Answers1

0
        final StructDescriptor structDescriptor = StructDescriptor.createDescriptor("XXXXX.XXX_T", con);
        final ResultSetMetaData metaData = structDescriptor.getMetaData();

        stmt.execute();


        Object[] data = (Object[]) ((Array) stmt.getObject(8)).getArray();
        System.out.println("ata.length"+data.length);

            for (Object tmp : data) {
                if (tmp != null) {
                    System.out.println("Object" + tmp);
                    Struct row = (Struct) tmp;
                    int i = 1;
                    for (Object attribute : row.getAttributes()) {

                        if (metaData.getColumnName(i).equals("CSR_ID"))
                            System.out.println(metaData.getColumnName(i) + " = " + attribute);
                        if (metaData.getColumnName(i).equals("ACCOUNT_NUMBER"))
                            System.out.println(metaData.getColumnName(i) + " = " + attribute);
                        if (metaData.getColumnName(i).equals("IBB"))
                            System.out.println(metaData.getColumnName(i) + " = " + attribute);
                        if (metaData.getColumnName(i).equals("ICR_LOAN_TYPE"))
                            System.out.println(metaData.getColumnName(i) + " = " + attribute);
                        if (metaData.getColumnName(i).equals("ALLOCATION"))
                            System.out.println(metaData.getColumnName(i) + " = " + attribute);
                        if (metaData.getColumnName(i).equals("TOTAL_REPAY"))
                            System.out.println(metaData.getColumnName(i) + " = " + attribute);
                        i++;
                    }
                }
            }
Arijit
  • 1
  • 1