DB2® for Linux, UNIX, and Windows stored procedures can have parameters of the ARRAY of ROW type. To retrieve or update data in those parameters in JDBC applications, you use arrays of Struct objects.
To retrieve or update data in ARRAY of ROW parameters, follow these steps:
CREATE TYPE MYTYPE.PERSON_T AS ROW
ID INTEGER
FIRSTNAME VARCHAR(20)
LASTNAME VARCHAR(20)
SALARY INTEGER
CREATE TYPE MYTYPE.PEOPLE_T AS MYTYPE.PERSON_T ARRAY[10]
CREATE PROCEDURE MYSP.PEOPLE_SP
(IN PIN MYTYPE.PEOPLE_T, OUT POUT MYTYPE.PEOPLE_T)
LANGUAGE SQL
BEGIN
…
END
The following code calls stored procedure MYSP.PEOPLE_SP,
and uses arrays of Struct objects to assign data
to ARRAY of ROW parameter PIN and to retrieve data from ARRAY of ROW
parameter POUT. The numbers to the right of selected statements correspond
to the previously described steps.Connection con;
CallableStatement cstmt;
…
peopleElements = new java.sql.Struct[2];
personAttributes = new Object[] {
new Integer(1), "John", "Doe", new Integer(60000)
};
peopleElements[0] =
con.createStruct("MYTYPE.PERSON_T", personAttributes); 1
personAttributes = new Object[] {
new Integer(2), "Jane", "Doe", new Integer(65000)
};
peopleElements[1] =
con.createStruct("MYTYPE.PERSON_T", personAttributes);
Array people = con.createArrayOf("MYTYPE.PERSON_T", peopleElements); 2
cstmt = con.prepareCall("CALL MYSP.PEOPLE_SP(?,?)"); 3
cstmt.setArray(1, people); 4
cstmt.registerOutParameter(2, java.sql.Types.ARRAY); 5
cstmt.execute(); 6
java.sql.Array people = cstmt.getArray(2); 7
java.sql.Struct[] persons =
(java.sql.Struct[])people.getArray(); 8
for (int i = 0; i < persons.length; i++) { 9
java.sql.Struct person = persons[i];
Object[] personAttributes = person.getAttributes();
Integer id = (Integer)personAttributes[0];
String firstName = (String)personAttributes[1];
String lastName = (String)personAttributes[2];
Integer salary = (Integer)personAttributes[3];
}