4 Getting Started with the Application

To develop a Java application that connects to the Oracle Database, you must ensure that certain components are installed as required.

This chapter covers the following topics:

4.1 What You Need to Install

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

  • Oracle Database

  • J2SE or JDK

  • Apache Maven

  • IntelliJ

  • Apache Tomcat

4.1.1 Oracle Database

To develop the Java web application, you need a working installation of Oracle Database along with the HR schema. There are two ways you can install the Oracle Database.

Option 1. Oracle Autonomous Database

The Oracle Database Cloud Services offer access to Oracle Cloud Free Tier which provides services of two Autonomous Databases for an unlimited time. Oracle Autonomous Database is an all-in-one cloud database solution for data marts, data lakes, operational reporting, and batch data processing. Oracle uses machine learning to completely automate all routine database tasks—ensuring higher performance, reliability, security, and operational efficiency. You can get access to a working Autonomous Database in just a few minutes. After signing up, you will have access to your choice of Autonomous Transaction Processing or Autonomous Data Warehouse databases.

We will be using Autonomous Transaction Processing to run the HR Application.

Note:

Refer to Provisioning an ATP Database instance video for instructions.

Option 2: Oracle Database Express Edition (XE) Available on OTN

As an alternate option, you can install Oracle Database Express Edition (XE) on premise.

Note:

The Quick Start guide provides instructions on how to download and install the Oracle Database Express Edition (XE) on your machine.

4.1.2 Install the HR schema

The HR web application uses the tables and data from the HR sample schema provided by Oracle. You need to install the HR schema in your database.

Once you provision your Autonomous Database or install Oracle Databse XE, see Installing HR Schema for detailed instructions to install the sample schema.

4.1.3 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).

  1. Download and install the Java SE. Refer http://www.oracle.com/technetwork/java/javase/downloads/index.html.
  2. Set the PATH environment variable for JDK. Refer JDK Installation Instructions for Windows for detailed instructions.

4.1.4 JDBC Drivers

You need to download certain JDBC drivers for running the HR application.

This guide uses Maven Central to download the necessary JDBC drivers required for the application. Later in this guide, you learn to add the following dependency to your project's pom.xml file:

<dependencies>
  <dependency>
    <groupId>com.oracle.database.jdbc<groupId>
    <artifactId>ojdbc8-production</artifactId>
    <version>19.7.0.0</version>
    <type>pom</type>
  </dependency>
</dependencies>

The ojdbc8-production pulls all the required JDBC jars from the Maven Central Repository. For more information, see the Maven Central Guide.

4.1.5 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 IntelliJ Idea community version to create and update the files for this application.

Note:

To download and install IntelliJ Idea, see https://www.jetbrains.com/idea/.

4.1.6 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.

This guide uses the Apache Tomcat server for deploying the JSP pages.

For more information on how to download and install Apache Tomcat, refer to http://tomcat.apache.org/.

4.2 Verifying the Oracle Database Installation

Oracle Database installation is platform-specific. You must verify that the installation was successful before you proceed to create the sample application.

Verify the Oracle Database XE Installation

  1. Verify the Oracle Database XE installation by connecting to the database.
  2. From the command prompt, connect to sqlplus.
    >sqlplus system/<password>@xepdb1
    
    SQL*Plus: Release 18.0.0.0.0 - Production on Wed Sep 9 16:01:34 2020
    Version 18.4.0.0.0
    
    Copyright (c) 1982, 2018, Oracle.  All rights reserved.
    
    Connected to:
    Oracle Database 18c Express Edition Release 18.0.0.0.0 - Production
    Version 18.4.0.0.0
    SQL>
  3. As an optional step, you can run a sample Java application with Oracle Database using JDBC. See Quick Start with JDBC.

Verify connecting to Oracle ATP Instance

Once you provision an ATP instance, verify the connection as follows:

  1. Download the Client Credentials. Login to your database instance and click on the DB Connection tab. Select Instance Wallet for Wallet type and click Download Wallet. Unzip the file to a secure directory on your local machine.
  2. Follow instructions provided in the Java Connectivity with Autonomous Database to connect using JDBC and UCP code samples.

4.3 Github Repository Details

The source files for the HR Web Application is available on GitHub.

Table 4-1 Github Repository Details

Name and Location Details
HRWebApp This repository contains the complete code samples of the application.

4.4 Import the Application in IntelliJ

Import the application in IntelliJ as follows:

  1. Open IntelliJ and select Open or Import.
  2. Navigate to the location where the HRWebApp is downloaded. Select HRWebApp and click OK.Image of window explorer to browse through folder structure.
  3. A project with all the files required to build the HR Web application is displayed.

4.5 Compile the Application in IntelliJ

The HR Web Application easily compiles using Maven in IntelliJ.

The code requires a few updates before you compile the application.
The HRWebApp must be downloaded and opened in IntelliJ.
  1. In the Project window on the left, double-click on pom.xml file.
    Select pom.xml
  2. Add the following dependency under the <dependencies> tag in pom.xml file. See JDBC Drivers for more information.
    Add the dependency for ojdbc8-production.
  3. In the Project window, expand the src folder, expand main and then Java folder. Under Java, expand com.oracle.jdbc.samples. In the bean folder, double-click JdbcBeanImpl.
    Select JdbcBeanImpl.
  4. Update the connection variable with the details of your database connection. For example, to connect to the HR schema on the Oracle XE database instance xepdb1 with port 1522 on your machine, edit the url parameter as follows:
    Connection connection =
            DriverManager.getConnection("jdbc:oracle:thin:@localhost:1522/xepdb1", "hr", "hr");
    Update the database connection details
  5. In the File menu, select Save All to save the changes.
  6. In the Maven window on the right, double-click on clean to clean the source code. The build progress is shown in the Run window at the bottom.Maven lifecycle steps
  7. Similarly, double-click on compile and package in the same order.
  8. Once the source code is packaged successfully, a war file is generated. Locate the JdbcWebSamples.war under the target folder.
    Image of JdbcWebSamples.war under target folder

4.6 Run the HR Web Application

The HR Web application is run using the Tomcat server.

Deploy the .war file on the Apache server
  1. Navigate to the HRWebApp folder on your local machine. Under the target folder, locate JdbcWebSamples.war file.
  2. Place JdbcWebSamples.war file under TOMCAT_HOME/webapps/.
  3. Navigate to the HRWebApp folder on your local machine. Locate tomcat-users.xml file.
  4. Place tomcat-users.xml file under TOMCAT_HOME/conf/.
  5. Start the tomcat server.
  6. Once the tomcat is started, access the HR web application from a browser using the URL http://localhost:8080/JdbcWebSamples/.
Verify the HR Web Application
  1. Login to the application with either hradmin or hrstaff user.

    Note:

  2. HR Web Application has several functionalities. List All displays the employees' details such as Employee_id, First_name, Last_Name, Email, Phone_number, Job_id, Salary etc. The details are retrieved from the Employees table and displayed on a web page. See the screenshot below that shows List All functionality.
  3. Similarly, verify the remaining functionalities such as Search by ID, Update Employee Record and Increment Salary by giving appropriate inputs.