SQLJ


SQLJ is a working title for efforts to combine Java and SQL. It was a common effort started around 1997 by engineers from IBM, Oracle, Compaq, Informix, Sybase, Cloudscape and Sun Microsystems.
It consists of the three parts: 0, 1 and 2. Part 0 describes the embedding of SQL statements into Java programs. SQLJ part 0 is the basis for part 10 of the standard, aka SQL Object Language Bindings. SQLJ parts 1 and 2 describes the converse possibility to use Java classes from SQL statements. Parts 1 and 2 are the basis for part 13 of the SQL standard, SQL Routines and Types Using the Java Programming Language.
"SQLJ" is commonly used to refer to just SQLJ part 0, usually when it is contrasted with other means of embedding SQL in Java, like JDBC.

ANSI and ISO standards

Part 0 was updated for JDBC 2.0 compatibility and ratified by ISO in 2000. The last two parts were combined when submitted to ISO. Part 2 was substantially rewritten for the ISO submission because the ANSI version was not formal enough for a specification, being closer to the style of a user manual. The combined version was ratified in 2002.
The SQLJ part 0 specification largely originated from Oracle, who also provided the first reference implementation.
In the following SQLJ is a synonym for SQLJ part 0.
Whereas JDBC provides an API, SQLJ consists of a language extension. Thus programs containing SQLJ must be run through a preprocessor before they can be compiled.

Advantages

Some advantages of SQLJ over JDBC include:
The following examples compare SQLJ syntax with JDBC usage.
JDBCSQLJ

PreparedStatement stmt = conn.prepareStatement;
stmt.setBigDecimal;
stmt.setBigDecimal;
ResultSet rs = stmt.executeQuery;
while )
rs.close;
stmt.close;

  1. sql private static iterator EmployeeIterator;
...
EmployeeIterator iter;
  1. sql iter = ;
do while );
iter.close;

JDBCSQLJ

PreparedStatement stmt = conn.prepareStatement, AVG"
+ " FROM DSN8710.EMP");
rs = stmt.executeQuery;
if )
maxSalary = rs.getBigDecimal;
avgSalary = rs.getBigDecimal;
if )
rs.close;
stmt.close;

  1. sql ;

JDBCSQLJ

stmt = conn.prepareStatement "
+ "VALUES ;
stmt.setString;
stmt.setString;
stmt.setString;
stmt.setString;
stmt.setBigDecimal;
stmt.executeUpdate;
stmt.close;

  1. sql ;