The SQLData Interface

The java.sql.SQLData interface allows you to create custom Java classes that mirror your user-defined database types. But, as my mother-in-law would say, “What do you get for that?” If you haven’t used an object database before, using a database to store objects, that is, both data and methods, requires a shift in your thinking. Instead of just modeling the data around, and establishing relationships between, different things, you can complete the puzzle by including a thing’s behavior. When you create a user-defined data type in the database, you can also include methods for its behaviors. You can continue to use relational SQL and retrieve the object data as though it were in tables, and execute object methods as though they were separate stored procedures, but with the SQLData interface, you don’t have to. Instead, you can create a Java object that will mimic your database object and retrieve an object directly from the database into your Java program as an object. There is no longer any need to do any relational-to-object mapping in your Java program. Now you can use objects.

When you use SQLData, follow these steps:

  1. Create custom Java classes to represent database user-defined data types.

  2. Add the custom Java classes to the Connection object’s type map.

  3. For insert and update operations, use a PreparedStatement object with an appropriately formulated SQL statement.

  4. Use the getObject( ) or setObject( ) accessor methods to get and set the object values as needed. ...

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.