User's Guide
PART 3. Java in the Database
CHAPTER 17. Using Java in the Database
You may wish to retrieve a Java column value in either of the following ways:
Retrieve the entire object.
Retrieve some of the fields of the object.
From SQL, you can create a variable of the appropriate type, and select the value from the object into that variable. However, the obvious place in which you may wish to make use of the entire object is in a Java application.
You can retrieve an object into a server-side Java class by using the getObject method of the ResultSet of a query. You can also retrieve an object to a client-side Java application, although the method is more complicated.
For a description of retrieving objects using JDBC, see Queries using JDBC.
Individual fields of an object have data types that correspond to SQL data types, using the SQL to Java data type mapping described in Java / SQL data type conversion.
You can retrieve individual fields by including them in the select-list of a query, as in the following simple example:
SELECT JProd>>unit_price FROM product WHERE ID = 400
If you use methods to set and get the values of your fields, as is common in object oriented programming, you can include a getField method in your query:
SELECT JProd>>getName() FROM Product WHERE ID = 401
For information on using objects in the WHERE clause and other issues in comparing objects, see Comparing Java fields and objects.
You can list the column name in a query select list, as in the following query:
SELECT JProd FROM jdba.product
When you do this, you do not retrieve the object from each column. Instead, the returned value of the object's toString method is returned. For the Product class, the toString method lists, in one string, the size, name, and unit price of the object. The results of the query are as follows:
JProd |
---|
Small Tee Shirt: 9.00 |
Medium Tee Shirt: 14.00 |
One size fits all Tee Shirt: 14.00 |
One size fits all Baseball Cap: 9.00 |
One size fits all Baseball Cap: 10.00 |
One size fits all Visor: 7.00 |
One size fits all Visor: 7.00 |
Large Sweatshirt: 24.00 |
Large Sweatshirt: 24.00 |
Medium Shorts: 15.00 |