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.
double r = cos(PI * E);
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:
- Class declaration
- Interface declaration
- Nested class declaration
- Nested interface declaration
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 { }
}
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.
- In Java source files (.java files) that you include in an SQLJ program
- In SQLJ class declarations
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. 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.
- 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) { … }
- Nested generics in the method return
type:
public* List<Map<String, Object>> *method(String, String, String);
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.
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.
public void runThis(Object... objects) throws SQLException
{
for (Object obj : objects)
{
#sql { INSERT INTO TAB (VARCHARCOL) VALUES(:obj) };
}
}