|
| 1 | +/* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.*/ |
| 2 | +/* |
| 3 | + DESCRIPTION |
| 4 | + The code sample shows how to use the JDBC driver and UCP to establish a |
| 5 | + connection to the Autonomous Database (ADB) using database token |
| 6 | + issued by the OCI Identity service. |
| 7 | + |
| 8 | + Step 1: Enter the DB_URL to pointing to your Autonomous Database (ADB) |
| 9 | + Step 2: Make sure to have either 21.4.0.0.1 or 19.13.0.0.1 JDBC driver |
| 10 | + and UCP (ucp.jar) in the classpath. Both must be from the same version. |
| 11 | + Step 2: Compile and Run the sample UCPDBTokenSample |
| 12 | + |
| 13 | + NOTES |
| 14 | + Use JDK8 and above |
| 15 | +
|
| 16 | + MODIFIED (MM/DD/YY) |
| 17 | + nbsundar 1/7/21 - Creation |
| 18 | + */ |
| 19 | + |
| 20 | +import java.sql.Connection; |
| 21 | +import java.sql.ResultSet; |
| 22 | +import java.sql.SQLException; |
| 23 | +import java.sql.Statement; |
| 24 | +import java.util.Properties; |
| 25 | + |
| 26 | +import java.sql.DatabaseMetaData; |
| 27 | +import oracle.ucp.jdbc.PoolDataSourceFactory; |
| 28 | +import oracle.ucp.jdbc.PoolDataSource; |
| 29 | +import oracle.jdbc.OracleConnection; |
| 30 | +import java.sql.DatabaseMetaData; |
| 31 | + |
| 32 | +public class UCPDBTokenSample { |
| 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:@demodb_high?TNS_ADMIN=/Users/nbsundar/ATPTesting/Wallet_DemoDB&oracle.jdbc.tokenAuthentication=OCI_TOKEN"; |
| 38 | + |
| 39 | + // If mutla TLS(mTLS) is disabled then, ADB connection does not require wallets. |
| 40 | + // Copy the connection string from "DB Connection" tab from "Connection Strings" section choosing "TLS" in the dropdown |
| 41 | + //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=gebqqvpozhjbqbs_testdb_medium.adb.oraclecloud.com)))?oracle.jdbc.tokenAuthentication=OCI_TOKEN"; |
| 42 | + |
| 43 | + final static String CONN_FACTORY_CLASS_NAME="oracle.jdbc.pool.OracleDataSource"; |
| 44 | + |
| 45 | + /* |
| 46 | + * The sample demonstrates UCP as client side connection pool. |
| 47 | + */ |
| 48 | + public static void main(String args[]) throws Exception { |
| 49 | + // Get the PoolDataSource for UCP |
| 50 | + PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource(); |
| 51 | + |
| 52 | + // Set the connection factory first before all other properties |
| 53 | + pds.setConnectionFactoryClassName(CONN_FACTORY_CLASS_NAME); |
| 54 | + pds.setURL(DB_URL); |
| 55 | + pds.setConnectionPoolName("JDBC_UCP_POOL"); |
| 56 | + |
| 57 | + // Default is 0. Set the initial number of connections to be created |
| 58 | + // when UCP is started. |
| 59 | + pds.setInitialPoolSize(5); |
| 60 | + |
| 61 | + // Default is 0. Set the minimum number of connections |
| 62 | + // that is maintained by UCP at runtime. |
| 63 | + pds.setMinPoolSize(5); |
| 64 | + |
| 65 | + // Default is Integer.MAX_VALUE (2147483647). Set the maximum number of |
| 66 | + // connections allowed on the connection pool. |
| 67 | + pds.setMaxPoolSize(20); |
| 68 | + |
| 69 | + Properties properties = new Properties(); |
| 70 | + |
| 71 | + properties.put(OracleConnection.CONNECTION_PROPERTY_DEFAULT_ROW_PREFETCH, "20"); |
| 72 | + properties.put(OracleConnection.CONNECTION_PROPERTY_THIN_NET_CHECKSUM_TYPES, |
| 73 | + "(MD5,SHA1,SHA256,SHA384,SHA512)"); |
| 74 | + properties.put(OracleConnection.CONNECTION_PROPERTY_THIN_NET_CHECKSUM_LEVEL, |
| 75 | + "REQUIRED"); |
| 76 | + // Connection property to enable IAM-Authentication |
| 77 | + properties.put(OracleConnection.CONNECTION_PROPERTY_TOKEN_AUTHENTICATION, "OCI_TOKEN"); |
| 78 | + |
| 79 | + pds.setConnectionProperties(properties); |
| 80 | + |
| 81 | + |
| 82 | + // Get the database connection from UCP. |
| 83 | + try (OracleConnection connection = (OracleConnection) pds.getConnection()) { |
| 84 | + // Perform a database operation |
| 85 | + // Get the JDBC driver name and version |
| 86 | + DatabaseMetaData dbmd = connection.getMetaData(); |
| 87 | + System.out.println("Driver Name: " + dbmd.getDriverName()); |
| 88 | + System.out.println("Driver Version: " + dbmd.getDriverVersion()); |
| 89 | + // Print some connection properties |
| 90 | + System.out.println("Default Row Prefetch Value is: " + |
| 91 | + connection.getDefaultRowPrefetch()); |
| 92 | + System.out.println("Database Username is: " + connection.getUserName()); |
| 93 | + System.out.println(); |
| 94 | + // Perform a database operation |
| 95 | + printTableNames(connection); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /* |
| 100 | + * Displays 15 table_name from all_tables. |
| 101 | + */ |
| 102 | + public static void printTableNames(Connection connection) throws SQLException { |
| 103 | + // Statement and ResultSet are AutoCloseable and closed automatically. |
| 104 | + try (Statement statement = connection.createStatement()) { |
| 105 | + try (ResultSet resultSet = statement |
| 106 | + .executeQuery("select table_name from all_tables where rownum < 15")) { |
| 107 | + System.out.println("Table name"); |
| 108 | + System.out.println("---------------------"); |
| 109 | + while (resultSet.next()) |
| 110 | + System.out.println(resultSet.getString(1)); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | + |
0 commit comments