Your SQLJ applications can use a number of functions that were introduced with the SDK for Java™ Version 5.
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.
double r = cos(PI * E);
import static java.lang.Math.*;
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.
public @interface MyAnot { }
You
also declare the following marker annotation in a program called MyAnnot2.java:public @interface MyAnot2 { }
// 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 { }
}
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.
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;
}
List <List<String>> strList2 = new ArrayList<List<String>>();
Map <String,String> map = new HashMap<String,String>();
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.
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.
String[] names = {"ABC","DEF","GHI"};
for (String n : names)
{
#sql {INSERT INTO TAB (VARCHARCOL) VALUES(:n) };
}
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.
public void runThis(Object... objects) throws SQLException
{
for (Object obj : objects)
{
#sql { INSERT INTO TAB (VARCHARCOL) VALUES(:obj) };
}
}