SQLJ: Rowid Sample Application
Table Of Contents
This sample application illustrates accessing the
ROWID of a database row 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.
ROWIDs map directly to data blocks, and hence are unique
for each row in a table; they are also the fastest way to access database
rows.
Each row in the database has an address. A row's address can be examined
by querying the pseudocolumn: ROWID. Values of this
pseudocolumn are strings representing the address of each row which have
the datatype ROWID.
This sample application tries to demonstrate the database
performance to retrieve records by using rowid and without using rowid.
With a large number of records in a table, using rowid for retrieving
records could better the performance.
Working of the Sample
The sample application uses a database table OTN_AIRLINES.
When the application is invoked, the table is created if it does not
exist. The status of the connection is shown
in the status bar. Errors if any, are shown in the status bar.
The user may choose or may not choose to use rowid
to retrieve records. The time taken to retrieve records is displayed
at the end of the retrieval.
Here is the SQLJ code usage for using ROWID in a
SQL statement. You can find more details of the code in SqljRowidSample.sqlj
file under src/oracle/otnsamples/sqlj/rowid
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;
}
|
|
Selecting records with/without ROWID in the SQL statement:
/**
* Retrieve the NAME and PARTNER values for the selected row using its ROWID.
**/
private void selectWithRowid(ROWID rowid) {
String name = null, partner = null;
// Note time before start of retrieval.
long before = System.currentTimeMillis();
try {
// Embedded SQL: This call fetches the name and partner values
// from the specified row. The row to be retrieved is identified
// by its ROWID.
// The result of the query is bound into the class variables name
// and partner, while the rowid for selection is bound from the class
// variable rowid.
#sql {SELECT name, partner INTO :name, :partner FROM OTN_AIRLINES
WHERE rowid = :rowid };
} catch (SQLException ex) { // Trap SQL errors.
............
}
//Get time at completion.
long after=System.currentTimeMillis();
// Compute Time taken and copy to textfield.
Integer time = new Integer((int)(after - before));
....................
}
/**
* Retrieve the NAME and PARTNER values for the selected row using the Airline
* CODE.
**/
private void selectWithoutRowid(String code) {
String name = null, partner = null;
// Note time before start of retrieval.
long before = System.currentTimeMillis();
try {
// Embedded SQL: This call fetches the name and partner values
// from the specified row. The row to be retrieved is identified
// by its airline CODE.
// The result of the query is bound into the class variables name
// and partner, while the code for selection is bound from the class
// variable code.
#sql {SELECT name, partner INTO :name, :partner FROM OTN_AIRLINES
WHERE code = :code };
} catch (SQLException ex) { // Trap SQL errors.
...................
}
//Get time at completion.
long after=System.currentTimeMillis();
// Compute Time taken and copy to textfield.
Integer time = new Integer((int)(after - before));
................
}
|
|
Note The time taken
for retrieving OTN_AIRLINES records using CODE and ROWID may not be consistent;
the reason being small number of rows in OTN_AIRLINES table. If a table
has a large number of rows, then ROWID retrieval will take lesser time
consistently.
- 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.
| 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
|
- Unjar the provided SqljRowidSample.jar
using the following command
> jar xvf SqljRowidSample.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 SqljRowidSample
with all the source files.
- Edit SqljRowidSample/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 |
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 SqljRowidSample.jws
from the SqljRowidSample
directory.
- Next, select Project/Make
SqljRowidSample.jpr
from main menu.
- Now, select Run/Run SqljRowidSample.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 SqljRowidSample
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:\SqljRowidSample>
set SQLJ_EXE_HOME=d:\sqlj\bin
D:\SqljRowidSample>
set SQLJ_LIB=d:\sqlj\lib
D:\SqljRowidSample>
set JDBC_LIB=d:\oracle9i\jdbc\lib
D:\SqljRowidSample>
set JAVA_HOME=d:\jdk1.3.1
D:\SqljRowidSample>
run
Running the application
manually:
-
Set CLASSPATH to include:
- Make sure that Java and SQLJ(where
sqlj is found) are
in the PATH.
Example: D:\SqljRowidSample>set PATH=.;d:\jdk1.3.1\bin;d:\sqlj\bin;%PATH%
- From the directory SqljRowidSample,
translate all *.sqlj
files to *.java
files using sqlj:
Example:
D:\SqljRowidSample>sqlj
-compile=false src\oracle\otnsamples\sqlj\rowid\*.sqlj
- From the same SqljRowidSample
directory, now compile all *.java
files using javac:
Example:
D:\SqljRowidSample>javac
-d . src\oracle\otnsamples\sqlj\rowid\*.java
- Run the class file using java from the
same SqljRowidSample
directory.
Example:
D:\SqljRowidSample>java
oracle.otnsamples.sqlj.rowid.SqljRowidSample
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 SqljRowidSample
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 SqljRowidSample
directory and from the $
prompt, use the command below to run the script file:
$sh run.sh
Running the application
manually:
The directory structure of the deliverable SqljRowidSample.jar
will be as shown below. SqljRowidSample
is the top level directory.
|
Directory
|
Files
|
Description
|
| SqljRowidSample |
SqljRowidSample.jws |
The Oracle9i
JDeveloper workspace file. |
| SqljRowidSample.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. |
| SqljRowidSample\doc |
Readme.html |
This file. |
| SqljRowidSample\src\oracle\otnsamples\sqlj\rowid |
SqljRowidSample.sqlj |
The sqlj source file for the sample. |
| SqljRowidFrame.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. |
|