DB2 Version 9.7 for Linux, UNIX, and Windows

SQLJ utilization of SDK for Java Version 5 function

Your SQLJ applications can use a number of functions that were introduced with the SDK for Java™ Version 5.

Static import

The static import construct lets you access static members without qualifying those members with the name of the class to which they belong. For SQLJ applications, this means that you can use static members in host expressions without qualifying them.

Example: Suppose that you want to declare a host expression of this form:
double r = cos(PI * E);
cos, PI, and E are members of the java.lang.Math class. To declare r without explicitly qualifying cos, PI, and E, include the following static import statement in your program:
import static java.lang.Math.*;

Annotations

Java annotations are a means for adding metadata to Java programs that can also affect the way that those programs are treated by tools and libraries. Annotations are declared with annotation type declarations, which are similar to interface declarations. Java annotations can appear in the following types of classes or interfaces:

You cannot include Java annotations directly in SQLJ programs, but you can include annotations in Java source code, and then include that source code in your SQLJ programs.

Example: Suppose that you declare the following marker annotation in a program called MyAnnot.java:
public @interface MyAnot { }
You also declare the following marker annotation in a program called MyAnnot2.java:
public @interface MyAnot2 { }
You can then use those annotations in an SQLJ program:
// Class annotations
@MyAnot2 public @MyAnot class  TestAnnotation   
{
  // Field annotation
  @MyAnot
  private static final int field1 = 0;          
  // Constructor annotation
  @MyAnot2 public @MyAnot TestAnnotation  () { } 
   // Method annotation
   @MyAnot             
   public static void main (String a[]) 
   {
      TestAnnotation TestAnnotation_o = new TestAnnotation();
      TestAnnotation_o.runThis();
   }
  // Inner class annotation
  public static @MyAnot class TestAnotherInnerClass { }
  // Inner interface annotation
  public static @MyAnot interface TestAnotInnerInterface { }
}

Enumerated types

An enumerated type is a data type that consists of a set of ordered values. The SDK for Java version 5 introduces the enum type for enumerated types.

You can include enums in the following places:
  • In Java source files (.java files) that you include in an SQLJ program
  • In SQLJ class declarations

Example: The TestEnum.sqlj class declaration includes an enum type:
public class TestEnum2
{
  public enum Color {
	  RED,ORANGE,YELLOW,GREEN,BLUE,INDIGO,VIOLET}
  Color color;
  …                          // Get the value of color
  switch (color) {
	case RED: 
    System.out.println("Red is at one end of the spectrum.");
    #sql[ctx] { INSERT INTO MYTABLE VALUES (:color) };
	  break;
	case VIOLET:
    System.out.println("Violet is on the other end of the spectrum.");
	  break;
	case ORANGE:
	case YELLOW:
	case GREEN:
  case BLUE:
  case INDIGO:
    System.out.println("Everything else is in the middle.");
	  break;
}

Generics

You can use generics in your Java programs to assign a type to a Java collection. The SQLJ translator tolerates Java generic syntax. Examples of generics that you can use in SQLJ programs are:
  • A List of List objects:
    List <List<String>> strList2 = new ArrayList<List<String>>();
  • A HashMap in which the key/value pair has the String type:
    Map <String,String> map = new HashMap<String,String>();
  • A method that takes a List with elements of any type:
    public void mthd(List <?> obj) {
    …
    }

Although you can use generics in SQLJ host variables, the value of doing so is limited because the SQLJ translator cannot determine the types of those host variables.

Enhanced for loop

The enhanced for lets you specify that a set of operations is performed on each member of a collection or array. You can use the iterator in the enhanced for loop in host expressions.

Example: INSERT each of the items in array names into table TAB.
String[] names = {"ABC","DEF","GHI"};
for (String n : names) 
{
  #sql {INSERT INTO TAB (VARCHARCOL) VALUES(:n) };
}

Varargs

Varargs make it easier to pass an arbitrary number of values to a method. A Vararg in the last argument position of a method declaration indicates that the last arguments are an array or a sequence of arguments. An SQLJ program can use the passed arguments in host expressions.

Example: Pass an arbitrary number of parameters of type Object, to a method that inserts each parameter value into table TAB.
public void runThis(Object...  objects) throws SQLException
{
  for (Object obj : objects) 
  {
    #sql { INSERT INTO TAB (VARCHARCOL) VALUES(:obj) };
  }
}