CS6320:  SW Engineering of Web Based Systems

 

SQLJ


  • Built on top of JDBC
  • Enables developers to write less code to embed SQL statements in Java Programs
  • A preprocessor is used to translate SQLJ into Java code using JDBC calls to database.   TO USE SQLJ your compiler must support this!!!!!
  • These SQL statements are actually checked at compile-time and hence allow for increased error detection (in JDBC pass SQL queries as strings...no error checking at compile time)

 
JDBC Program (from E-Commerce book)

Java.sql.CallableStatement  stmt;
Connection conn;
ResultSet res;

//Setup Connection

conn = DriverManager.getConnection("jdbc:default");

//prepare statement to execute
stmt = conn.prepareStatment("SELECT empno, ename FROM emp WHERE sal > ? and deptno = ? ");

//Use positional parameters to set variables
stmt.setString(1,sal_p);
stmt.setInteger(2, deptno_p);

//Execute query and store results
res = stmt.executeQuery();
 
 
 
 
 
 

Equivalent SQLJ Program (from E-Commerce book)

ResultSet res;

#sql res = (SELECT empno, ename FROM emp WHERE  sal > :sal_p AND deptno = :deptno_p);
 

//pass the above throug the SQLJ translator and will 
// get the same results as program on the left.

© Lynne Grewe