Connect to Oracle Autonomous Database Using JDBC with IntelliJ

You can establish a connection to Oracle Autonomous Database using the IntelliJ IDE. The following sections provide information about how to first connect to Oracle Autonomous Database using a Maven project or a Gradle project. Make sure that you have completed the steps described in Prerequisites prior to connecting to Oracle Autonomous Database.

Prerequisites

The following sections provide information about the tasks that you need to perform before connecting your Java applications to Oracle Autonomous Database using Oracle JDBC driver and Universal Connection Pool. Refer to the appropriate prerequisites depending on the authentication type selected during provisioning of your Autonomous Exadata VM Cluster (AVMC). By default, one-way TLS connections are enabled when you provision an AVMC. See Create an Autonomous Exadata VM Cluster for more information.

You can find the authentication type on the Details page of your AVMC. See View Details of an Autonomous Exadata VM Cluster for instructions.

Provision an Oracle Autonomous Database Instance

You need access to an Oracle Autonomous Database. Refer to Before You Begin with Autonomous Database on Dedicated Exadata Infrastructure if you have no provisioned one already.

Remember the password that you used for ADMIN user. For the demonstration purpose, we will be using ADMIN user, but our recommendation is to create other database users either using Oracle SQL Developer or Database Actions.

Install JDK 8

Download latest JDK 8 or a higher JDK versions.

Note

Ensure that you use JDK8u162 or a later version. Use java -version to check the JDK version that you have installed. To check the JDBC driver version, type java -jar ojdbc8.jar.

Download a Sample Program from Github

  1. Download the ADBQuickStart.java file from Github. This sample application uses the Sales History (SH) sample schema and displays 20 records from the SH.CUSTOMERS table.
  2. Modify the ADBQuickStart.java file to include your Oracle Autonomous Database connection information:
    • DB_USER: You can use ADMIN, the user created by default when the Oracle Autonomous Database is created (if you create another OracleAutonomous Database user, you can use that user instead).
    • DB_PASSWORD: Use the database user's password. If connecting as the ADMIN user, set this to the password you chose during the Create Autonomous Database step while provisioning Oracle Autonomous Database. For security reasons, you need to enter the password through the console when you run the sample.
    • DB_URL: Obtain the connection string for your Autonomous Database by following the instructions in View Connection Strings for an Autonomous Database. If you are directly using in the Java program, you need to escape " in the connection string with \"

    A sample quick start Java file looks like this:

    DB_URL = "jdbc:oracle:thin:@jdbc:oracle:thin:@(description=
          (retry_count=20)(retry_delay=3)(address=(protocol=tcps)(port=1521)(host=adb.us-sanjose-1.oraclecloud.com))(connect_data=(service_name=g13ec47eade81f7_demodb_low.adb.oraclecloud.com))(security=(ssl_server_cert_dn=\"CN=adb.us-sanjose-1.oraclecloud.com,
          OU=Oracle ADB SANJOSE, O=Oracle Corporation, L=Redwood City, ST=California,
          C=US\")))"
    DB_USER="ADMIN" and DB_PASSWORD="your_password"
  3. Save changes to the ADBQuickStart.java file.

Troubleshooting and Debugging: If you encounter any error, refer the Troubleshooting Tips page for some helpful hints.

Provision an Oracle Autonomous Database Instance

You need access to an Oracle Autonomous Database. Refer to Before You Begin with Autonomous Database on Dedicated Exadata Infrastructure if you have no provisioned one already.

Remember the password that you used for ADMIN user. For the demonstration purpose, we will be using ADMIN user, but our recommendation is to create other database users either using Oracle SQL Developer or Database Actions.

Obtain Client Credentials

  1. Download a wallet file from the Autonomous Database instance to obtain a zip file that contains the client security credentials and network configuration settings required to access an Autonomous Database instance.

    Obtain the client security credentials (wallet.zip file):

    • ADMIN user: On the Oracle Cloud Infrastructure Console, click Database connection. See Download Client Credentials.
    • Other user (non-administrator): Obtain the Oracle Wallet from the administrator for your Autonomous Database instance.
    Note

    Protect the wallet.zip file and its contents to prevent unauthorized database access.
  2. Unzip the client credentials file (wallet.zip).

Install JDK 8

Download latest JDK 8 or a higher JDK versions.

Note

Ensure that you use JDK8u162 or a later version. Use java -version to check the JDK version that you have installed. To check the JDBC driver version, type java -jar ojdbc8.jar.

Download a Sample Program from Github

  1. Download the ADBQuickStart.java file from Github. This sample application uses the Sales History (SH) sample schema and displays 20 records from the SH.CUSTOMERS table.
  2. Modify the ADBQuickStart.java file to include your Oracle Autonomous Database connection information:
    • DB_USER: You can use ADMIN, the user created by default when the Oracle Autonomous Database is created (if you create another OracleAutonomous Database user, you can use that user instead).
    • DB_PASSWORD: Use the database user's password. If connecting as the ADMIN user, set this to the password you chose during the Create Autonomous Database step while provisioning Oracle Autonomous Database. For security reasons, you need to enter the password through the console when you run the sample.
    • DB_URL: Enter the net service name (TNS Alias) DBName_medium where DBName is the Oracle Autonomous Database Name entered during the Create Autonomous Database step while provisioning Oracle Autonomous Database. The available net service names can be seen in the tnsnames.ora file, which is a part of the client credentials zip file.

      TNS_ADMIN should point to the location where you have unzipped the client credentials of Oracle Autonomous Database.

    A sample quick start Java file looks like this:

    DB_URL = "jdbc:oracle:thin:@DBName_medium?TNS_ADMIN=/Users/test/wallet_DBName" DB_USER="ADMIN" and DB_PASSWORD="enter_it_from_console"
  3. Save changes to the ADBQuickStart.java file.

Procedure

You can connect to Autonomous Database using IntelliJ and either Maven or Gradle.

  1. Create a Maven project.
    1. Click from File menu, select New, and then select Project.
    2. Select Maven on the left hand side and select the latest version of JDK as Project SDK.
    3. Click Next.
    4. Give Name as ADBQuickstart.
  2. Create the ADBQuickStart.java file.
    1. Right-click on src/main/java.
    2. Select New, and then select Java Class
    3. Enter com.oracle.jdbctest.ADBQuickStart.java. This will create the required package structure as well. Make sure to copy contents of the ADBQuickstart.java file to this new file.
  3. Modify the pom.xml file with the following changes: Add Oracle JDBC Driver as a dependency.

    ojdbc8-production will download Oracle JDBC driver (ojdbc8.jar) along with ucp.jar (required for using UCP as a client side connection pool), oraclepki.jar, osdt_core.jar, osdt_cert.jar. These JARs are required for using Oracle Wallets while connecting to Oracle Autonomous Database.

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <dependencies>
      <dependency>
        <groupId>com.oracle.database.jdbc</groupId>
        <artifactId>ojdbc8-production</artifactId>
        <version>19.18.0.0</version>
        <type>pom</type>
      </dependency>
    </dependencies>
  4. Build and run a ADB QuickStart. Compile the Java code by right-clicking ADBQuickStart.java, and then click Build Module ADBQuickStart. Make sure you do not have any compilation error in the Java code and you are using the latest JDK version.
  5. Run the sample Java program. Right-click ADBQuickStart.java, and then click Run ADBQuickStart.main(). Make sure to enter the database password on the console.

    Sample Output:

    The queried rows along with a success message are displayed, as shown in the following screen:


    Description of intellij_build_and_run.png follows

Note

Note: If you connect to Oracle Autonomous Database from behind a firewall, you will likely encounter a connection timeout error. Make sure to be outside the firewall while running this sample or update the tnsnames.ora file to use an HTTPS proxy.
  1. Follow the instructions from the Gradle Guide for Gradle download and build instructions, and set the PATH variable before using Gradle commands. As a first step, create a Gradle project using the following command. Make sure to choose 2:application for Select type of project to generate. Also, for Source package (default:temp): use com.oracle.jdbctest.
    gradle init
  2. Copy the ADBQuickStart.java file to the src/main/java/com/oracle/jdbctest directory.
  3. Modify the build.gradle file with the following changes:
    • Add mavenCentral() as a repository.
    • Add Oracle JDBC driver as a dependency.
      Note

      ojdbc8-production downloads Oracle JDBC driver (ojdbc8.jar) along with ucp.jar (required for using UCP as a client side connection pool), oraclepki.jar, osdt_core.jar, osdt_cert.jar. These JARs are required for using Oracle Wallets while connecting to Oracle Autonomous Database.

      For additional information, see Maven Central Guide.

    • Update the mainClassName to ADBQuickStart.
    • Add a run block to read the password from the console.
      repositories { 
        // Maven Central
         mavenCentral()
       } 
      dependencies { 
        // Get the 19.18.0.0 Oracle JDBC driver along with other companion jars
        implementation("com.oracle.database.jdbc:ojdbc8-production:19.18.0.0")
       }
      application { 
        // Define the main class for the application
        mainClassName ='{your_project_directory}.ADBQuickStart' 
      } 
      // To pause to read the password from console
      run {
        standardInput = System.in
      }
  4. Build a Gradle Application. Make sure you are in the directory where build.gradle file is present. Compile the Java code using the following command:
    ./gradlew build
  5. Run the sample Java program.
    ./gradlew run

    Sample Output: The queried rows along with a success message are displayed, as shown in the following screen:


    Description of gradle_build_and_run.png follows

Note

If you connect to Oracle Autonomous Database from behind a firewall, you will likely encounter a connection timeout error. Make sure to be outside the firewall while running this sample or update the tnsnames.ora file to use an HTTPS proxy.