|
| 1 | +/* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. |
| 2 | +Licensed under the Universal Permissive License v 1.0 |
| 3 | +as shown at http://oss.oracle.com/licenses/upl */ |
| 4 | + |
| 5 | +/* |
| 6 | + DESCRIPTION |
| 7 | + The code sample demonstrates establishing a connection to Autonomous Database (ATP/ADW) using |
| 8 | + Oracle JDBC driver and Universal Connection Pool (UCP). It does the following. |
| 9 | + |
| 10 | + (a) Set the connection factory class name to |
| 11 | + oracle.jdbc.pool.OracleDataSource before getting a connection. |
| 12 | + (b) Set the connection pool properties(e.g.,minPoolSize, maxPoolSize). |
| 13 | + (c) Get the connection and perform some database operations. |
| 14 | + For a quick test, the sample retrieves 20 records from the Sales History (SH) schema |
| 15 | + that is accessible to any DB users on autonomous Database. |
| 16 | + |
| 17 | + Step 1: Enter the Database details DB_URL and DB_USER. |
| 18 | + You will need to enter the DB_PASSWORD of your Autonomous Database through console |
| 19 | + while running the sample. |
| 20 | + Step 2: Download the latest Oracle JDBC driver(ojdbc8.jar) and UCP (ucp.jar) |
| 21 | + along with oraclepki.jar, osdt_core.jar and osdt_cert.jar and add to your classpath. |
| 22 | + Refer to https://www.oracle.com/database/technologies/maven-central-guide.html |
| 23 | + Step 3: Compile and Run the sample. |
| 24 | + |
| 25 | + SH Schema: |
| 26 | + This sample uses the Sales History (SH) sample schema. SH is a data set suited for |
| 27 | + online transaction processing operations. The Star Schema Benchmark (SSB) sample schema |
| 28 | + is available for data warehousing operations. Both schemas are available |
| 29 | + with your shared ADB instance and do not count towards your storage. |
| 30 | + ou can use any ADB user account to access these schemas. |
| 31 | + |
| 32 | + NOTES |
| 33 | + Use JDK 1.8 and above |
| 34 | + |
| 35 | + MODIFIED (MM/DD/YY) |
| 36 | + nbsundar 11/09/2020 - Creation |
| 37 | + */ |
| 38 | +package com.oracle.jdbctest; |
| 39 | + |
| 40 | +import java.sql.Connection; |
| 41 | +import java.sql.ResultSet; |
| 42 | +import java.sql.SQLException; |
| 43 | +import java.sql.Statement; |
| 44 | +import java.util.Scanner; |
| 45 | + |
| 46 | +import oracle.ucp.jdbc.PoolDataSourceFactory; |
| 47 | +import oracle.ucp.jdbc.PoolDataSource; |
| 48 | + |
| 49 | +/* |
| 50 | + * The sample demonstrates connecting to Autonomous Database using |
| 51 | + * Oracle JDBC driver and UCP as a client side connection pool. |
| 52 | + */ |
| 53 | +public class ADBQuickStart { |
| 54 | + |
| 55 | + public static void main(String args[]) throws Exception { |
| 56 | + // Make sure to have Oracle JDBC driver 18c or above |
| 57 | + // to pass TNS_ADMIN as part of a connection URL. |
| 58 | + // TNS_ADMIN - Should be the path where the client credentials zip (wallet_dbname.zip) file is downloaded. |
| 59 | + // dbname_medium - It is the TNS alias present in tnsnames.ora. |
| 60 | + final String DB_URL="jdbc:oracle:thin:@dbname_medium?TNS_ADMIN=/Users/test/wallet_dbname/"; |
| 61 | + // Update the Database Username and Password to point to your Autonomous Database |
| 62 | + final String DB_USER = "admin"; |
| 63 | + String DB_PASSWORD = null ; |
| 64 | + final String CONN_FACTORY_CLASS_NAME="oracle.jdbc.pool.OracleDataSource"; |
| 65 | + |
| 66 | + // For security purposes, you must enter the password through the console |
| 67 | + try { |
| 68 | + Scanner scanner = new Scanner(System.in); |
| 69 | + System.out.print("Enter the password for Autonomous Database: "); |
| 70 | + DB_PASSWORD = scanner.nextLine(); |
| 71 | + } |
| 72 | + catch (Exception e) { |
| 73 | + System.out.println("ADBQuickStart - Exception occurred : " + e.getMessage()); |
| 74 | + System.exit(1); |
| 75 | + } |
| 76 | + // Get the PoolDataSource for UCP |
| 77 | + PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource(); |
| 78 | + |
| 79 | + // Set the connection factory first before all other properties |
| 80 | + pds.setConnectionFactoryClassName(CONN_FACTORY_CLASS_NAME); |
| 81 | + pds.setURL(DB_URL); |
| 82 | + pds.setUser(DB_USER); |
| 83 | + pds.setPassword(DB_PASSWORD); |
| 84 | + pds.setConnectionPoolName("JDBC_UCP_POOL"); |
| 85 | + |
| 86 | + // Default is 0. Set the initial number of connections to be created |
| 87 | + // when UCP is started. |
| 88 | + pds.setInitialPoolSize(5); |
| 89 | + |
| 90 | + // Default is 0. Set the minimum number of connections |
| 91 | + // that is maintained by UCP at runtime. |
| 92 | + pds.setMinPoolSize(5); |
| 93 | + |
| 94 | + // Default is Integer.MAX_VALUE (2147483647). Set the maximum number of |
| 95 | + // connections allowed on the connection pool. |
| 96 | + pds.setMaxPoolSize(20); |
| 97 | + |
| 98 | + |
| 99 | + // Get the database connection from UCP. |
| 100 | + try (Connection conn = pds.getConnection()) { |
| 101 | + System.out.println("Available connections after checkout: " |
| 102 | + + pds.getAvailableConnectionsCount()); |
| 103 | + System.out.println("Borrowed connections after checkout: " |
| 104 | + + pds.getBorrowedConnectionsCount()); |
| 105 | + // Perform a database operation |
| 106 | + doSQLWork(conn); |
| 107 | + } catch (SQLException e) { |
| 108 | + System.out.println("ADBQuickStart - " |
| 109 | + + "doSQLWork()- SQLException occurred : " + e.getMessage()); |
| 110 | + } |
| 111 | + |
| 112 | + System.out.println("Available connections after checkin: " |
| 113 | + + pds.getAvailableConnectionsCount()); |
| 114 | + System.out.println("Borrowed connections after checkin: " |
| 115 | + + pds.getBorrowedConnectionsCount()); |
| 116 | + } |
| 117 | + /* |
| 118 | + * Selects 20 rows from the SH (Sales History) Schema that is the accessible to all |
| 119 | + * the database users of autonomous database. |
| 120 | + */ |
| 121 | + private static void doSQLWork(Connection conn) throws SQLException { |
| 122 | + String queryStatement = "SELECT CUST_ID, CUST_FIRST_NAME, CUST_LAST_NAME, CUST_CITY," |
| 123 | + + "CUST_CREDIT_LIMIT FROM SH.CUSTOMERS WHERE ROWNUM < 20 order by CUST_ID"; |
| 124 | + |
| 125 | + System.out.println("\n Query is " + queryStatement); |
| 126 | + |
| 127 | + conn.setAutoCommit(false); |
| 128 | + // Prepare a statement to execute the SQL Queries. |
| 129 | + try (Statement statement = conn.createStatement(); |
| 130 | + // Select 20 rows from the CUSTOMERS table from SH schema. |
| 131 | + ResultSet resultSet = statement.executeQuery(queryStatement)) { |
| 132 | + System.out.println(String.join(" ", "\nCUST_ID", "CUST_FIRST_NAME", |
| 133 | + "CUST_LAST_NAME", "CUST_CITY", "CUST_CREDIT_LIMIT")); |
| 134 | + System.out.println("-----------------------------------------------------------"); |
| 135 | + while (resultSet.next()) { |
| 136 | + System.out.println(resultSet.getString(1) + " " + resultSet.getString(2) + " " + |
| 137 | + resultSet.getString(3)+ " " + resultSet.getString(4) + " " + |
| 138 | + resultSet.getInt(5)); |
| 139 | + } |
| 140 | + System.out.println("\nSuccessfully established a connection to Autonomous Database\n"); |
| 141 | + } |
| 142 | + } // End of doSQLWork |
| 143 | + |
| 144 | +} // End of ADBQuickStart |
0 commit comments