Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SAP Sybase SQL Anywhere 16.0 » SQL Anywhere Server - Programming » Java in the database » Tutorial: Using Java in the database

 

Lesson 4: Calling methods in a 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.

Prérequis

Complete the steps in Lessons 1, 2, and 3 before attempting Lesson 4.

This lesson assumes that you have the roles and privileges listed in the Privileges section at the start of this tutorial: Tutorial: Using Java in the database.

Contexte et remarques

The Java class file containing the compiled methods from the Invoice example has been loaded into the database.

 Task
  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.

  3. The following stored procedures illustrate how to pass arguments to and retrieve return values from the Java methods in the Invoice class. If you examine the Java source code, you see that the init method of the Invoice class takes both string and double arguments. String arguments are specified using Ljava/lang/String;. Double arguments are specified using D. The method returns void and this is specified using V after the right parenthesis.

    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;
  4. The following functions call Java methods that 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;
  5. The following illustrates a sample call to the stored procedure that acts as a wrapper for the init method of the Invoice class:

    CALL init('Shirt',10.00,'Jacket',25.00);
  6. The following SELECT statement calls several of the other methods in the Invoice class:

    SELECT getLineItem1Description() as Item1, 
           getLineItem1Cost() as Item1Cost,
           getLineItem2Description() as Item2, 
           getLineItem2Cost() as Item2Cost,
           rateOfTaxation() as TaxRate, 
           totalSum() as Cost;

    The SELECT statement returns six columns.

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

Résultat

You have created stored procedures or functions that act as wrappers for the methods in the Java class. These lessons have taken you through the steps involved in writing Java methods and calling them from SQL.

 See also