Chapter 10. Structured Query Language for Java (SQLJ) 407
Consider these points when you compare SQLJ and JDBC:
򐂰 SQLJ source programs are smaller than the equivalent JDBC programs.
򐂰 SQLJ programs allow direct embedding of Java bind expressions within SQL statements.
JDBC requires a separate call statement for each bind variable and specifies the binding
by position number.
򐂰 SQLJ provides strong type checking of query output and returns parameters on
pre-compile. JDBC passes values to and from SQL without compile-time type checking.
򐂰 SQLJ provides simplified rules for SQL statements and for calling stored procedures and
functions. JDBC requires more statements and takes more time and effort to code.
򐂰 SQLJ generates JDBC statements. You do not have complete control of the JDBC
statements as you do when writing it directly.
򐂰 If performance is your major concern, consider using JDBC rather than SQLJ because you
can deal directly with the JDBC statements.
10.2.1 An overview of how SQLJ works
This is an overview of how to program using SQLJ and Java. The SQL statement is written in
the Java source within curly brackets ({}) and it is preceded by a symbol #sql, for example:
#sql { select CFIRST into :customerName from CSTMR where CID = :cstno }
This is all you need to enter to retrieve a customer record from the CSTMR file. This is much
simpler than JDBC and is a more natural way of coding SQL statements.
To use SQLJ, the member type of the Java source is different and needs to be converted into
Java code using a translator. This translator converts the SQL statement into JDBC
statements. For example, we have the following .sqlj source file:
HelloWorld.sqlj
This member type needs to be translated by the SQLJ translator, for example:
Sqlj HelloWorld.sqlj
As a result of running the translator, the following files are generated:
򐂰 HelloWorld.java
򐂰 HelloWorld.class
򐂰 HelloWorld_SJProfileKeys.class
򐂰 HelloWorld_SJProfile0.ser
The Java source and class files are standard Java files. The translator generates the
additional class and .ser file to handle the conversion of the SQL statements to JDBC
statements.
VisualAge for Java also provides an SQLJ tool that implements the SQLJ standard. The
translator component is integrated into the IDE, so you can import, translate, and edit SQLJ
files. The runtime environment is an installable feature that is added to your workspace. The
original .sqlj source files are maintained in your project resources directory, as are the
profiles.
In our examples, we use VisualAge for Java as a tool to store source files, translate source
files, and act as a runtime environment.

Get Building Java Applications for the iSeries Server with VisualAge for Java 3.5 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.