Accessing fields and methods of the Java object

Here are more examples of how to call Java methods, pass arguments, and return values.

To create stored procedures/functions for the methods in the Invoice class

  1. Create the following SQL stored procedures to pass arguments to and retrieve return values from the Java methods in the Invoice class:

    -- Invoice.init takes a string argument (Ljava/lang/String;)
    -- a double (D), a string argument (Ljava/lang/String;), and
    -- another double (D), and returns nothing (V)
    CREATE PROCEDURE init( IN arg1 CHAR(50),
                           IN arg2 DOUBLE, 
                           IN arg3 CHAR(50), 
                           IN arg4 DOUBLE) 
    EXTERNAL NAME 
      'Invoice.init(Ljava/lang/String;DLjava/lang/String;D)V' 
    LANGUAGE JAVA; 
    -- Invoice.rateOfTaxation take no arguments ()
    -- and returns a double (D)
    CREATE FUNCTION rateOfTaxation() 
    RETURNS DOUBLE 
    EXTERNAL NAME 
      'Invoice.rateOfTaxation()D' 
    LANGUAGE JAVA; 
    -- Invoice.rateOfTaxation take no arguments ()
    -- and returns a double (D)
    CREATE FUNCTION totalSum() 
    RETURNS DOUBLE 
    EXTERNAL NAME 
      'Invoice.totalSum()D' 
    LANGUAGE JAVA; 
    -- Invoice.getLineItem1Description take no arguments ()
    -- and returns a string (Ljava/lang/String;)
    CREATE FUNCTION getLineItem1Description() 
    RETURNS CHAR(50) 
    EXTERNAL NAME
      'Invoice.getLineItem1Description()Ljava/lang/String;' 
    LANGUAGE JAVA; 
    -- Invoice.getLineItem1Cost take no arguments ()
    -- and returns a double (D)
    CREATE FUNCTION getLineItem1Cost() 
    RETURNS DOUBLE 
    EXTERNAL NAME 
      'Invoice.getLineItem1Cost()D' 
    LANGUAGE JAVA; 
    -- Invoice.getLineItem2Description take no arguments ()
    -- and returns a string (Ljava/lang/String;)
    CREATE FUNCTION getLineItem2Description() 
    RETURNS CHAR(50) 
    EXTERNAL NAME
      'Invoice.getLineItem2Description()Ljava/lang/String;' 
    LANGUAGE JAVA; 
    -- Invoice.getLineItem2Cost take no arguments ()
    -- and returns a double (D)
    CREATE FUNCTION getLineItem2Cost() 
    RETURNS DOUBLE 
    EXTERNAL NAME 
      'Invoice.getLineItem2Cost()D' 
    LANGUAGE JAVA;

    The descriptors for arguments to and return values from Java methods have the following meanings:

    Field type Java data type
    B byte
    C char
    D double
    F float
    I int
    J long
    L class-name; An instance of the class class-name. The class name must be fully qualified, and any dot in the name must be replaced by a /. For example, java/lang/String.
    S short
    V void
    Z Boolean
    [ Use one for each dimension of an array.

    For more information about the syntax of these statements, see CREATE PROCEDURE statement and CREATE FUNCTION statement.

  2. Call the stored procedure that is acting as a wrapper to call the Java method:

    CALL init('Shirt',10.00,'Jacket',25.00);
    SELECT getLineItem1Description() as Item1, 
           getLineItem1Cost() as Item1Cost,
           getLineItem2Description() as Item2, 
           getLineItem2Cost() as Item2Cost,
           rateOfTaxation() as TaxRate, 
           totalSum() as Cost;

    The query returns six columns with values as follows:

    Item1 Item1Cost Item2 Item2Cost TaxRate Cost
    Shirt 10 Jacket 25 0.15 40.25