4.4. Accessing Database Objects Using SQLJ

Now that you've learned about database objects and the custom Java classes, I'll show you how to add, retrieve, and modify column and row objects using SQLJ. The examples in this section use the customers and customers2 tables. At the end of this section is a complete program illustrating the use of the SQLJ statements in the examples.

4.4.1. Adding Objects

There are two types of database objects: column objects and row objects. First, I'll discuss adding column objects to the customers table using SQLJ statements. Then, I'll discuss adding row objects to the customers2 table, also using SQLJ statements.

4.4.1.1. Adding a column object

You can add a column object to a table using one of two methods:

  • Use the object type constructor to set the object attributes in an INSERT statement.

  • Use a host object in an INSERT statement. This method involves three steps:

    1. Create a host object using the appropriate custom class.

    2. Set the host object attributes using the set mutator methods.

    3. Use the host object in an INSERT statement.

The following example uses the first method (using the object type's constructor) to add a customer column object to the customers table:

#sql {
  INSERT INTO
    customers
  VALUES (
    t_customer(3, 'Jerry', 'Fieldtop', '01-JAN-1965', '650-555-1222',
      t_address('1 Any Street', 'Any Town', 'CA', '12345')
    )
  )
};

The second method (using host variables) is a bit more involved. To begin with, you need to create a host object. The ...

Get Java Programming with Oracle SQLJ 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.