Structs

You use a java.sql.Struct object to insert object data into the database, update it, or select object data from the database. A Struct object represents a database object as a record of Object attributes. If the database object consists of objects within objects, then a given attribute that represents another database object type will itself need to be cast to another Struct for as many levels as are needed to resolve all the attributes. Let’s start our detailed look at using a Struct with inserting object values into the database.

Inserting Object Values

There are four steps to inserting an object into the database using a Struct object:

  1. Create an oracle.sql.StructDescriptor object for the database data type.

  2. Create a Java Object array with the same number of elements as there are attributes in the database data type and populate it with Java objects of the appropriate data type.

  3. Create a Struct object using the oracle.sql.STRUCT constructor, passing the appropriate StructDescriptor, Connection, and Object array of objects.

  4. Use a PreparedStatement object and its setObject( ) method to insert the data represented by the Struct object into the database.

Let’s look at these steps in detail.

Creating a StructDescriptor

The java.sql.Struct interface, being rather new, supports only selecting objects from a database into a Struct object. The interface does not support creating a Struct object in a Java program and using it to store the data it in the database. The missing functionality, ...

Get Java Programming with Oracle JDBC now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.