Example Java Calls

Calling Java using the UD5/JNI+MW1 driver is straightforward and simple. There are 2 primary ways of calling a Java method, either:

Calling from SQL

Calling Java using the sql proc statement involves 3 steps (assuming that the classpath has been set in an environment variable, or your ASN file):

EXAMPLE 1:

This example calls a method which returns an array of strings. Firstly here is the Java code:

//v 1.3



public class TESTING {

    public static String[] HELLOA() {

	String[] anArray = { "Mary had a little lamb", "Its fleece was white as snow", "And everywhere that Mary went", "That lamb was sure to go" };

	return (anArray);

    }

}

The calls may use either the sql proc statement from Uniface, or the SQL workbench. In either case, each command must be executed in turn, they may not be executed "at once".



; put the results into the message frame

sql "@return-type [Ljava/lang/String;","ud5"

sql/print "TESTING.HELLOA","ud5"

putmess $result

macro "^message"

The resulting message frame contents would be:



Results from Java            

=================            

Mary had a little lamb       

Its fleece was white as snow 

And everywhere that Mary went

That lamb was sure to go     

EXAMPLE 2:

This example calls a method which returns an single string. Firstly here is the Java code:

//v 1.3



public class TESTING {

    public static String HELLO() {

	return ("Hello World");

    }

}

The calls may use either the sql proc statement from Uniface, or the SQL workbench. In either case, each command must be executed in turn, they may not be executed "at once".



; put the results into the message frame

sql "@return-type Ljava/lang/String;","ud5"

sql/print "TESTING.HELLO","ud5"

putmess $result

macro "^message"

The resulting message frame contents would be:



Hello World

EXAMPLE 3:

This example calls a method which returns an int. Firstly here is the Java code:

//v 1.3



public class TESTING {

    public static int ADD(int a, int b) {

	return a+b;

    }

    public static int ADD(String a, String b) {

        Integer Ia = new Integer(a);

	Integer Ib = new Integer(b);

	return ADD(Ia.intValue(),Ib.intValue());

    }

}

Note that this is an overloaded method. When Java methods are called from the sql proc statement, they always take String values by default (to change this see example 4). You can then convert the string values, and perform any type checking in Java.

The calls may use either the sql proc statement from Uniface, or the SQL workbench. In either case, each command must be executed in turn, they may not be executed "at once".



; put the results into the message frame

sql "@return-type I","ud5"

sql/print "TESTING.ADD 125,234","ud5"

putmess $result

macro "^message"

The resulting message frame contents would be:



359

EXAMPLE 4:

This example calls a method which takes 2 int parameters, and returns an int. Firstly here is the Java code:

//v 1.3



public class TESTING {

    public static int SUBTRACT(int a, int b) {

	return a-b;

    }

}

The calls may use either the sql proc statement from Uniface, or the SQL workbench. In either case, each command must be executed in turn, they may not be executed "at once".



; put the results into the message frame

sql "@return-type I","ud5"

sql "@param-sig I","ud5"

sql/print "TESTING.SUBTRACT 150,250","ud5"

putmess $result

macro "^message"

The resulting message frame contents would be:



-100

Calling from ACTIVATE

Calling Java using the activate proc statement involves 3 steps (assuming that the classpath has been set in an environment variable, or your ASN file):

EXAMPLE 1:

This example calls a method which returns an int, and has two int's as parameters. Firstly here is the Java code:

//v 1.3



public class TESTING {

    public static int ADD(int a, int b) {

	return a+b;

    }

}

See the below section for the remainder of this example:

$Revision: 1.9 $ $Date: 2014/11/05 02:24:16 $[go to top]