The following sections describe the steps involved in creating Java methods and calling them from SQL. It shows you how to compile and install a Java class into the database to make it available for use in SQL Anywhere. It also shows you how to access the class and its members and methods from SQL statements.
The following sections assume that you have a Java Development Kit (JDK) installed, including the Java compiler (javac) and Java VM.
Source code and batch files for the sample are provided in samples-dir\SQLAnywhere\JavaInvoice.
The first step to using Java in the database is to write the Java code and compile it. This is done outside the database.
Create the sample Java class source file.
For your convenience, the sample code is included here. You can paste the following code into Invoice.java or obtain the file from samples-dir\SQLAnywhere\JavaInvoice.
import java.io.*; public class Invoice { public static String lineItem1Description; public static double lineItem1Cost; public static String lineItem2Description; public static double lineItem2Cost; public static double totalSum() { double runningsum; double taxfactor = 1 + Invoice.rateOfTaxation(); runningsum = lineItem1Cost + lineItem2Cost; runningsum = runningsum * taxfactor; return runningsum; } public static double rateOfTaxation() { double rate; rate = .15; return rate; } public static void init( String item1desc, double item1cost, String item2desc, double item2cost ) { lineItem1Description = item1desc; lineItem1Cost = item1cost; lineItem2Description = item2desc; lineItem2Cost = item2cost; } public static String getLineItem1Description() { return lineItem1Description; } public static double getLineItem1Cost() { return lineItem1Cost; } public static String getLineItem2Description() { return lineItem2Description; } public static double getLineItem2Cost() { return lineItem2Cost; } public static boolean testOut( int[] param ) { param[0] = 123; return true; } public static void main( String[] args ) { System.out.print( "Hello" ); for ( int i = 0; i < args.length; i++ ) System.out.print( " " + args[i] ); System.out.println(); } } |
Compile the file to create the file Invoice.class.
javac Invoice.java |
The class is now compiled and ready to be installed into the database.
Discuss this page in DocCommentXchange.
|
Copyright © 2010, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.0 |