Skip to content

Commit 19c34f2

Browse files
authored
Add files via upload
1 parent 1a5ad95 commit 19c34f2

File tree

2 files changed

+214
-0
lines changed

2 files changed

+214
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
2+
/* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.*/
3+
/*
4+
DESCRIPTION
5+
The code sample shows how to use the JDBC driver to establish a connection
6+
to the Autonomous Database (ADB) using database token
7+
issued by the OCI Identity service.
8+
9+
You need to use either JDBC driver to use
10+
database token authenticatio.
11+
12+
Step 1: Enter the DB_URL to pointing to your Autonomous Database (ADB)
13+
Step 2: Make sure to have either 21.4.0.0.1 or 19.13.0.0.1 JDBC driver
14+
in the classpath.
15+
Step 2: Compile and Run the sample JDBCDBTokenSample
16+
17+
NOTES
18+
Use JDK 1.7 and above
19+
MODIFIED (MM/DD/YY)
20+
nbsundar 1/7/21 - Creation
21+
*/
22+
23+
import java.io.IOException;
24+
import java.io.InputStream;
25+
import java.sql.Connection;
26+
import java.sql.ResultSet;
27+
import java.sql.SQLException;
28+
import java.sql.Statement;
29+
import java.util.Properties;
30+
31+
import oracle.jdbc.pool.OracleDataSource;
32+
import oracle.jdbc.OracleConnection;
33+
import java.sql.DatabaseMetaData;
34+
35+
public class JDBCDBTokenSample {
36+
37+
//If mutual TLS (mTLS) is enabled then, ADB connection requires wallets.
38+
// Download the wallet zip file and provide the path to the zip file as TNS_ADMIN
39+
// Note that you need to pass the property oracle.jdbc.tokenAuthentication=OCI_TOKEN for token authentication
40+
final static String DB_URL="jdbc:oracle:thin:@demodb_high?TNS_ADMIN=/Users/nbsundar/ATPTesting/Wallet_DemoDB&oracle.jdbc.tokenAuthentication=OCI_TOKEN";
41+
// If mutla TLS(mTLS) is disabled then, ADB connection does not require wallets.
42+
// Copy the connection string from "DB Connection" tab from "Connection Strings" section choosing "TLS" in the dropdown
43+
//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";
44+
45+
46+
public static void main(String args[]) throws SQLException {
47+
48+
// For more connection related properties. Refer to
49+
// the OracleConnection interface.
50+
Properties properties = new Properties();
51+
52+
properties.put(OracleConnection.CONNECTION_PROPERTY_DEFAULT_ROW_PREFETCH, "20");
53+
properties.put(OracleConnection.CONNECTION_PROPERTY_THIN_NET_CHECKSUM_TYPES,
54+
"(MD5,SHA1,SHA256,SHA384,SHA512)");
55+
properties.put(OracleConnection.CONNECTION_PROPERTY_THIN_NET_CHECKSUM_LEVEL,
56+
"REQUIRED");
57+
// Connection property to enable database token authentication.
58+
// properties.put(OracleConnection.CONNECTION_PROPERTY_TOKEN_AUTHENTICATION, "OCI_TOKEN");
59+
60+
61+
OracleDataSource ods = new OracleDataSource();
62+
ods.setURL(DB_URL);
63+
ods.setConnectionProperties(properties);
64+
65+
// With AutoCloseable, the connection is closed automatically.
66+
try (OracleConnection connection = (OracleConnection) ods.getConnection()) {
67+
// Get the JDBC driver name and version
68+
DatabaseMetaData dbmd = connection.getMetaData();
69+
System.out.println("Driver Name: " + dbmd.getDriverName());
70+
System.out.println("Driver Version: " + dbmd.getDriverVersion());
71+
// Print some connection properties
72+
System.out.println("Default Row Prefetch Value is: " +
73+
connection.getDefaultRowPrefetch());
74+
System.out.println("Database Username is: " + connection.getUserName());
75+
System.out.println();
76+
// Perform a database operation
77+
printTableNames(connection);
78+
}
79+
}
80+
/*
81+
* Displays 15 table_name from all_tables.
82+
*/
83+
public static void printTableNames(Connection connection) throws SQLException {
84+
// Statement and ResultSet are AutoCloseable and closed automatically.
85+
try (Statement statement = connection.createStatement()) {
86+
try (ResultSet resultSet = statement
87+
.executeQuery("select table_name from all_tables where rownum < 15")) {
88+
System.out.println("Table name");
89+
System.out.println("---------------------");
90+
while (resultSet.next())
91+
System.out.println(resultSet.getString(1));
92+
}
93+
}
94+
}
95+
}
96+
97+
98+
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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

Comments
 (0)