SQLJ: DATE Sample Application
Table Of Contents
This sample application illustrates the accessing SQL
DATE data type access through SQLJ calls.
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 Order Delivery
system in which the proposed and the actual delivery dates of goods are
tracked by the application. The user can add new orders with the proposed
delivery date and at a later point of time update this record with the
actual delivery date. The orders are listed for the user to browse/update.
Working of the Sample
The sample application uses a database table OTN_DELIVERYDETAILS.
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.
Pressing the 'New' button can create New order. Order
Id and the Proposed Delivery Date needs to be entered mandatorily. The
Actual Delivery Date value is optional. The news order can be persisted
in the database by pressing the 'Add' button. Only the Actual Delivery
Date for the existing Orders can be updated.
Here is the code usage for creating Sqlj Default
Context with a database connection and also for inserting an Order into
the database. You can find more details of the code in SqljDateSample.sqlj
file under src/oracle/otnsamples/sqlj/datetype
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;
}
|
|
Insert method:
/**
* This method persists the delivery details to the database using SQLJ
* statement.
*/
private void insertDeliveryData( ) {
// To parse dates from String, Ex: 05-SEP-1980
SimpleDateFormat formatter = new SimpleDateFormat( "dd-MMM-yyyy" );
Date proposedDate = null;
Date actualDate = null;
try {
// Parse the date from user entered String
proposedDate = new Date(formatter.parse(gui.getProposedDate()).getTime());
actualDate = new Date(formatter.parse(gui.getActualDate()).getTime());
} catch( ParseException parseEx ) { }
...........
...........
try {
#sql { INSERT INTO otn_deliverydetail VALUES( :id, :proposedDate,
:actualDate)} ;
} catch (Exception sqlEx) {
}
|
|
- 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 SqljDateSample.jar
using the following command
> jar xvf SqljDateSample.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 SqljDateSample
with all the source files.
- Edit SqljDateSample/Connection.properties
file in your favourite editor. Change the HostName, Port,
SID, UserName and Password to
connect to your own database.
| HostName |
= |
localhost |
| 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 SqljDateSample.jws
from the SqljDateSample
directory.
- Next, select Project/Make
SqljDateSample.jpr
from main menu.
- Now, select Run/Run SqljDateSample.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 SqljDateSample
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:\SqljDateSample>
set SQLJ_EXE_HOME=d:\sqlj\bin
D:\SqljDateSample>
set SQLJ_LIB=d:\sqlj\lib
D:\SqljDateSample>
set JDBC_LIB=d:\oracle9i\jdbc\lib
D:\SqljDateSample>
set JAVA_HOME=d:\jdk1.3.1
D:\SqljDateSample>
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:\SqljDateSample>set
PATH=.;d:\jdk1.3.1\bin;d:\sqlj\bin;%PATH%
- From the directory SqljDateSample,
translate all *.sqlj
files to *.java
files using sqlj:
Example:
D:\SqljDateSample>sqlj
-compile=false src\oracle\otnsamples\sqlj\datetype\*.sqlj
- From the same SqljDateSample
directory, now compile all *.java
files using javac:
Example:
D:\SqljDateSample>javac
-d . src\oracle\otnsamples\sqlj\datetype\*.java
- Run the class file using java from the
same SqljDateSample
directory.
Example:
D:\SqljDateSample>java
oracle.otnsamples.sqlj.datetype.SqljDateSample
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 SqljDateSample
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 SqljDateSample
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 SqljDateSample.jar
will be as shown below. SqljDateSample
is the top level directory.
|
Directory
|
Files
|
Description
|
| SqljDateSample |
SqljDateSample.jws |
The Oracle9i
JDeveloper workspace file. |
| SqljDateSample.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. |
| SqljDateSample\doc |
Readme.html |
This file. |
| SqljDateSample\src\oracle\otnsamples\sqlj\datetype |
SqljDateSample.sqlj |
The sqlj source file for sample. |
| SqljDateFrame.java |
The source file for the sample User
Interface. |
|