Connecting a Spring Boot Java Application to Oracle Autonomous Database (ADB)

vijay balebail
3 min readMar 23, 2024

--

Oracle’s Autonomous Database offers a compelling mix of automation, performance, scalability and security, making it an attractive choice for many organizations. In this blog post, we’ll explore how to connect a Spring Boot Java application to an Oracle Autonomous Database with wallets, focusing on the necessary dependencies and the JDBC URL syntax for a secure connection.

Prerequisites

Before diving into the technicalities, ensure that you have:

  • An Oracle Cloud account with access to the Autonomous Database and have download the wallet zip file.
  • oracle jdbc drivers and companion jars (oraclepki.jar, osdt_cert.jar, and osdt_core.jar)
  • A Spring Boot application set up in your development environment.

Step 1: Including the Dependencies

To connect your Spring Boot application to an Oracle Autonomous Database, you need the Oracle JDBC (Java Database Connectivity) driver and additional libraries for secure connections. These dependencies are crucial for enabling your application to communicate with the database over a secure protocol.

You need to include the following JAR files in your project’s dependency file (e.g., pom.xml for Maven or build.gradle for Gradle):

  • ojdbc8.jar: The Oracle JDBC driver compatible with JDK8.
  • oraclepki.jar, osdt_cert.jar, and osdt_core.jar: Libraries required for secure database connections.

Here’s how you can include these dependencies in your pom.xml file for a Maven project:

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc-bom</artifactId>
<version>19.7.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ucp</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.database.security</groupId>
<artifactId>oraclepki</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.database.security</groupId>
<artifactId>osdt_core</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.database.security</groupId>
<artifactId>osdt_cert</artifactId>
</dependency>
</dependencies>

These dependencies can be found in the Oracle Maven repository or as part of your Oracle Database jdbc download page.

Step 2: Configuring the JDBC URL

The JDBC URL is the string that your application uses to connect to the Oracle Autonomous Database. It includes details such as the protocol, host, port, service name, and the location of your security wallet (a directory containing security files for TLS connections).

The syntax for the JDBC URL is as follows:

jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=tcps)
(HOST=hostname)(PORT=1522))
(CONNECT_DATA=(SERVICE_NAME=myservicename))
(Security=(MY_WALLET_DIRECTORY=/opt/mywallet/)))

Replace hostname, myservicename, and /opt/mywallet with your database’s host name, your service name, and the path to your wallet directory, respectively. Alternatively, you can add parameter TNS_ADMIN to url and unzip the wallet contents in that directory and update sqlnet.ora file to point to the wallet location.

This JDBC URL can be configured in your application.properties or application.yml file in your Spring Boot application.

application.properties Example:

spring.datasource.url=jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=tcps)(HOST=yourHostname)(PORT=1522))(CONNECT_DATA=(SERVICE_NAME=yourServiceName))(Security=(MY_WALLET_DIRECTORY=/path/to/your/wallet/)))
spring.datasource.username=yourUsername spring.datasource.password=yourPassword
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver

application.yml Example:

spring:
datasource:
url: jdbc:oracle:thin:@your_db_connection_string?TNS_ADMIN=path/to/your/wallet_directory
username: your_username
password: your_password
driver-class-name: oracle.jdbc.OracleDriver

Step 3: Running Your Application

With the dependencies added and the JDBC URL configured, you’re now ready to run your Spring Boot application. If everything is set up correctly, your application should be able to connect to the Oracle Autonomous Database securely and perform database operations.

Conclusion

Connecting a Spring Boot Java application to an Oracle Autonomous Database involves including the necessary Oracle JDBC and security libraries, configuring the JDBC URL with the correct parameters, and ensuring your application has access to the wallet directory for secure connections. Following these steps will enable you to leverage the powerful features of Oracle’s Autonomous Database in your enterprise applications.

Reference links

jdbc URL and springboot config.

--

--