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