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.
Parent topic: Connect to 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.
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
- 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. - 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"
- DB_USER: You can use
- 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
-
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 thewallet.zip
file and its contents to prevent unauthorized database access. - Unzip the client credentials file (
wallet.zip
).
Install JDK 8
Download latest JDK 8 or a higher JDK versions.
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
- 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. - 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"
- DB_USER: You can use
- Save changes to the
ADBQuickStart.java
file.
Procedure
You can connect to Autonomous Database using IntelliJ and either Maven or Gradle.
- Create a Maven project.
- Click from File menu, select New, and then select Project.
- Select Maven on the left hand side and select the latest version of JDK as Project SDK.
- Click Next.
- Give Name as ADBQuickstart.
- Create the
ADBQuickStart.java
file.- Right-click on
src/main/java
. - Select New, and then select Java Class
- 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.
- Right-click on
- 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 withucp.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>
- 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.
- 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:
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.
- 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
- Copy the ADBQuickStart.java file to the
src/main/java/com/oracle/jdbctest
directory. - 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 withucp.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 }
- Add
- 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
- 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:
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.