I want to write a Java program that will insert a row to a table that has a number of nested tables. Following that, I want to insert an unpredictable number of rows to each of these nested tables.
There are lots of examples a PreparedStatement of this sort:
new PreparedStatement("INSERT INTO CONTAINER_TBL (A, B, NESTED_TBL)
VALUES ('X', 'Y',
NESTED_TBL_TYPE(NESTED_ROW_TYPE('Q', 99),
NESTED_ROW_TYPE('R', 999))
)");
This is fine if I know ahead of time how many nested rows I'll need to insert. But what if I don't?
Pass a Java array as a collection:
Oracle 12c Setup:
Java: (Using
ojdbc7.jar
)Oracle Query
Results:
Version with older syntax:
Just passing to and from a dummy query (rather than inserting into the database) to also show how to retrieve an array of objects:
Output:
This compiled for me with
ojdbc6.jar
and worked with Oracle 11gR2. You should find the correctojdbc
version for your database and use that.