Skip to content
94 changes: 94 additions & 0 deletions java/jdbc/ConnectionSamples/JDBCDBTokenSample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@

/* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.*/
/*
DESCRIPTION
The code sample shows how to use the JDBC driver to establish a connection
to the Autonomous Database (ADB) using access token
issued by the OCI Identity service.

Step 1: Enter the DB_URL to pointing to your Autonomous Database (ADB)
Step 2: Make sure to have either 21.4.0.0.1 or 19.13.0.0.1 JDBC driver
in the classpath.
Step 3: Compile and run the sample JDBCDBTokenSample

NOTES
Use JDK8 and above
MODIFIED (MM/DD/YY)
nbsundar 1/7/21 - Creation
*/

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import oracle.jdbc.pool.OracleDataSource;
import oracle.jdbc.OracleConnection;
import java.sql.DatabaseMetaData;

public class JDBCDBTokenSample {

//If mutual TLS (mTLS) is enabled then, ADB connection requires wallets.
// Download the wallet zip file and provide the path to the zip file as TNS_ADMIN
// Note that you need to pass the property oracle.jdbc.tokenAuthentication=OCI_TOKEN for token authentication
final static String DB_URL="jdbc:oracle:thin:@dbname_high?TNS_ADMIN=/Users/user/wallet/Wallet_dbname&oracle.jdbc.tokenAuthentication=OCI_TOKEN";
// If mutual TLS(mTLS) is disabled then, ADB connection does not require wallets.
// Copy the connection string from "DB Connection" tab from "Connection Strings" section choosing "TLS" in the dropdown
//final static String DB_URL="jdbc:oracle:thin:@(description= (retry_count=20)(retry_delay=3)(address=(protocol=tcps)(port=1521)(host=adb.us-phoenix-1.oraclecloud.com))(connect_data=(service_name=gebqqeredfsozhjbqbs_dbname_medium.adb.oraclecloud.com)))?oracle.jdbc.tokenAuthentication=OCI_TOKEN";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this example URL, it is using a TNS descriptor format. The descriptor format supports a "TOKEN_AUTH" parameter that has the same effect as the "oracle.jdbc.tokenAuthentication" property.
It would be nice to show this TOKEN_AUTH parameter in the example, rather than the oracle.jdbc.tokenAuthentication property.

"jdbc:oracle:thin:@(description=" + "(retry_count=20)(retry_delay=3)" + "(address=(protocol=tcps)(port=1521)(host=adb.us-phoenix-1.oraclecloud.com))" + "(security=(token_auth=OCI_TOKEN))" + "(connect_data=(service_name=gebqqeredfsozhjbqbs_dbname_medium.adb.oraclecloud.com)))"

Note that I've added in line breaks as well, these will improve the readability of the long form descriptor string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good to know, but, I prefer not to use this as customers need to add in the middle of the connection string. It is easy to use EZConnect and pass this additional parameter. We can add it as a comment to show the possibility though.

// Another way to enable token authentication in the long form connection string.
final static String DB_URL="jdbc:oracle:thin:@(description="
+ "(retry_count=20)(retry_delay=3)(address=(protocol=tcps)(port=1521)(host=adb.us-phoenix-1.oraclecloud.com))"
+ "(security=(token_auth=OCI_TOKEN))"
+ "(connect_data=(service_name=gebqqeredfsozhjbqbs_dbname_medium.adb.oraclecloud.com)))";


public static void main(String args[]) throws SQLException {

// For more connection related properties. Refer to
// the OracleConnection interface.
Properties properties = new Properties();

//Connection property to enable IAM token authentication.
// properties.put(OracleConnection.CONNECTION_PROPERTY_TOKEN_AUTHENTICATION, "OCI_TOKEN");

OracleDataSource ods = new OracleDataSource();
ods.setURL(DB_URL);
ods.setConnectionProperties(properties);

// With AutoCloseable, the connection is closed automatically.
try (OracleConnection connection = (OracleConnection) ods.getConnection()) {
// Get the JDBC driver name and version
DatabaseMetaData dbmd = connection.getMetaData();
System.out.println("Driver Name: " + dbmd.getDriverName());
System.out.println("Driver Version: " + dbmd.getDriverVersion());
// Print some connection properties
System.out.println("Default Row Prefetch Value is: " +
connection.getDefaultRowPrefetch());
System.out.println("Database Username is: " + connection.getUserName());
System.out.println();
// Perform a database operation
printTableNames(connection);
}
}
/*
* Displays 15 table_name from all_tables.
*/
public static void printTableNames(Connection connection) throws SQLException {
// Statement and ResultSet are AutoCloseable and closed automatically.
try (Statement statement = connection.createStatement()) {
try (ResultSet resultSet = statement
.executeQuery("select table_name from all_tables where rownum < 15")) {
System.out.println("Table name");
System.out.println("---------------------");
while (resultSet.next())
System.out.println(resultSet.getString(1));
}
}
}
}



Loading