Build a Java Application

To build a Java application that accesses an Autonomous Database, you start by configuring your development system to support database access that can take advantage of the continuous availability and high performance features of Autonomous Database.

After configuring your development system to support database access, you code database connections and SQL statements in your application to take advantage of the continuous availability and high performance features.

Tip:

For a "try it out" alternative to reading the following topics, you can go through the Lab 5: Build Java Application Stacks in Oracle Autonomous Database Dedicated for Developers and Database Users Workshop.

Configure Your Java Development System

To configure your development system so that your Java application can take advantage of the continuous availability and high performance features of an Autonomous Database, perform these steps.

  1. Download and install the Java Development Kit (JDK).
  2. Download the client credentials for your Autonomous Database.
  3. Get the Oracle Java Database Connectivity (JDBC) drivers.

Before You Begin

Your development system must meet certain criteria to configure it successfully.

Download and Install the JDK

Go to the Java SE Downloads page. Then, download and install JDK 8u221 or later by following the instructions on the page.

Download the Client Credentials for Your Autonomous Database

  1. Download the zip file containing client credentials for your database to a secure directory on your computer.

    This zip file is available for download from the database's Details page in the Oracle Cloud console. Download the credentials as follows.

    1. In your web browser, sign in to Oracle Cloud and navigate to the Details page for the Autonomous Database.

    2. Click DB Connection.

    3. On the Database Connection page click Download.

    4. In the Download Wallet dialog, enter a password in the Password field and confirm the password in the Confirm Password field.

      The password must be at least 8 characters long and must include at least 1 letter and either 1 numeric character or 1 special character.

    5. Click Download and unzip, to save the client credentials zip file to a secure directory.

Get the Oracle JDBC Drivers

Get the Oracle JDBC drivers, version 19.6.0.0 or later, from either Maven Central or the JDBC Downloads page at Oracle Technical Resources. (See the Oracle Technologies JDBC Home page for related videos and other resources.)

To get the JDBC drivers from Maven Central, follow these steps.

  1. Get the Oracle JDBC drivers from Central Maven Repository.

    Provide the driver Maven dependency GAV (GroupID, ArtifactID, VersionID), to pull ojdbc8.jar, along with other jars such as oraclepki.jar, osdt_core.jar, and osdt_cert.jar. See Maven Central Guide.

    For ojdbc8.jar version 19.6.0.0, provide this GAV:

    <groupId>com.oracle.database.jdbc</groupId>
    <artifactId>ojdbc8</artifactId>
    <version>19.7.0.0</version>

    For ojdbc8.jar version 19.7.0.0, provide this GAV:

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

To get the JDBC drivers from Oracle Technical Resources, follow these steps.

  1. Go to the Oracle JDBC Downloads page. Then, choose the latest version of the drivers to go to its version-specific download page.

  2. Download and unzip this archive to the directory where you want to place the JDBC driver: ojdbc8-full.tar.gz.

  3. Point the connection URL to your Autonomous Database.

    Append TNS_ADMIN to the connection URL, setting its value to the full path of the directory where you unzipped the client credentials. For example:

    // Use TNS alias name plus TNS_ADMIN with JDBC driver 18.3 or higher
    DB_URL="jdbc:oracle:thin:@wallet_dbname?
    TNS_ADMIN=/Users/test/wallet_dbname";
    
    // For Microsoft Windows, use this for TNS_ADMIN:
    // TNS_ADMIN=C:\\Users\\test\\wallet_dbname”;
  4. Add the paths to the following unzipped JAR files to the CLASSPATH environment variable you use when you compile and run Java programs.

    Use DataSourceSample.java or UCPSample.java to verify the connection to your Autonomous Database.

    • ojdbc8.jar: the core JDBC driver
    • oraclepki.jar, osdt_core.jar, and osdt_cert.jar: for an Autonomous Database that uses wallet-based authentication
    • ucp.jar: for Universal Connection Pooling (UCP)
    • ons.jar and simplefan.jar: for FAN (Fast Application Notification) support

Code Database Connections and SQL Statements

After configuring your development system to support Java application connectivity to an Autonomous Database, follow these guidelines to achieve high performance and continuous availability of your application's connections to the database:

For example:

import java.sql.Connection;
import javax.sql.PooledConnection;
import oracle.jdbc.OracleConnection;
import oracle.jdbc.replay.OracleDataSourceFactory;
import oracle.jdbc.replay.OracleDataSource;
import oracle.jdbc.replay.OracleConnectionPoolDataSource;
...
  PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource();
  // Set the connection factory first before all other properties
  pds.setConnectionFactoryClassName(
    "oracle.jdbc.replay.OracleConnectionPoolDataSourceImpl");
  pds.setFastConnectionFailoverEnabled(true);
  pds.setURL("jdbc:oracle:thin:@tp_tls?TNS_ADMIN=/users/jdoe/adbcredentials");
  pds.setUser("appuser");
  pds.setPassword("<password>");
  pds.setConnectionPoolName("JDBC_UCP_POOL");

  Connection conn = pds.getConnection();

Additional Resources

For detailed information about the Oracle Database JDBC Driver, see Oracle Database JDBC Developer's Guide and Oracle Database JDBC Java API Reference.

For detailed information about the Universal Connection Pool, see Oracle Universal Connection Pool Developer's Guide and Oracle Universal Connection Pool API Reference.