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:
Create an
oracle.sql.StructDescriptorobject for the database data type.Create a Java
Objectarray 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.Create a
Structobject using theoracle.sql.STRUCTconstructor, passing the appropriateStructDescriptor,Connection, andObjectarray of objects.Use a
PreparedStatementobject and itssetObject( )method to insert the data represented by theStructobject 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, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access