Search code examples
javalistparametersoracle8i

Pass table of object type from java to oracle 8i


I need to pass a list of objects (like a table of values) from java to oracle 8i.

I am trying with the below database objects in oracle 8i.

Is this possible in 8i?
Please help me with a solution if possible.

create or replace type GROUP_OBJ as object    (          

 GROUP_TYPE VARCHAR2(32), 
 PARAM_1 VARCHAR2(16), 
 PARAM_2 VARCHAR2(16)
);
/


CREATE OR REPLACE
TYPE GROUP_table AS TABLE OF GROUP_OBJ; 
/

CREATE OR REPLACE PROCEDURE GROUP_TABLE_TEST (TABLE_OBJ IN GROUP_table)
IS

BEGIN
    null;
EXCEPTION
   WHEN OTHERS
   THEN
      ROLLBACK;
END GROUP_TABLE_TEST;

Solution

  • I got the answer. I was getting into trouble due to case sensitivity issue. the type name to pass into Structdescriptor and arraydescriptor is case sensitive. when gave the Upper case for both , it worked.

    import java.sql.CallableStatement;
    import java.sql.Connection;
    import java.sql.SQLException;
    
    import oracle.sql.ARRAY;
    import oracle.sql.ArrayDescriptor;
    import oracle.sql.STRUCT;
    import oracle.sql.StructDescriptor;
    
    public class PassTableOfObject {
    
          public static void main(String[] args) throws SQLException {
             Connection conn=FucntionTest.getConnection();
    
                try{
    
                    StructDescriptor itemDescriptor = StructDescriptor
                            .createDescriptor("GROUP_OBJ", conn);
    
                    STRUCT[] structs = new STRUCT[2];
                    for (int index = 0; index < 2; index++)
                    {
    
                        String[] params = new String[2];
                        params[0] = "Group "+index;
                        params[1] = "Param  1 "+index;
                        STRUCT struct = new STRUCT(itemDescriptor, conn, params);
                        structs[index] = struct;
                    }
    
                    ArrayDescriptor desc = ArrayDescriptor.createDescriptor("GROUP_TABLE", conn);
                    ARRAY oracleArray = new ARRAY(desc, conn, structs);
    
    
    
                    CallableStatement cs = null;
                    cs = conn.prepareCall("{call GROUP_TABLE_TEST(?)}");
                    cs.setArray(1, oracleArray);
                    cs.execute();
                    conn.commit();
    
    
                    System.out.println("insert procedure executed successfully");                  
    
                    }catch(SQLException e){
                        e.printStackTrace();        
                    }finally{
                        conn.close();
                    }
          }
    }