@@ -46,34 +46,32 @@ online transaction processing operations. The Star Schema Benchmark (SSB) sample
46
46
import oracle .ucp .jdbc .PoolDataSourceFactory ;
47
47
import oracle .ucp .jdbc .PoolDataSource ;
48
48
49
- public class ADBQuickStart {
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 {
50
54
51
- // Make sure to have Oracle JDBC release 18c or higher
52
- // to pass TNS_ADMIN as part of a connection URL.
53
- // TNS_ADMIN - Should be the path where the client credentials zip file is downloaded.
54
- // wallet_dbname - This is the TNS Alias that you can get from tnsnames.ora.
55
- final static String DB_URL ="jdbc:oracle:thin:@wallet_dbname?TNS_ADMIN=/Users/test/wallet_dbname/" ;
56
- // Update the Database Username to point to your Autonomous Database. For a quick testing, you can
57
- // use "admin" user.
58
- final static String DB_USER = "admin" ;
59
- // For security reasons, you will need to provide database password through console.
60
- static String DB_PASSWORD ;
61
- final static String CONN_FACTORY_CLASS_NAME ="oracle.jdbc.pool.OracleDataSource" ;
62
-
63
- /*
64
- * The sample demonstrates UCP as client side connection pool.
65
- */
66
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 file is downloaded.
59
+ final String DB_URL ="jdbc:oracle:thin:@dbname_medium?TNS_ADMIN=/Users/test/wallet_dbname/" ;
60
+ // Update the Database Username and Password to point to your Autonomous Database
61
+ final String DB_USER = "admin" ;
62
+ String DB_PASSWORD = null ;
63
+ final String CONN_FACTORY_CLASS_NAME ="oracle.jdbc.pool.OracleDataSource" ;
64
+
67
65
// For security purposes, you must enter the password through the console
68
66
try {
69
67
Scanner scanner = new Scanner (System .in );
70
68
System .out .print ("Enter the password for Autonomous Database: " );
71
69
DB_PASSWORD = scanner .nextLine ();
72
70
}
73
- catch (Exception e ) {
71
+ catch (Exception e ) {
74
72
System .out .println ("ADBQuickStart - Exception occurred : " + e .getMessage ());
75
- }
76
-
73
+ System . exit ( 1 );
74
+ }
77
75
// Get the PoolDataSource for UCP
78
76
PoolDataSource pds = PoolDataSourceFactory .getPoolDataSource ();
79
77
@@ -96,36 +94,43 @@ public static void main(String args[]) throws Exception {
96
94
// connections allowed on the connection pool.
97
95
pds .setMaxPoolSize (20 );
98
96
97
+
99
98
// Get the database connection from UCP.
100
99
try (Connection conn = pds .getConnection ()) {
101
100
System .out .println ("Available connections after checkout: "
102
101
+ pds .getAvailableConnectionsCount ());
103
102
System .out .println ("Borrowed connections after checkout: "
104
103
+ pds .getBorrowedConnectionsCount ());
104
+
105
105
// Perform a database operation
106
- doSQLWork (conn );
106
+ try {
107
+ doSQLWork (conn );
108
+ } catch (SQLException e ) {
109
+ System .out .println ("ADBQuickStart - "
110
+ + "doSQLWork()- SQLException occurred : " + e .getMessage ());
111
+ }
107
112
}
108
113
109
114
System .out .println ("Available connections after checkin: "
110
115
+ pds .getAvailableConnectionsCount ());
111
116
System .out .println ("Borrowed connections after checkin: "
112
117
+ pds .getBorrowedConnectionsCount ());
113
- }
114
-
115
- /*
116
- * Selects 20 rows from the SH (Sales History) Schema that is the accessible to all
117
- * the database users of autonomous database.
118
- */
119
- public static void doSQLWork (Connection conn ) {
120
-
121
- try {
122
- conn .setAutoCommit (false );
123
- // Prepare a statement to execute the SQL Queries.
124
- try (Statement statement = conn .createStatement () ) {
118
+ }
119
+ /*
120
+ * Selects 20 rows from the SH (Sales History) Schema that is the accessible to all
121
+ * the database users of autonomous database.
122
+ */
123
+ private static void doSQLWork (Connection conn ) throws SQLException {
124
+ String queryStatement = "SELECT CUST_ID, CUST_FIRST_NAME, CUST_LAST_NAME, CUST_CITY,"
125
+ + "CUST_CREDIT_LIMIT FROM SH.CUSTOMERS WHERE ROWNUM < 20 order by CUST_ID" ;
126
+
127
+ System .out .println ("Query is " + queryStatement );
128
+
129
+ conn .setAutoCommit (false );
130
+ // Prepare a statement to execute the SQL Queries.
131
+ try (Statement statement = conn .createStatement ();
125
132
// Select 20 rows from the CUSTOMERS table from SH schema.
126
- try (ResultSet resultSet = statement .executeQuery ("select CUST_ID,"
127
- + "CUST_FIRST_NAME, CUST_LAST_NAME, CUST_CITY, CUST_CREDIT_LIMIT "
128
- + "FROM SH.CUSTOMERS WHERE ROWNUM < 20 order by CUST_ID " ) ) {
133
+ ResultSet resultSet = statement .executeQuery (queryStatement )) {
129
134
System .out .println ("\n CUST_ID" + " " + "CUST_FIRST_NAME" + " " + "CUST_LAST_NAME"
130
135
+ " " + "CUST_CITY" + " " + "CUST_CREDIT_LIMIT" );
131
136
System .out .println ("-----------------------------------------------------------" );
@@ -134,16 +139,8 @@ public static void doSQLWork(Connection conn) {
134
139
resultSet .getString (3 )+ " " + resultSet .getString (4 ) + " " +
135
140
resultSet .getInt (5 ));
136
141
}
137
- System .out .println ("\n Successfully established a connection to Autonomous Database\n " );
138
- }
139
- }
140
- }
141
- catch (SQLException e ) {
142
- System .out .println ("ADBQuickStart - "
143
- + "doSQLWork()- SQLException occurred : " + e .getMessage ());
144
- }
145
- } // doSQLWork
146
-
147
- } // ADBQuickStart
148
-
149
-
142
+ System .out .println ("\n Successfully established a connection to Autonomous Database\n " );
143
+ }
144
+ } // End of doSQLWork
145
+
146
+ } // End of ADBQuickStart
0 commit comments