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.
Prerequisites
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.
Context and remarks
The Java class file containing the compiled methods from the Invoice example has been loaded into the database.
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.
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.
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; |
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; |
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); |
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 |
![]() |
Discuss this page in DocCommentXchange.
|
Copyright © 2014, SAP AG or an SAP affiliate company. - SAP Sybase SQL Anywhere 16.0 |