1 Prerequisites

The following sections provide information about the tasks that you need to perform before establishing a connection to Oracle Database.

1.1 Installing Oracle Database

This section lists a few resources that you can use to install Oracle Database Express Edition (XE) 21c. If you have already installed Oracle Database and have the credentials, then you can skip this step.

1.2 Installing JDK 8

This section lists the step to install JDK 8.

Install JDK 8 or a higher JDK version that is a long-term release. For example, JDK 11 or JDK 17.

1.3 Creating a Database User (Optional)

This section lists the steps to create a database user.

Note:

If you already have a database user and password, then you can skip this step.

It is recommended to create a new database user when verifying the database connection. If you are not familiar with creating a database user and assigning privileges, then you can use the CreateUser.java file to create a new database user.

  1. Download and edit the file CreateUser.java, provide the connection string, admin user, and admin password along with the new database user and password to be created.
  2. Download the latest Oracle JDBC driver and UCP.
  3. Make sure that Oracle Database is running before compiling and running the sample.
    # Compile the Java program 
     javac -classpath ./test/ojdbc8.jar:./test/ucp.jar CreateUser.java 
    # Run the Java program 
     java -classpath ./test/ojdbc8.jar:./test/ucp.jar:. CreateUser

    Alternately, you can use the following commands to create a new database user if you are using Docker.

    # These are instructions to create a new database user  
    #Step 1: Make sure docker is running before running the below docker command
    #Step 2: Enter the admin password for your database on the console
    #Step 3: Provide the new database user and password to be created
    
    # Create a session within the container with Oracle as user 
    docker exec -it --user oracle \
       -e 'ORACLE_HOME=/opt/oracle/product/18c/dbhomeXE' \
       $(docker ps --format '{{.ID}}') \
       /bin/bash -c  'exec ${ORACLE_HOME}/bin/sqlplus "sys@xepdb1 as sysdba"'
    
    # Create a new database user and grant required privileges. 
    # Replace "{newdbuser}"  and "{newdbpassword}" with the new DB user and password that you want to create
    grant CREATE SESSION, CREATE VIEW, CREATE SEQUENCE,CREATE PROCEDURE, CREATE TABLE, 
    CREATE TRIGGER, CREATE TYPE, CREATE MATERIALIZED VIEW to {newdbuser} identified by {newdbpassword};

1.4 Downloading a Sample Java Program from Github

This section lists the steps to download a sample Java Program from Github.

  1. Download the QuickStart.java file from Github. This sample application creates a table todoitem that contains the tasks and the task completion status, inserts a few rows, and displays the contents of the table.
  2. Edit the QuickStart.java file to include the following database connection information:
    • DB_USER: Use the database user
    • DB_PASSWORD: Use the database password
    • DB_URL: Enter the connection string for Oracle Database. For Oracle Database XE, use the following connection string:
      Example : DB_URL = "jdbc:oracle:thin:@//localhost:1521/XEPDB1"
  3. Save the changes to the QuickStart.java file.