Skip to content

Commit c47165b

Browse files
authored
Update ADBQuickStart.java
Addressed review comments
1 parent 51934ed commit c47165b

File tree

1 file changed

+45
-48
lines changed

1 file changed

+45
-48
lines changed

java/jdbc/ConnectionSamples/ADBQuickStart.java

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -46,34 +46,32 @@ online transaction processing operations. The Star Schema Benchmark (SSB) sample
4646
import oracle.ucp.jdbc.PoolDataSourceFactory;
4747
import oracle.ucp.jdbc.PoolDataSource;
4848

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 {
5054

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-
*/
6655
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+
6765
// For security purposes, you must enter the password through the console
6866
try {
6967
Scanner scanner = new Scanner(System.in);
7068
System.out.print("Enter the password for Autonomous Database: ");
7169
DB_PASSWORD = scanner.nextLine();
7270
}
73-
catch (Exception e) {
71+
catch (Exception e) {
7472
System.out.println("ADBQuickStart - Exception occurred : " + e.getMessage());
75-
}
76-
73+
System.exit(1);
74+
}
7775
// Get the PoolDataSource for UCP
7876
PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource();
7977

@@ -96,36 +94,43 @@ public static void main(String args[]) throws Exception {
9694
// connections allowed on the connection pool.
9795
pds.setMaxPoolSize(20);
9896

97+
9998
// Get the database connection from UCP.
10099
try (Connection conn = pds.getConnection()) {
101100
System.out.println("Available connections after checkout: "
102101
+ pds.getAvailableConnectionsCount());
103102
System.out.println("Borrowed connections after checkout: "
104103
+ pds.getBorrowedConnectionsCount());
104+
105105
// 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+
}
107112
}
108113

109114
System.out.println("Available connections after checkin: "
110115
+ pds.getAvailableConnectionsCount());
111116
System.out.println("Borrowed connections after checkin: "
112117
+ 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();
125132
// 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)) {
129134
System.out.println("\nCUST_ID" + " " + "CUST_FIRST_NAME" + " " + "CUST_LAST_NAME"
130135
+ " " + "CUST_CITY" + " " + "CUST_CREDIT_LIMIT");
131136
System.out.println("-----------------------------------------------------------");
@@ -134,16 +139,8 @@ public static void doSQLWork(Connection conn) {
134139
resultSet.getString(3)+ " " + resultSet.getString(4) + " " +
135140
resultSet.getInt(5));
136141
}
137-
System.out.println("\nSuccessfully 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("\nSuccessfully established a connection to Autonomous Database\n");
143+
}
144+
} // End of doSQLWork
145+
146+
} // End of ADBQuickStart

0 commit comments

Comments
 (0)