SQLJ: DML Sample Application

Table Of Contents 

Overview of the Sample Application 

Back To Top

This sample application shows how to perform SQL INSERT, SELECT, UPDATE, and DELETE operations using SQLJ.

SQLJ enables applications programmers to embed SQL operations in Java code. A SQLJ program is a Java program containing embedded SQL statements that comply with the ISO standard SQLJ Language Reference syntax.

SQLJ consists of a translator and a runtime component (translator.jar/zip and runtime12ee.jar/zip) and is smoothly integrated into the development environment. The translation, compilation, and customization take place in a single step when the front-end utility sqlj is run. The translation process replaces embedded SQL with calls to the SQLJ runtime, which implements the SQL operations. When the end user runs the SQLJ application, the runtime is invoked to handle the SQL operations.

SQLJ runs on top of JDBC. To access an Oracle database, you would typically use an Oracle JDBC driver. In order to run SQLJ programs, apart from SQLJ classes, JDBC classes should be present in the system CLASSPATH. SQLJ code is written and saved in *.sqlj files and should be translated to *.java files before compiling using the front-end utility sqlj.

This sample application simulates an Airlines Details Form in which the Airline code, Name and Partner details are tracked by the application. The user can add new Airlines details and at a later point of time update this record with the changed details. All the Airlines details are saved in the database for the user to browse/update/delete.

Working of the Sample

The sample application uses a database table OTN_AIRLINES. When the application is invoked, the table and few records are created in the database if they do not exist. The status of the connection is shown in the status bar. Errors if any, are shown in the status bar. All the created/existing records are displayed in the JTable when 'Select' button is pressed. Records can also be selected by giving values for code, name or partner. The records may be inserted, modified and deleted using the form displayed.

The underlying code will use embedded SQL along with SQLJ iterators to perform all the SELECT, INSERT, UPDATE and DELETE operations of the records in the database table.

Here is the SQLJ code usage for selecting, inserting and updating an OTN_AIRLINES record in the database. You can find more details of the code in SqljDMLSample.sqlj file under src/oracle/otnsamples/sqlj/dml folder. Look into Description of Sample Files section for folder and file details.

Creating new DefaultContext for the database connection:
   .........
   .........				
  /** Database Connection Object */
  private Connection connection = null;
     
  /** Database Connection Context object. **/
  private DefaultContext connContext = null;

  private Connection dbConnection() {
    try {


      // Load the properties file to get the connection information.
      Properties prop = loadParams("Connection");

      // Create a OracleDataSource instance.
      OracleDataSource ods = new OracleDataSource();
      .........
      .........

      // Create a connection  object.
      connection = ods.getConnection();
      
      // get a default context using above connection to execute SQLJ statement.
      connContext = new DefaultContext(connection);         

      //Set the above connection context as the default context for this

      //application.
      DefaultContext.setDefaultContext(connContext);
  } catch(Exception ex){ //Catch exceptions.
      .........
  }
  return connection;
 }			  
			  

Select method:
/**
 *  Method to select records from the OTN_AIRLINES table using SQLJ, based on 
 *  the query conditions entered by the user.
 **/
 private void selectRecords(String code,String name,String partner){
   ........

   try{
       .........
	     .........
      //Declare an instance of the SelRowIterator, which will hold
      //all the OTN_AIRLINES records returned by the query executed.
      SelRowIter selRow = null;

      //Embedded SQL: This call selects all records from the OTN_AIRLINES table
      //which meet the selection criteria. The selection criteria is bound to
      // the variables: code, name and partner. The query results are assigned
      // to an iterator: selrow
      #sql selRow  = { SELECT * FROM otn_airlines
                       WHERE UPPER(code) LIKE UPPER(:code) 
                       AND UPPER(name) LIKE UPPER(:name)       
                       AND UPPER(partner) LIKE UPPER(:partner)
                     };

      //Populate the iterator and process all rows returned.

      while(selRow.next()) {
        code = selRow.code();
        name = selRow.name();
        partner = selRow.partner();
        ......... 
       }
      .........

      //Close the iterator.
      selRow.close();
   } catch(Exception ex){  //Catch exceptions.
       .........
   }
 }

Insert method:
 private void insertRecords(String code,String name,String partner){
  try{
     //Embedded SQL: This call inserts a record into the OTN_AIRLINES table.
     //The values to be inserted are bound to the variables: code, name
     //and partner.

     #sql { INSERT INTO otn_airlines VALUES(:code, :name, :partner)};
     ........ 
  } catch (SQLException ex) { //Catch SQLExceptions.
      ........
  }
 }				
				

Update method:
 private void updateRecord(String code,String name,String partner){
   try{
     //Embedded SQL: This call updates the OTN_AIRLINES record that has been
     //selected, to the new values specified in the TextFields. The values to 
     //be updated are bound to the variables: name,code and partner.
     int cnt;
     #sql { SELECT count(*) INTO :cnt FROM otn_airlines
            WHERE code =:code };

     #sql { UPDATE otn_airlines SET name =:name, partner =:partner
                                              WHERE code =:code };
   .........
				

 

Required Software

Back To Top
  • Oracle9i JDeveloper ( Note: Oracle9i JDeveloper is Oracle's Visual Java Development Tool and can be downloaded from here)
    or
    JDK1.2.x or above This can be downloaded from here.
  • Oracle9i Database or higher running SQL*Net TCP/IP listener. This can be downloaded from here.
  • Oracle9i SQLJ Translators, Release 9.2.0.1.0. This can be downloaded from here.
  • Oracle JDBC Driver, Release 2 (9.2.0.1). This can be downloaded from here.

Note: Both JDBC and SQLJ libraries are available with Oracle Database or client installation and need not be downloaded separately.

Notations Used

Notation
Description
<SQLJ_EXE_HOME>
the directory where SQLJ executable: sqlj is found. For example: d:\sqlj\bin
<SQLJ_LIB>
the directory where SQLJ classes: translator.jar and runtime12ee.jar are found.
For example: d:\sqlj\lib
<JDBC_LIB>
the directory where JDBC driver class: classes12.jar is found.
For example: d:\oracle9i\jdbc\lib
<JAVA_HOME>
the directory where JDK1.2 or higher is installed. For example: D:\jdk1.3.1

Application Set-up and Configuration

Back To Top
  • Unjar the provided SqljDMLSample.jar using the following command 

  • > jar xvf SqljDMLSample.jar

    Note: You will find jar.exe in JDK_HOME\bin. Ensure JDK_HOME\bin is present in your system path. (JDK_HOME is the root directory of the JDKx.x installation). This creates a folder SqljDMLSample with all the source files.
  • Edit SqljDMLSample/Connection.properties file in your favourite editor. Change the HostName, Port, SID, UserName and Password to connect to your own database.
HostName = localhost.idc.oracle.com
SID = ORCL
Port = 1521
UserName = scott
Password = tiger

Running the Application 

Back To Top

This sample application can be run in 3 different ways listed below.

From Oracle9i JDeveloper

    • Open Oracle9i JDeveloper and use File/Open menu option to select the SqljDMLSample.jws from the SqljDMLSample directory.
    • Next, select Project/Make SqljDMLSample.jpr from main menu.
    • Now, select Run/Run SqljDMLSample.jpr from main menu to run the application.

From JDK for Windows

This section will describe steps to run the application from console using JDK on Windows. The sample can be run either manually or using a Batch file .

Run application using batch file: run.bat provided:

By setting few environment variables, the sample application could be directly run by just executing the batch file: run.bat from the command prompt, from SqljDMLSample directory. Environmental variables JAVA_HOME, SQLJ_EXE_HOME, SQLJ_LIB and JDBC_LIB have to be set before running run.bat file. Please look into Notations sections for more details on these environmental variables.

Note: If you have already run the sample application using JDeveloper, you will need to delete *.generated.java files that would have been created by JDeveloper.
Example:

D:\SqljDMLSample> set SQLJ_EXE_HOME=d:\sqlj\bin
D:\SqljDMLSample
> set SQLJ_LIB=d:\sqlj\lib
D:\SqljDMLSample
> set JDBC_LIB=d:\oracle9i\jdbc\lib
D:\SqljDMLSample> set JAVA_HOME=d:\jdk1.3.1
D:\SqljDMLSample> run

Running the application manually:

  • Set CLASSPATH to include:
    • Oracle9i JDBC Driver file: classes12.jar/zip
    • Oracle9i SQLJ files: translator.jar/zip and runtime12ee.jar/zip
    • SqljDMLSample directory where Connection.properties exists(the current directory).
      Example:
       
      D:\SqljDMLSample>set CLASSPATH=d:\sqlj\lib\translator.jar;d:\sqlj\lib\runtime12ee.jar;
      D:\oracle9i\jdbc\lib\classes12.zip;.
  • Make sure that Java and SQLJ(where sqlj is found) are in the PATH.
    Example:
    D:\SqljDMLSample>set PATH=.;d:\jdk1.3.1\bin;d:\sqlj\bin;%PATH%
  • From the directory SqljDMLSample, translate all *.sqlj files to *.java files using sqlj:
    Example:
     
    D:\SqljDMLSample>sqlj -compile=false src\oracle\otnsamples\sqlj\dml\*.sqlj
  • From the same SqljDMLSample directory, now compile all *.java files using javac:
    Example: 
    D:\SqljDMLSample>javac -d . src\oracle\otnsamples\sqlj\dml\*.java
  • Run the class file using java from the same SqljDMLSample directory.
    Example:
    D:\SqljDMLSample>java oracle.otnsamples.sqlj.dml.SqljDMLSample

From JDK for Linux

This section will describe steps to run the application from console using JDK on Red Hat Linux Advanced Server Release 2.1. The sample can be run either manually or using a script file .

Run application using script file: run.sh provided. (For Bourne Shell):

By setting few environment variables, the sample application could be directly run by just executing the script file: run.sh from the command prompt, from SqljDMLSample directory. The user will be prompted to enter the environmental variables JAVA_HOME, SQL_EXE, SQLJ_LIB and JDBC_LIB when the script is run. Please look into Notations sections for more details on these environmental variables.

  • Go to SqljDMLSample directory and from the $ prompt, use the command below to run the script file:
    $sh run.sh

Running the application manually:

  • Set CLASSPATH to include:
    • Oracle9i JDBC Driver file: classes12.jar/zip
    • Oracle9i SQLJ classes: translator.jar/zip and runtime12ee.jar/zip
    • SqljDMLSample directory where Connection.properties exists(the current directory).
      Example:
      $export CLASSPATH=/home1/idcotn/download/jdbc/lib/classes12.zip:
      /home1/idcotn/download/sqlj/lib/translator.jar:
      /home1/idcotn/download/sqlj/lib/runtime12ee.jar:.
  • Make sure that Java and SQLJ (where sqlj is found) are in the PATH.
    Example: $export PATH=/usr/java/jdk1.3.1_02/bin:/home1/idcotn/download/sqlj/bin:$PATH
  • From the directory SqljDMLSample, translate all *.sqlj files to *.java files using sqlj:
    Example:
     
    $sqlj -compile=false /src/oracle/otnsamples/sqlj/dml/*.sqlj
  • From the directory SqljDMLSample, now compile all the java files using javac:
    Example:
    $javac -d . /src/oracle/otnsamples/sqlj/dml/SqljDMLSample/*.java
  • Run the class file using java from the same SqljDMLSample directory.
    Example:
    $java oracle.otnsamples.sqlj.dml.SqljDMLSample

Description of Sample Files 

Back To Top
The directory structure of the deliverable SqljDMLSample.jar will be as shown below. SqljDMLSample is the top level directory.

Directory
Files
Description
SqljDMLSample SqljDMLSample.jws The Oracle9i JDeveloper workspace file.
SqljDMLSample.jpr The Oracle9i JDeveloper project file.
Connection.properties This file has the details of the database connection parameters.
run.bat The batch file to compile and run the sample in Windows environment.
run.sh The shell script to compile and run the sample in Linux environment.
SqljDMLSample\doc Readme.html This file.
SqljDMLSample\src\oracle\otnsamples\sqlj\dml SqljDMLSample.sqlj The sqlj source file for sample.
SqljDMLFrame.java The source file for the sample User Interface.
PopulateTable.sqlj The sqlj source for the class which creates and populates the table required by the sample application in the database.
GenTableModel.java The source file for the GenTableModel class, which handles the JTable data.


Please enter your comments about this sample in the OTN Sample Code Discussion Forum.