Writing exits for DB2 and Java generation

This topic extends "Writing exits for EGL access of DB2" and is specific to Java generation.

Source code

The exit code that runs by default is in the fda7.jar file, which is in com.ibm.etools.egl.java.runtime_version, where version is the product version. The class name is EglDatabaseExits.

The following table lists the methods in that class.

Method name Purpose Type for input and output
dateExitToDB Before database input, revise the value that will be assigned to the database from a host variable of type DATE. String
dateExitFromDB After database output, revise the value that will be placed in a host variable of type DATE. String
timeExitToDB Before database input, revise the value that will be assigned to the database from a host variable of type TIME. String
timeExitFromDB After database output, revise the value that will be placed in a host variable of type TIME. String
timestampExitToDB Before database input, revise the value that will be assigned to the database from a host variable of type TIMESTAMP. String
timestampExitFromDB After database output, revise the value that will be placed in a host variable of type TIMESTAMP. String
Here is the code that runs by default:
package egl.io.sql;

public class EglDatabaseExits
{
   /**
     * Does custom formatting of a string used as input to a DATE column.
     *
     * @param input -  the value from EGL.
     * @return a string in the format which will then be formatted 
       by StrLib.defaultDateFormat.
     */
    public static String dateExitToDB( String input ) { return input; }

   /**
     * Does custom formatting of a string value fetched from a DATE column.
     *
     * @param input -  the value from the DATE column, formatted 
       according to StrLib.defaultDateFormat.
     * @return a string, will be assigned to an EGL variable.
     */
    public static String dateExitFromDB( String output ) { return output; }

    /**
     * Does custom formatting of a string used as input to a TIME column.
     *
     * @param input -  the value from EGL.
     * @return a string in the format that will then be formatted 
       by StrLib.defaultTimeFormat.
     */
    public static String timeExitToDB( String input ) { return input; }

    /**
     * Does custom formatting of a string value fetched from a TIME column.
     *
     * @param input - the value from the TIME column, formatted 
       according to StrLib.defaultTimeFormat.
     * @return a string which  will be assigned to an EGL variable.
     */
    public static String timeExitFromDB( String output ) { return output; }

    /**
     * Does custom formatting of a string used as input to a TIMESTAMP column.
     *
     * @param input - the value from EGL.
     * @return a string in the format which will then be formatted 
       by StrLib.defaultTimestampFormat.
     */
    public static String timestampExitToDB( String input ) { return input; }

    /**
     * Does custom formatting of a string value fetched from a TIMESTAMP column.
     *
     * @param input -  the value from the TIMESTAMP column, formatted 
       according to StrLib.defaultTimestampFormat.
     * @return a string, will be assigned to an EGL variable.
     */
    public static String timestampExitFromDB( String output ) { return output; }
}

As shown, the masks that you set for date, time, and timestamp have an effect on the data sent to and or received from those exits. You can set those masks in your code by setting the StrLib.defaultDateFormat, StrLib.defaultTimeFormat, and StrLib.defaultTimeStampFormat variables. Those variables can receive default values from Java runtime properties. For details, see the reference topics that are listed at the end of this topic.

Example and logic flow

The following SQL statement uses two character fields, each of which has a sqlDataCode value to state that the related column is of type DATE:
get myrec with #sql{ 
   SELECT COL1 FROM TBL WHERE :dateChar1 = '2010-04-01' }
   into dateChar2;
The query is constructed as follows, whether by the EGL Java generator or by the EGL Debugger:
  1. Get the value of the dateChar1 field.
  2. Pass that value to the EglDatabaseExits.dateExitToDB method.
  3. Create a JDBC Date object from the value returned by the method. The object creation uses a string-to-date conversion that is guided by the value of the StrLib.defaultDateFormat variable.
  4. When interacting with the database, use the JDBC Date object.
Here are the events when you fetch example data:
  1. Get the value of COL1 from the database. That value is a JDBC Date object.
  2. Create a string from that object. The string creation uses a date-to-string conversion that is guided by the value of the StrLib.defaultDateFormat variable.
  3. Pass the string to the EglDatabaseExits.dateExitFromDB method.
  4. Assign the value that was returned from that method to the dateChar2 field.

Usage notes

To debug or run generated Java code that uses a custom EglDatabaseExits class, place the class in the classpath in a way that precedes fda7.jar, which is the EGL Java runtime jar file. In the EGL Debugger, you can use the classpath preference or the Java Build Path property of the project.

In general, the easiest way to set the classpath of a generated Java batch program in Eclipse is by using Java Build Path property of the project. Outside of Eclipse, use the CLASSPATH environment variable or -classpath command-line argument.

When you compile generated code, you do not need a custom EglDatabaseExits class in the classpath.

To debug or run generated Java code in the Apache Tomcat server or in WebSphere Application Server, place the custom class in the deployment web project, in the WEB-INF/classes folder. Either server searches that folder before searching the WEB-INF/lib folder, where the fda7.jar file is placed for you.


Feedback