SQL/MX Programming Manual for Java
Introduction
HP NonStop SQL/MX Programming Manual for Java—523726-003
1-4
SQLJ Compared With JDBC
SQLJ Compared With JDBC
SQLJ offers the following benefits as compared with JDBC programs.
Statically Compiled SQL
SQLJ programs typically use embedded SQL statements in SQLJ clauses for static 
SQL and JDBC methods for dynamic SQL. SQLJ is intended to yield the performance 
benefits of statically compiled SQL. Static SQL statements do not incur the cost of 
compilation at execution time because you compile them during development. If 
needed, you can use JDBC for SQL statements in an application, but SQL statements 
in JDBC run dynamically. For more information, see SQLJ Versus JDBC Performance 
on page 1-6.
Concise Programming Model
SQLJ offers a more concise programming model than JDBC. SQLJ automatically 
generates the code that you must include in a JDBC program. Therefore, you do not 
have to write as much code in an SQLJ program as you would in a JDBC program.
For example, in SQLJ, you can use host variables in an SQLJ clause:
#sql { INSERT INTO employee VALUES (:lastName,
:firstsName,
:deptNum,
:jobCode) };
In JDBC, you must individually set the value of parameters, which take the place of 
host variables, in separate call statements, as this example shows:
Connection conn = 
DriverManager.getConnection("jdbc:sqlmx:");
PreparedStatement stmt = conn.prepareStatement
("INSERT INTO employee VALUES (?,?,?,?)");
stmt.setString(1,lastName);
stmt.setString(2,firstName);
stmt.setInt(3,deptNum);
stmt.setInt(4,jobCode);
stmt.execute();
stmt.close();
In SQLJ, you can use both positioned and named iterators to retrieve rows. The 
positioned iterator has a more concise style than the named iterator, which is similar to 
the JDBC model for retrieval. For more information about iterators, see Iterators and 
Result Sets on page 3-48.










