Connect to Oracle Autonomous Database Using JDBC with Eclipse
You can establish a connection to Oracle Autonomous Database using the Eclipse 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 Eclipse and either Maven or Gradle.
- Create a Maven project.
- From the File menu, select New, and then select New Maven Project. You can either use maven archetype or select Create a simple project (skip archetype selection).
- Choose GAV for your Maven project. These will appear in the
pom.xml
file for the project.- Group Id:
com.oracle
- Artifact Id:
ADBquickstart
- Version: Leave it as
0.0.1-SNAPSHOT
- Group Id:
- Create the
ADBQuickStart.java
file.- Right-click on
src/main/java
, select New, and then select Class. - Enter the following values, and then click Finish.
- Package:
com.oracle.jdbctest
- Name:
ADBQuickStart
- Package:
- 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 sample Java program. Make sure you do not have any compilation error in the Java code and you are using the latest JDK version. Right-click ADBQuickStart.java. Click Run As, and then click Java Application to run the sample Java program. You are prompted to enter the database password.
Once you enter the password, the results are displayed.
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 thetnsnames.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.