Calling methods in the Java class

To access the Java methods in the class, you must create stored procedures or functions that act as wrappers for the methods in the class.

 Call a Java method using Interactive SQL
  1. Create the following SQL stored procedure to call the Invoice.main method in the sample class:

    CREATE PROCEDURE InvoiceMain( IN arg1 CHAR(50) )
    EXTERNAL NAME 'Invoice.main([Ljava/lang/String;)V'
    LANGUAGE JAVA;

    This stored procedure acts as a wrapper to the Java method.

  2. Call the stored procedure to call the Java method:

    CALL InvoiceMain('to you');

    If you examine the database server message log, you see the message "Hello to you" written there. The database server has redirected the output there from System.out.

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

 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;
    
    -- The Java methods below take no arguments and return a double (D)
    -- or a string (Ljava/lang/String;)
    
    CREATE FUNCTION rateOfTaxation() 
    RETURNS DOUBLE 
    EXTERNAL NAME 'Invoice.rateOfTaxation()D' 
    LANGUAGE JAVA;
    
    CREATE FUNCTION totalSum() 
    RETURNS DOUBLE 
    EXTERNAL NAME 'Invoice.totalSum()D' 
    LANGUAGE JAVA;
    
    CREATE FUNCTION getLineItem1Description() 
    RETURNS CHAR(50) 
    EXTERNAL NAME 'Invoice.getLineItem1Description()Ljava/lang/String;' 
    LANGUAGE JAVA;
    
    CREATE FUNCTION getLineItem1Cost() 
    RETURNS DOUBLE 
    EXTERNAL NAME 'Invoice.getLineItem1Cost()D' 
    LANGUAGE JAVA;
    
    CREATE FUNCTION getLineItem2Description() 
    RETURNS CHAR(50) 
    EXTERNAL NAME 'Invoice.getLineItem2Description()Ljava/lang/String;' 
    LANGUAGE JAVA;
    
    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.
  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
 See also