2 Getting Started with the Application

To develop a Java application that connects to Oracle Database 12c Release 1 (12.1), you must ensure that certain components are installed as required. This chapter covers the following topics:

2.1 What You Need to Install

To be able to develop the sample application, you need to install the following products and components:

The following subsections describe these requirements in detail.

2.1.1 Oracle Database 12c Release 1 (12.1)

To develop the Java application, you need a working installation of Oracle Database 12c Release 1 (12.1) Server with the HR schema, which comes with the database. The installation creates an instance of Oracle Database 12c Release 1 (12.1) and provides additional tools for managing this database. For more information, refer to the following Oracle Database 12c Release 1 (12.1) installation guides and release notes:

2.1.1.1 Unlocking the HR Schema for the JDBC Application

The HR user account, which owns the sample HR schema used for the Java application in this guide, is initially locked. You must log in as a user with administrative privileges (SYS) and unlock the account before you can log in as HR.

If the database is locally installed, use the Run SQL Command Line to unlock the account as follows:

  1. To access the Run SQL Command Line, from the Start menu, select Programs (or All Programs), then Oracle Database 12c Release 1 (12.1), and then click Run SQL Command Line. Log in as a user with DBA privileges, for example:

    > CONNECT SYS AS SYSDBA;
    Enter password: password
    
  2. Run the following command:

    > ALTER USER HR ACCOUNT UNLOCK;
    

    or,

    > ALTER USER HR IDENTIFIED BY HR;
    
  3. Test the connection as follows:

    > CONNECT HR
    Enter password: password
    

You should see a message indicating that you have connected to the database.

Note:

For information about creating and using secure passwords with Oracle Database 12c Release 1 (12.1), refer to Oracle Database Security Guide.

In addition, some of the constraints and triggers present in the HR schema are not in line with the scope of the Java application created in this guide. You must remove these constraints and triggers as follows using the following SQL statements:

DROP TRIGGER HR.UPDATE_JOB_HISTORY;
DROP TRIGGER HR.SECURE_EMPLOYEES;
DELETE FROM JOB_HISTORY;

2.1.2 J2SE or JDK

To create and compile Java applications, you need the full Java 2 Platform, Standard Edition, Software Development Kit (J2SE SDK), formerly known as the Java Development Kit (JDK). You also need the Java Runtime Environment (JRE).

Note:

Oracle Database 12c Release 1 (12.1) supports JDK 6 and JDK 7.

See Also:

2.1.3 Integrated Development Environment

For ease in developing the application, you can choose to develop your application in an integrated development environment (IDE). This guide uses Oracle JDeveloper to create the files for this application. For more information about installing JDeveloper, refer to Installing Oracle JDeveloper.

2.1.4 Web Server

The sample application developed in this guide uses JavaServer Pages (JSP) technology to display information and accept input from users. To deploy these pages, you need a Web server with a servlet and JSP container, such as the Apache Tomcat application server.

This guide uses the embedded server called the Oracle WebLogic Server in JDeveloper for deploying the JSP pages. If you choose not to install Oracle JDeveloper, then any Web server that enables you to deploy JSP pages should suffice.

JDeveloper supports direct deployment to the following production application servers:

  • Oracle WebLogic Server

  • Oracle Application Server

  • Apache Tomcat

  • IBM WebSphere

  • JBoss

For more information about these servers, please refer to vendor-specific documentation.

2.2 Verifying the Oracle Database 12c Release 1 (12.1) Installation

Oracle Database 12c Release 1 (12.1) installation is platform-specific. You must verify that the installation was successful before you proceed to create the sample application. This section describes the steps for verifying an Oracle Database 12c Release 1 (12.1) installation.

Verifying a installation involves the following tasks:

2.2.1 Checking Installed Directories and Files

Installing Oracle Java products creates the following directories:

  • ORACLE_HOME/jdbc

  • ORACLE_HOME /jlib

Check if the directories described in Table 2-1 have been created and populated in the ORACLE_HOME directory.

Table 2-1 Directories and Files in the ORACLE_HOME Directory

Directory Description

$OH/jdbc/lib

The lib directory contains the ojdbc6.jar and ojdbc7.jar required Java classes. These contain the JDBC driver classes for use with JDK 6 and JDK 7.

$OH/jdbc/Readme.txt

This file contains late-breaking and release-specific information about the drivers, which may not have been included in other documentation on the product.

$OH/jlib

This directory contains the orai18n.jar file. This file contains classes for globalization and multibyte character sets support.


Note:

Use the ojdbcn.jar file (where 'n' is the release number) supplied with Oracle Database Installation Guide, or Oracle Database Client Installation Guide.

2.2.2 Checking the Environment Variables

This section describes the environment variables that must be set for the JDBC Thin Driver. You must set the classpath for your installed JDBC Thin Driver. For JDK 6, you must set the following values for the CLASSPATH variable:


ORACLE_HOME/jdbc/lib/ojdbc6.jar
ORACLE_HOME/jlib/orai18n.jar

For JDK 7, you must set the following values for the CLASSPATH variable:


ORACLE_HOME/jdbc/lib/ojdbc7.jar
ORACLE_HOME/jlib/orai18n.jar

Ensure that there is only one JDBC class file, such as ojdbc6.jar, and one globalization classes file, orai18n.jar, in the CLASSPATH variable.

2.2.3 Determining the JDBC Driver Version

Starting from Oracle Database 12c Release 1 (12.1), you can get details about the JDBC support in the database as follows:

> java -jar ojdbc6.jar
  Oracle 12.1.0.0. JDBC 4.0 compiled with JDK6

In addition, you can determine the version of the JDBC driver that you installed by calling the getDriverVersion method of the OracleDatabaseMetaData class.

Note:

The JDBC Thin Driver requires a TCP/IP listener to be running on the computer where the database is installed.

Example 2-1 illustrates how to determine the driver version:

Example 2-1 Determining the JDBC Driver Version

import java.sql.*;
import oracle.jdbc.*;
import oracle.jdbc.pool.OracleDataSource;

class JDBCVersion
{
  public static void main (String args[]) throws SQLException
  {
    OracleDataSource ods = new OracleDataSource();
    ods.setURL("jdbc:oracle:thin:hr/hr@localhost:1521/oracle");
    Connection conn = ods.getConnection();

    // Create Oracle DatabaseMetaData object
    DatabaseMetaData meta = conn.getMetaData();

    // gets driver info:
    System.out.println("JDBC driver version is " + meta.getDriverVersion());
  }
}

2.3 Installing Oracle JDeveloper

In this guide, the integrated development environment (IDE) that is used to create the sample Java application using JDBC is Oracle JDeveloper release 11.1.1. This release of JDeveloper is supported on the Microsoft Windows Vista, Windows XP, Windows 2003, Windows 2000, Linux, and Mac OS X operating systems. Installation of the latest version of JDeveloper is described in detail in Installation Guide for Oracle JDeveloper, which is available online on the Oracle Technology Network at

https://download.oracle.com/docs/cd/E12839_01/install.1111/e13666/toc.htm

Also read JDeveloper 11g Release Notes, which is available online on the Oracle Technology Network at

http://www.oracle.com/technetwork/developer-tools/jdev/overview/index.html