Oracle8i Enterprise JavaBeans and CORBA Developer's Guide
Release 8.1.5






Prev  Chap Top Next

Using SQLJ

You can often simplify the implementation of a CORBA server object by using Oracle8i SQLJ to perform static SQL operations. Using SQLJ statements results in less code than the equivalent JDBC calls, and makes the implementation easier to understand and debug. This section shows a version of the example first shown in "A First CORBA Application", but uses SQLJ rather than JDBC for the database access. Refer to the Oracle8i SQLJ Developer's Guide and Reference for complete information about SQLJ.

The only code that changes for this SQLJ implementation is in the file, that implements the Employee object. The SQLJ implementation, which can be called EmployeeImpl.sqlj, is listed below. You can contrast that with the JDBC implementation of the same object in "Write the Server Object Implementation".

package employeeServer;

import employee.*;
import java.sql.*;

public class EmployeeImpl extends _EmployeeImplBase {
  public EmployeeInfo getEmployee (int ID) throws SQLError {
    try {
      String name = null;
      double salary = 0.0;
      #sql { select ename, sal into :name, :salary from emp
             where empno = :ID };
      return new EmployeeInfo (name, empno, (float)salary);
    } catch (SQLException e) {
      throw new SQLError (e.getMessage ());

The SQLJ version of this implementation is considerably shorter than the JDBC version. In general, Oracle recommends that you use SQLJ where you have static SQL commands to process, and use JDBC, or a combination of JDBC and SQLJ, in applications where dynamic SQL statements are required.

Running the SQLJ Translator

To compile the EmployeeImpl.sqlj file, you issue the following SQLJ command:

% sqlj -J-classpath 
$(ORACLE_HOME)/lib/vbjapp.jar:$(JDK_HOME)/lib/ -ser2class

This command does the following:

The SQLJ translation generates two additional class files:


which you must also load into the database when you execute the loadjava command.

A Complete SQLJ Example

This example is available in complete form in the examples/corba/basic example directory, complete with a Makefile or Windows NT batch file so you can see how the example is compiled and loaded. See also "sqljimpl".



Copyright © 1999 Oracle Corporation.

All Rights Reserved.