Contents Index Creating a SQL variable of type Invoice Using Java in the Database

ASA Programming Guide
  Introduction to Java in the Database
    Tutorial: A Java in the database exercise

Access fields and methods of the Java object


If a variable (or column value in a table) contains a reference to a Java object, then the fields of the object can be passed values and its methods can be invoked.

For example, the variable of type Invoice that you created in the previous section contains a reference to an Invoice object and has four fields, the value of which can be set using SQL statements.

To access fields of the Invoice object

  1. From Interactive SQL, execute the following SQL statements to set field values for the variable Inv.

    SET Inv.lineItem1Description = 'Work boots';
    SET Inv.lineItem1Cost = '79.99';
    SET Inv.lineItem2Description = 'Hay fork';
    SET Inv.lineItem2Cost = '37.49';

    Each SQL statement passes a value to a field in the Java object referenced by Inv.

  2. Execute SELECT statements against the variable. Any of the following SQL statements return the current value of a field in the Java object referenced by Inv.

    SELECT Inv.lineItem1Description;
    SELECT Inv.lineItem1Cost;
    SELECT Inv.lineItem2Description;
    SELECT Inv.lineItem2Cost;
  3. Use a field of the Inv variable in a SQL expression.

    Execute the following SQL statement:

    SELECT * FROM PRODUCT
    WHERE unit_price < Inv.lineItem2Cost;

In addition to having public fields, the Invoice class has one instance method, which you can invoke

To invoking methods of the Invoice object

Calling methods versus referencing fields 

Method names are always followed by parentheses, even when they take no arguments. Field names are not followed by parentheses.

The totalSum() method takes no arguments, but returns a value. The brackets are used because a Java operation is being invoked even though the method takes no arguments.

For Java in the database, direct field access is faster than method invokation. Accessing a field does not require the Java VM to be invoked, while invoking a method requires the VM to execute the method.

As indicated by the Invoice class definition outlined at the beginning of this section, the totalSum instance method makes use of the class method rateOfTaxation.

You can access this class method directly from a SQL statement.

SELECT Invoice.rateOfTaxation();

Notice the name of the class is used, not the name of a variable containing a reference to an Invoice object. This is consistent with the way Java handles class methods, even though it is being used in a SQL statement. A class method can be invoked even if no object based on that class has been instantiated.

Class methods do not require an instance of the class to work properly, but they can still be invoked on an object. The following SQL statement yields the same results as the previously executed SQL statement.

SELECT Inv.rateOfTaxation();

Contents Index Creating a SQL variable of type Invoice Using Java in the Database