Build a Java Application
To build a Java application that accesses an Autonomous Database, you start by configuring your development system to support database access that can take advantage of the continuous availability and high performance features of Autonomous Database.
After configuring your development system to support database access, you code database connections and SQL statements in your application to take advantage of the continuous availability and high performance features.
Tip:
For a "try it out" alternative to reading the following topics, you can go through the Lab 5: Build Java Application Stacks in Oracle Autonomous Database Dedicated for Developers and Database Users Workshop.Related Topics
Configure Your Java Development System
To configure your development system so that your Java application can take advantage of the continuous availability and high performance features of an Autonomous Database, perform these steps.
- Download and install the Java Development Kit (JDK).
- Download the client credentials for your Autonomous Database.
- Get the Oracle Java Database Connectivity (JDBC) drivers.
Before You Begin
Your development system must meet certain criteria to configure it successfully.
-
It must have internet access.
-
It must have network access to the Autonomous Database.
See Configure a Development System to Access the Database to learn how to ensure your development system meets this requirement.
Download and Install the JDK
Go to the Java SE Downloads page. Then, download and install JDK 8u221 or later by following the instructions on the page.
Download the Client Credentials for Your Autonomous Database
-
Download the zip file containing client credentials for your database to a secure directory on your computer.
This zip file is available for download from the database's Details page in the Oracle Cloud console. If you have an Oracle Cloud user account that permits you to access this page, download the credentials as follows. If you don't have such an account, you need to get the zip file from the administrator of the database, together with the password that was used to protect the zip file.
-
In your web browser, sign in to Oracle Cloud and navigate to the Details page for the Autonomous Database.
-
Click DB Connection.
-
On the Database Connection page click Download.
-
In the Download Wallet dialog, enter a password in the Password field and confirm the password in the Confirm Password field.
The password must be at least 8 characters long and must include at least 1 letter and either 1 numeric character or 1 special character.
-
Click Download and unzip, to save the client credentials zip file to a secure directory.
-
Get the Oracle JDBC Drivers
Get the Oracle JDBC drivers, version 19.6.0.0 or later, from either Maven Central or the JDBC Downloads page at Oracle Technical Resources. (See the Oracle Technologies JDBC Home page for related videos and other resources.)
To get the JDBC drivers from Maven Central, follow these steps.
-
Get the Oracle JDBC drivers from Central Maven Repository.
Provide the driver Maven dependency GAV (GroupID, ArtifactID, VersionID), to pull
ojdbc8.jar
, along with other jars such asoraclepki.jar
,osdt_core.jar
, andosdt_cert.jar
. See Maven Central Guide.For
ojdbc8.jar
version 19.6.0.0, provide this GAV:<groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc8</artifactId> <version>19.7.0.0</version>
For
ojdbc8.jar
version 19.7.0.0, provide this GAV:<groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc8-production</artifactId> <version>19.7.0.0</version> <type>POM</type>
To get the JDBC drivers from Oracle Technical Resources, follow these steps.
-
Go to the Oracle JDBC Downloads page. Then, choose the latest version of the drivers to go to its version-specific download page.
-
Download and unzip this archive to the directory where you want to place the JDBC driver:
ojdbc8-full.tar.gz
. -
Point the connection URL to your Autonomous Database.
Append
TNS_ADMIN
to the connection URL, setting its value to the full path of the directory where you unzipped the client credentials. For example:// Use TNS alias name plus TNS_ADMIN with JDBC driver 18.3 or higher DB_URL="jdbc:oracle:thin:@wallet_dbname? TNS_ADMIN=/Users/test/wallet_dbname"; // For Microsoft Windows, use this for TNS_ADMIN: // TNS_ADMIN=C:\\Users\\test\\wallet_dbname”;
-
Add the paths to the following unzipped JAR files to the
CLASSPATH
environment variable you use when you compile and run Java programs.Use DataSourceSample.java or UCPSample.java to verify the connection to your Autonomous Database.
ojdbc8.jar
: the core JDBC driveroraclepki.jar
,osdt_core.jar
, andosdt_cert.jar
: for an Autonomous Database that uses wallet-based authenticationucp.jar
: for Universal Connection Pooling (UCP)ons.jar
andsimplefan.jar
: for FAN (Fast Application Notification) support
Code Database Connections and SQL Statements
After configuring your development system to support Java application connectivity to an Autonomous Database, follow these guidelines to achieve high performance and continuous availability of your application's connections to the database:
- Use connection pools.
- Enable FAN (Fast Application Notification).
- Use the predefined database service that best matches the operations you will be performing. For information about the predefined database services, see Predefined Database Service Names for Autonomous Databases.
For example:
import java.sql.Connection;
import javax.sql.PooledConnection;
import oracle.jdbc.OracleConnection;
import oracle.jdbc.replay.OracleDataSourceFactory;
import oracle.jdbc.replay.OracleDataSource;
import oracle.jdbc.replay.OracleConnectionPoolDataSource;
...
PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource();
// Set the connection factory first before all other properties
pds.setConnectionFactoryClassName(
"oracle.jdbc.replay.OracleConnectionPoolDataSourceImpl");
pds.setFastConnectionFailoverEnabled(true);
pds.setURL("jdbc:oracle:thin:@tp_tls?TNS_ADMIN=/users/jdoe/adbcredentials");
pds.setUser("appuser");
pds.setPassword("<password>");
pds.setConnectionPoolName("JDBC_UCP_POOL");
Connection conn = pds.getConnection();
Additional Resources
- Introducing JDBC in Oracle Database 19c JDBC Developer's Guide or Oracle Database 23ai JDBC Developer's Guide
- Oracle Database JDBC Java API Reference in Oracle Database 19c JDBC Developer's Guide or Oracle Database 23ai JDBC Developer's Guide
- Introduction to UCP in Oracle Database 19c Universal Connection Pool Developer's Guide or Oracle Database 23ai Universal Connection Pool Developer's Guide
- Oracle Universal Connection Pool API Reference in Oracle Database 19c Universal Connection Pool Java API Reference or Oracle Database 23ai Universal Connection Pool Java API Reference
Copyright © 2021, Oracle and/or its affiliates.