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

SQL Anywhere 10.0.1 » SQL Anywhere Server - Programming » Tutorial: Using Java in the Database » Introduction to Java in the Database tutorial

Introduction to Java in the Database tutorial Next Page

Creating and compiling the sample Java class


The first step to using Java in the database is to write the Java code and compile it. This is done outside the database.

To create and compile the class
  1. 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();
        }
    }
  2. 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.