Skip Headers
Oracle® Database 2 Day + Java Developer's Guide
11g Release 2

Part Number E12137-01
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Master Index
Master Index
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
View PDF

1 Using Java with Oracle Database

Oracle Database is a relational database that you can use to store, use, and modify data. The Java Database Connectivity (JDBC) standard is used by Java applications to access and manipulate data in relational databases.

JDBC is an industry-standard application programming interface (API) developed by Sun Microsystems that lets you embed SQL statements in Java code. JDBC is based on the X/Open SQL Call Level Interface (CLI) and complies with the Entry Level of the SQL-92 standard. Each vendor, such as Oracle, creates its JDBC implementation by implementing the interfaces of the standard java.sql package.

See Also:

This guide shows you how to use a simple Java application to connect to Oracle Database and access and modify data within the database. Further, it uses the Oracle Application Development Framework (ADF) to develop a master-detail application to display employee data.

This chapter introduces you to the Java application created in this guide, and to the tools you can use to develop the Java application in the following topics:

1.1 Using Java to Connect to Oracle Database

JDBC is a database access protocol that enables you connect to a database and run SQL statements and queries on the database. The core Java class libraries provide the JDBC APIs, java.sql and javax.sql. However, JDBC is designed to allow vendors to supply drivers that offer the necessary specialization for a particular database.

Note:

Oracle Database 11g Release 2 support JDK 5 and onward. The JDBC support in this release includes the ojdbc5.jar and ojdbc6.jar files. The ojdbc6.jar file offers JDBC 4.0 compliance. To use this file, you need JDK 6.

Oracle Database provides support for the client-side application development through the JDBC Thin Driver and the Oracle Call Interface (OCI) Driver, and the oracle.sql and oracle.jdbc packages. The classes and interfaces in these packages extend the JDBC standard. They allow you to access and modify Oracle data types and use Oracle performance extensions for JDBC with greater flexibility in a Java application.

The following sections describe Oracle support for the JDBC standard:

1.1.1 Oracle JDBC Thin Driver

Oracle recommends using the JDBC Thin Driver for most requirements. JDBC-OCI is only needed for OCI-specific features.

The JDBC Thin Driver is a pure Java, Type IV driver. It supports the JavaTM 2 Platform Standard Edition 5.0, also known as Java Development Kit (JDK) 5. It also includes support for JDK 6. It is platform-independent and does not require any additional Oracle software for client-side application development. The JDBC Thin Driver communicates with the server using SQL*Net to access Oracle Database.

The JDBC Thin Driver allows a direct connection to the database by providing a pure Java implementation of Oracle network protocols (Two-Task Common, also known as the TTC protocol, and SQL*Net). The driver supports the TCP/IP protocol and requires a Transparent Network Substrate (TNS) listener on the TCP/IP sockets on the database server. The Thin driver will work on any machine that has a suitable Java virtual machine (JVM).

You can access the Oracle-specific JDBC features and the standard features by using the oracle.jdbc package.

1.1.2 Oracle JDBC OCI Driver

The JDBC OCI driver is a Type II driver used with Java applications. It requires an Oracle client installation. It supports all installed Oracle Net adapters, including interprocess communication (IPC), named pipes, TCP/IP, and InternetworkPacket Exchange/Sequenced Packet Exchange (IPX/SPX).

OCI is an API that enables you to create applications that use native procedures or function calls. The JDBC OCI driver, written in a combination of Java and C, converts JDBC calls to calls to OCI. It does this by using native methods to call C-entry points. These calls communicate with the database using SQL*Net.

1.1.3 Oracle JDBC Packages

Oracle support for the JDBC API is provided through the oracle.jdbc and oracle.sql packages. These packages support all Java Development Kit (JDK) releases from 1.5 through 1.6.

oracle.sql

The oracle.sql package supports direct access to data in SQL format. This package consists primarily of classes that provide Java mappings to SQL data types and their support classes. Essentially, the classes act as Java wrappers for SQL data. The characters are converted to Java chars and, then, to bytes in the UCS-2 character set.Each of the oracle.sql.* data type classes extends oracle.sql.Datum, a superclass that includes functions and features common to all the data types. Some of the classes are for JDBC 2.0-compliant data types. In addition to data type classes, the oracle.sql package supports classes and interfaces for use with objects and collections.

oracle.jdbc

The interfaces of the oracle.jdbc package define the Oracle extensions to the interfaces in the java.sql package. These extensions provide access to Oracle SQL-format data. They also provide access to other Oracle-specific features, including Oracle performance enhancements.

The key classes and interfaces of this package provide methods that support standard JDBC features and perform tasks such as:

  • Returning Oracle statement objects

  • Setting Oracle performance extensions for any statement

  • Binding oracle.sql.* types into prepared and callable statements

  • Retrieving data in oracle.sql format

  • Getting meta information about the database and result sets

  • Defining integer constants used to identify SQL types

1.2 Using JDeveloper to Create JDBC Applications

The Java application tutorial in this guide uses Oracle JDeveloper 10g release 10.1.3 as the integrated development environment (IDE) for developing the Java application and creating Web pages for users to view and change the data.

Oracle JDeveloper is an IDE with support for modeling, developing, debugging, optimizing, and deploying Java applications and Web services.

JDeveloper provides features for you to write and test Java programs that access the database with SQL statements embedded in Java programs. For the database, JDeveloper provides functions and features to do the following:

1.2.1 JDeveloper User Interface

Oracle JDeveloper is an IDE that uses windows for various application development tools. You can display or hide any of the windows, and you can dock them or undock them to create a desktop suited to your method of working.

In addition to these tools, JDeveloper provides a range of navigators to help you organize and view the contents of your projects. Application and System navigators show you the files in your projects, and a Structure window shows you the structure of individual items.

You can arrange the windows as you choose, and can close and open them from the View menu. Figure 1-1 shows the default layout of some of the available navigators, palettes, and work areas in the JDeveloper user interface (GUI).

Figure 1-1 JDeveloper User Interface

JDeveloper user interface
Description of "Figure 1-1 JDeveloper User Interface"

See Also:

Working with Windows in the IDE, in the JDeveloper online Help

1.2.2 JDeveloper Tools

For creating a Java application, JDeveloper provides the following tools to simplify the process:

  • Structure window, which provides a tree view of all of the elements in the application currently being edited be it Java, XML, or JSP/HTML.

  • Java Visual Editor, which you can use to assemble the elements of a user interface quickly and easily.

  • JSP/HTML Visual Editor, which you can use to visually edit HTML and JSP pages.

  • Java Source Editor, which provides extensive features for helping in writing the Java code, such as distinctive highlighting for syntax and semantic errors, assistance for adding and sorting import statements, the Java Code Insight feature, and code templates.

    Note:

    The Java Code Insight feature is a facility that provides context-specific, intelligent input when creating code in the Java Source Editor. In this guide, you will see many instances of how you can use Java Code Insight to insert code.
  • Component Palette, from which you select the user interface components, such as buttons and text areas, that you want to display on your pages.

  • Property Inspector, which gives a simple way of setting properties of items such as user interface components.

Figure 1-1 might help you get a better idea of where you can access these tools in the JDeveloper UI.

1.3 Overview of Sample Java Application

This guide shows you how to create an application using Java, JDBC and Oracle ADF. In this application, you build in the following functions and features:

  1. Allow users to log in and validate the user name and password.

  2. Establish a connection to the database.

  3. Query the database for data and retrieve the data using a JavaBean.

  4. Display the data using JavaServer Pages (JSP) technology.

  5. Allow users to insert, update, or delete records.

  6. Access and modify information from a master-detail application.

  7. Handle exceptions.

Note:

The application connects to the HR schema that ships with Oracle Database. Although the Oracle Database client installation comes with both the Thin and OCI drivers, the sample application will use only the JDBC Thin Driver.

Overview of Application Web Pages (JSP Pages)

Figure 1-2 shows the relationships among the pages developed for this application.

Figure 1-2 Web Pages in the Sample Application

Description of Figure 1-2 follows
Description of "Figure 1-2 Web Pages in the Sample Application"

A brief description of the Web pages in the sample application follows:

Classes

The sample application includes the following classes:

Note:

This application is developed throughout this guide in the form of a tutorial. It is recommended, therefore, that you read these chapters in sequence.

1.4 Advanced Application Development Using Developer Frameworks

To develop enterprise solutions that search, display, create, modify, and validate data using web, wireless, desktop, or web services interfaces, you need to use developer frameworks to simplify your job.

Using frameworks, developers can write code based on well-defined interfaces. This is largely a time-saving benefit, but it also makes sense in a Java EE environment because Java EE frameworks provide the necessary infrastructure for the enterprise application. In other words, Java EE frameworks make the concepts expressed in the Java EE design patterns more concrete.

The Oracle Application Development Framework (Oracle ADF) is such an end-to-end application framework that builds on Java EE standards and open-source technologies to simplify and accelerate implementing service-oriented applications.

To illustrate how application development can be made easy using a feature-rich environment that facilitates the creation of complex applications, this guide includes a master-detail application in Chapter 7.