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:
Create custom Java classes to represent database user-defined data types.
Add the custom Java classes to the
Connectionobject’s type map.For insert and update operations, use a
PreparedStatementobject with an appropriately formulated SQL statement.Use the
getObject( )orsetObject( )accessor methods to get and set the object values as needed. ...
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