External Java methods can simplify the process of writing complex business logics, as the developers have a relative freedom that they no longer have to restrain themselves in the boundaries of procedural languages since they can be benefited by using the extensive capabilities of Java.
Used Parameter Types in External Java Methods
While using the External Java methods, we can pass certain information by the means of parameters to and from the procedures. When you define the parameter, you also specify the way in which the parameter can be used. There are three different types of parameters IN, OUT and INOUT. The type determines how the parameter will be used.
The IN parameter allows you to pass values in to the calling procedure, but will not be accessible outside the procedure. IN parameters act like constants, as the value of IN parameters cannot be changed within the procedure.
An OUT parameter is like the return value for a procedure, but it appears in the parameter list. Inside the program, an OUT parameter acts like an initialized variable. In fact, the OUT parameter has no value at all until the program terminates successfully. If you are specifying an OUT type, parameter in the case of external Java procedure the parameter datatype should be javaparameter (in Java method signature).
With an INOUT parameter, you can pass values into the procedure and return a value back to the calling procedure.
We have included some elaborative examples to describe the process of creating external Java methods.
CREATE PROCEDURE p1 (OUT b int, IN c Boolean, INOUT d int) specific spname EXTERNAL NAME examplejar:examples.testingForProcedure:QLConnect(javaparameter, Boolean, javaparameter)
Where, spname is the specific name of procedure for unique identification. examplejar is name of jar where java class is supposed to be found. examples.testingForProcedure is the name of the class with its package name (package name is mandatory if class resides in a package). SQLConnect is the name of the method.
CREATE PROCEDURE SELF() specific SSELF EXTERNAL NAME testingForProcedure: processData()
Where, SSELF is name the procedure for unique identification. testingForProcedure is the name of the class. processData() is the name of the method.
CREATE PROCEDURE p1(IN a int, IN b Boolean) specific specefic2 EXTERNAL NAME examplesjar:testingForProcedure:: verify(Integer,Boolean)
Where, Specefic2 is the specific name of procedure for unique identification.
examplesjar is name of jar where java class will be found..
testingForProcedure is the name of the class.
verify(Integer,Boolean) is the name of the method along with parameter list.
CREATE PROCEDURE p2() specific specefic3 EXTERNAL NAME examples.testingForProcedure:check().
Where, Specefic2 is the specific name of procedure for unique identification. examples.testingForProcedure is the name of the class with package name. check()is the name of the method.
Few things that a user must know
1. The jar name is optional, but the jar, which is specified in the class path, can be provided.
2. Package name is mandatory if the java class has a package. If you do not specify package name then java class will not be loaded and class not found error would be thrown.
3. The Specified Class Name must have a constructor with java.sql.Connnection as parameter; otherwise, an Exception will be thrown.
4. The java data type in the specified method must be a javaparameter for each Out or InOut parameter mentioned in the Procedure Definition.
5. The java data type in the specified method must contain same count of parameters as defined in the Procedure Definition.
6. The specified Class must contain specified functions having parameters of same count and same data type as defined in the Procedure Definition.
7. Jar/ Java class must be available in Daffodil DB classpath.