|
| 1 | + |
| 2 | +/* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.*/ |
| 3 | +/* |
| 4 | + DESCRIPTION |
| 5 | + The code sample shows how to use the JDBC driver to establish a connection |
| 6 | + to the Autonomous Database (ADB) using access token |
| 7 | + issued by the OCI Identity service. |
| 8 | + |
| 9 | + Step 1: Enter the DB_URL to pointing to your Autonomous Database (ADB) |
| 10 | + Step 2: Make sure to have either 21.4.0.0.1 or 19.13.0.0.1 JDBC driver |
| 11 | + in the classpath. |
| 12 | + Step 3: Compile and run the sample JDBCDBTokenSample |
| 13 | + |
| 14 | + NOTES |
| 15 | + Use JDK8 and above |
| 16 | + MODIFIED (MM/DD/YY) |
| 17 | + nbsundar 1/7/21 - Creation |
| 18 | + */ |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.io.InputStream; |
| 22 | +import java.sql.Connection; |
| 23 | +import java.sql.ResultSet; |
| 24 | +import java.sql.SQLException; |
| 25 | +import java.sql.Statement; |
| 26 | +import java.util.Properties; |
| 27 | + |
| 28 | +import oracle.jdbc.pool.OracleDataSource; |
| 29 | +import oracle.jdbc.OracleConnection; |
| 30 | +import java.sql.DatabaseMetaData; |
| 31 | + |
| 32 | +public class JDBCDBTokenSample { |
| 33 | + |
| 34 | + //If mutual TLS (mTLS) is enabled then, ADB connection requires wallets. |
| 35 | + // Download the wallet zip file and provide the path to the zip file as TNS_ADMIN |
| 36 | + // Note that you need to pass the property oracle.jdbc.tokenAuthentication=OCI_TOKEN for token authentication |
| 37 | + final static String DB_URL="jdbc:oracle:thin:@dbname_high?TNS_ADMIN=/Users/user/wallet/Wallet_dbname&oracle.jdbc.tokenAuthentication=OCI_TOKEN"; |
| 38 | + // If mutual TLS(mTLS) is disabled then, ADB connection does not require wallets. |
| 39 | + // Copy the connection string from "DB Connection" tab from "Connection Strings" section choosing "TLS" in the dropdown |
| 40 | + //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"; |
| 41 | + // Another way to enable token authentication in the long form connection string. |
| 42 | + final static String DB_URL="jdbc:oracle:thin:@(description=" |
| 43 | + + "(retry_count=20)(retry_delay=3)(address=(protocol=tcps)(port=1521)(host=adb.us-phoenix-1.oraclecloud.com))" |
| 44 | + + "(security=(token_auth=OCI_TOKEN))" |
| 45 | + + "(connect_data=(service_name=gebqqeredfsozhjbqbs_dbname_medium.adb.oraclecloud.com)))"; |
| 46 | + |
| 47 | + |
| 48 | + public static void main(String args[]) throws SQLException { |
| 49 | + |
| 50 | + // For more connection related properties. Refer to |
| 51 | + // the OracleConnection interface. |
| 52 | + Properties properties = new Properties(); |
| 53 | + |
| 54 | + //Connection property to enable IAM token authentication. |
| 55 | + // properties.put(OracleConnection.CONNECTION_PROPERTY_TOKEN_AUTHENTICATION, "OCI_TOKEN"); |
| 56 | + |
| 57 | + OracleDataSource ods = new OracleDataSource(); |
| 58 | + ods.setURL(DB_URL); |
| 59 | + ods.setConnectionProperties(properties); |
| 60 | + |
| 61 | + // With AutoCloseable, the connection is closed automatically. |
| 62 | + try (OracleConnection connection = (OracleConnection) ods.getConnection()) { |
| 63 | + // Get the JDBC driver name and version |
| 64 | + DatabaseMetaData dbmd = connection.getMetaData(); |
| 65 | + System.out.println("Driver Name: " + dbmd.getDriverName()); |
| 66 | + System.out.println("Driver Version: " + dbmd.getDriverVersion()); |
| 67 | + // Print some connection properties |
| 68 | + System.out.println("Default Row Prefetch Value is: " + |
| 69 | + connection.getDefaultRowPrefetch()); |
| 70 | + System.out.println("Database Username is: " + connection.getUserName()); |
| 71 | + System.out.println(); |
| 72 | + // Perform a database operation |
| 73 | + printTableNames(connection); |
| 74 | + } |
| 75 | + } |
| 76 | + /* |
| 77 | + * Displays 15 table_name from all_tables. |
| 78 | + */ |
| 79 | + public static void printTableNames(Connection connection) throws SQLException { |
| 80 | + // Statement and ResultSet are AutoCloseable and closed automatically. |
| 81 | + try (Statement statement = connection.createStatement()) { |
| 82 | + try (ResultSet resultSet = statement |
| 83 | + .executeQuery("select table_name from all_tables where rownum < 15")) { |
| 84 | + System.out.println("Table name"); |
| 85 | + System.out.println("---------------------"); |
| 86 | + while (resultSet.next()) |
| 87 | + System.out.println(resultSet.getString(1)); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | + |
| 94 | + |
0 commit comments