Connect to Oracle Database Using JDBC Without a Build Tool

You can establish a connection to Oracle Autonomous Database manually without any build tool.

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

  1. Download the19c version of ojdbc8-full.tar.gz from OTN and unzip the contents to your classpath.
    Note

    ojdbc8-full.tar.gz contains the latest JDBC driver ojdbc8.jar, ucp.jar (required JAR 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.
  2. Compile the Java program. Make sure to provide the correct path for the required JARs in the classpath:
    javac -classpath ./lib/ojdbc8.jar:./lib/ucp.jar:/lib/oraclepki.jar:./lib/osdt_core.jar:./lib/osdt_cert.jar com/oracle/jdbctest/ADBQuickStart.java
  3. Run the sample Java program. Make sure to provide the correct path for the required JARs in the classpath:
    java -classpath ./lib/ojdbc8.jar:./lib/ucp.jar:/lib/oraclepki.jar:./lib/osdt_core.jar:./lib/osdt_cert.jar:. com.oracle.jdbctest.ADBQuickStart

    Sample Output:

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


    Description of no_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.