ThreadSample Illustration


This document is organized into following sub-sections :

  1. Overview of the Sample Application
  2. Required Software
  3. Installation and Configuration
  4. Description of Sample Files

Overview of the Sample Application

This sample illustrates the use of threads in a JDBC application. Java Threads is a native java feature using which, two paths of execution
can execute concurrently in a co-operative manner. By using threads in a typical java application which performs database access and data processing, one can improve performance by having two threads, one performing data access, and other handling the processing of data simultaneously.

The aim of this application is to illustrate the typical use of Java Threads in a database application. It retrieves all the rows from AVAILABLE_ROOM_TYPES table, displays the rows and also computes the average rate and total number of rooms of each type. The database retrieval is done in one thread and the display and computation in another. The computation thread also performs a database operation (a join to get the hotel name from the hotel id).

This application uses the JDBC-Thin driver to connect to the database. JDBC-OCI driver can be used after changing the database URL.

Steps in the Sample

  1. The user invokes the application, and the application displays the connection status and any errors in the status bar.
  2. When the user presses the START button, 2 threads are fired, one of which does a query and fetches data from the database into a local buffer (vector). The second thread simultaneously processes data and computes average rate and number of rooms for each room type. The second thread maintains the GUI.
  3. When the user pressses the SUSPEND/RESUME button, both the threads are stopped so that the user can view the average of any type of room at any point of time.
  4. When the user presses Exit, the application exits.

Required Software

  • Oracle9i JDeveloper or higher ( Note: JDeveloper is Oracle's Visual Java Development Tool and can be downloaded from here )
    or

    JDK1.2.x or above (can be downloaded from here )
  • Oracle9i Database or higher running SQL*Net TCP/IP listener. This can be downloaded from here.
  • Travel Schema needs to be loaded into the database. This can be downloaded from here.

Installation and Configuration

The section has a set of for step-by-step instructions to run this application successfully. This will be discussed in the following subsections

Install the sample

Database setup

Run the sample using Oracle9i JDeveloper

Run the sample using the JDK environment

Install the sample

Step 1. Unjar ThreadSample.jar using the following command:

> jar xvf ThreadSample.jar

You will find jar.exe in JDK_HOME\bin. Make sure jar.exe is present in your system path. (JDK_HOME is the root directory of the JDKx.x installation ).

This creates a folder ThreadSample with all the source files

Step 2. Edit Connection.properties file located in the directory ThreadSample . Change the host name, port number and database name to those of the database instance you want to connect to.

Change the following lines:

HostName = incq063e.idc.oracle.com
SID = ora9i
Port = 1521
UserName = travel
Password = travel

Database Setup

The application requires the Travel Schema to be loaded into the database.

Run the sample using Oracle9i JDeveloper

Step 1. Using JDeveloper File/Open option select the ThreadSample.jws from the ThreadSample directory.

Step 2. Select Project/Make from JDeveloper main menu and select Make Project "ThreadSample.jpr"

Step 3. Select Run/Run<ThreadSample.jpr> from JDeveloper main menu to run the application.

Run the sample using the JDK Environment

 Step 1. Install Oracle JDBC Driver and include the zip/jar files in the CLASSPATH. Oracle JDBC 2.0 compliant drivers can be downloaded from Oracle Technology Network. Make sure that Connection.properties in the classpath

 Step 2.  Change directory to ThreadSample/oracle/otnsamples/jdbc/thread. Compile the files using javac:

               %javac *.java

 Step 3. Run the class file using java from ThreadSample directory:            
            
              %java oracle.otnsamples.jdbc.thread.ThreadSample

Description of Sample Files

The directory structure of the deliverable ThreadSample.jar will be as shown below

ThreadSample is the top level directory.

Directory
Files
Description
ThreadSample ReadMe.html This file
Connection.properties This file has the details of the database connection parameters
ThreadSample.jws The JDeveloper workspace file
ThreadSample.jpr The JDeveloper project file
ThreadSample\oracle\otnsamples\jdbc\thread ThreadSample.java The source file for sample
ThreadFrame.java The source file for the sample User Interface
GenTableModel.java The source file which handles the JTable data

 



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