forked from oracle-samples/oracle-db-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJDBCUrlSample.java
More file actions
executable file
·378 lines (332 loc) · 13.5 KB
/
JDBCUrlSample.java
File metadata and controls
executable file
·378 lines (332 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.*/
/**
* DESCRIPTION
*
* This class demonstrates how to specify a URL to the Oracle JDBC Driver.
* <br>
* The demo can be configured to use the following forms of URLs:
* <ol>
* <li>Thin-Style</li>
* <li>Oracle Net Connect Descriptor</li>
* <li>TNS Alias</li>
* </ol>
*
* <h3>Thin-Style URL</h3>
* Thin-style is the simplest form of URL. It consists of a host, port, and a
* service name or system identifier (SID). To run the demo using a thin-style
* URL, provide command line arguments in either of the following forms:
* <br>
* <code>-t {host} {port} {sid}</code>
* <br>
* <code>-t {host} {port} /{service_name}</code>
* <br>
* Note the '/' character is used to differentiate between an SID and service name.
*
* <h3>Connect Descriptor URL</h3>
* Connect descriptors offer a syntax for advanced configuration of the
* network connection. To learn about the syntax, see the Oracle Net Database
* Net Services Guide linked to below. This demo will use a minimal
* configuration consisting of a host, port, and service name or SID. To run
* the demo using a connect descriptor URL, provide command line arguments in
* either of the following forms:
* <br>
* <code>-c {host} {port} {sid}</code>
* <br>
* <code>-c {host} {port} /{service_name}</code>
* <br>
* Note the '/' character is used to differentiate between an SID and service name.
*
* <h3>TNS Alias URL</h3>
* A TNS alias is a reference to an connect descriptor defined in a
* tnsnames.ora file. To learn more about tnsnames.ora, see the Oracle
* Database Net Services Reference linked to below. To run the demo using a
* TNS alias URL, provide command line arguments in either of the following
* forms:
* <br>
* <code>-a {alias} {tns_names_dir}</code>
* <br>
* Where tns_names_dir is the directory which holds tnsnames.ora.
*
* <h3>Further Reading</h3>
* <ul>
* <li>
* To learn more about URL's and the Oracle JDBC Driver, have a look at our
* Developer's Guide.
* <a target="_blank" href="http://www.oracle.com/technetwork/database/application-development/jdbc/learnmore/index.html">
* The latest version can be found here.
* </a> URLs are discussed in <i>Chapter 8 Data Sources and URLs</i>.
* </li>
* <li>
* To learn more about Oracle Net concepts (such as the difference between a
* service and SID, or the full syntax of a connect descriptor), see
* <i>Chapter 2 Identifying and Accessing the Database</i> in the Oracle
* Database Net Services Guide.
* <a target="_blank" href="https://docs.oracle.com/en/database/oracle/oracle-database/18/netag/identifying-and-accessing-database.html#GUID-5BC573CE-BA12-4251-A987-429095385EC2">
* The latest version is 18.1, which can be found here
* </a>
* </li>
* <li>
* To learn more about TNS names aliases and the syntax of tnsnames.ora, see
* <i>Chapter 6 Local Naming Parameters in the tnsnames.ora File</i> in the
* Oracle Database Net Services Reference.
* <a target="_blank" href="https://docs.oracle.com/en/database/oracle/oracle-database/18/netrf/local-naming-parameters-in-tnsnames-ora-file.html#GUID-A3F9D023-9CC4-445D-8921-6E40BD900EAD">
* The latest version is 18.1, which can be found here.
* </a>
* </li>
* </ul>
*/
import java.io.Console;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Objects;
import java.util.Properties;
import oracle.jdbc.pool.OracleDataSource;
import oracle.jdbc.OracleConnection;
public class JDBCUrlSample {
// Default credentials to use if a console is not available. If you are
// running this demo in an IDE like Eclipse, you may need to define these
// values.
private static final String DEFAULT_USER = "";
private static final String DEFAULT_PASSWORD = "";
/** The URL this demo will use */
private final String url;
/** Connection properties this demo will use. */
private final Properties connectionProperties;
private JDBCUrlSample(String url){
this(url, new Properties());
}
private JDBCUrlSample(String url, Properties connectionProperties) {
this.url = url;
this.connectionProperties = connectionProperties;
}
/**
* Returns a new demo object initialized with a thin-style URL using a
* database service name.
* <p style="white-space:pre;">
* The URL syntax is:
* jdbc:oracle:thin:@{host_name}:{port_number}/{service_name}
* </p>
* @param host The hostname of an Oracle Database.
* @param port The port number of an Oracle Database.
* @param service The service name of an Oracle Database.
* @return A newly instantiated demo object.
*/
public static JDBCUrlSample newThinStyleServiceNameDemo(String host,
int port, String service) {
Objects.requireNonNull(host, "Host cannot be null");
Objects.requireNonNull(service, "Service name cannot be null");
String thinStyleURL =
"jdbc:oracle:thin:@" + host + ":" + port + "/" + service;
return new JDBCUrlSample(thinStyleURL);
}
/**
* Returns a new demo object initialized with a thin-style URL using
* a database SID.
* <p style="white-space:pre;">
* The URL syntax is:
* jdbc:oracle:thin:@{host_name}:{port_number}:{sid}
* </p>
* @param host The hostname of an Oracle Database.
* @param port The port number of an Oracle Database.
* @param sid The system identifier of an Oracle Database.
* @return A newly instantiated demo object.
*/
public static JDBCUrlSample newThinStyleSIDDemo(String host, int port,
String sid) {
Objects.requireNonNull(host, "Host cannot be null");
Objects.requireNonNull(sid, "SID cannot be null");
String thinStyleURL =
"jdbc:oracle:thin:@" + host + ":" + port + ":" + sid;
return new JDBCUrlSample(thinStyleURL);
}
/**
* Returns a new demo object initialized with a connect descriptor URL using
* a service name.
* <p style="white-space:pre;">
* The URL syntax is:
* jdbc:oracle:thin:@(DESCRIPTION=
* (ADDRESS=(PROTOCOL=tcp)(HOST={host_name})(PORT={port}))
* (CONNECT_DATA=(SERVICE_NAME={service_name})))
* </p>
* @param host The hostname of an Oracle Database.
* @param port The port number of an Oracle Database.
* @param service The service name of an Oracle Database.
* @return A newly instantiated demo object.
*/
public static JDBCUrlSample newConnectDescriptorServiceNameDemo(String host,
int port, String service) {
Objects.requireNonNull(host, "Host cannot be null");
Objects.requireNonNull(service, "Service name cannot be null");
String connectDescriptorURL =
"jdbc:oracle:thin:@(DESCRIPTION="
+ "(ADDRESS=(PROTOCOL=tcp)(HOST=" + host + ")(PORT=" + port + "))"
+ "(CONNECT_DATA=(SERVICE_NAME=" + service + ")))";
return new JDBCUrlSample(connectDescriptorURL);
}
/**
* Returns a new demo object initialized with a connect descriptor URL using
* a database SID.
* <p style="white-space:pre;">
* The URL syntax is:
* jdbc:oracle:thin:@(DESCRIPTION=
* (ADDRESS=(PROTOCOL=tcp)(HOST={host_name})(PORT={port}))
* (CONNECT_DATA=(SID={sid})))
* </p>
* @param host The hostname of an Oracle Database.
* @param port The port number of an Oracle Database.
* @param sid The system identifier of an Oracle Database.
* @return A newly instantiated demo object.
*/
public static JDBCUrlSample newConnectDescriptorSIDDemo(String host, int port,
String sid) {
Objects.requireNonNull(host, "Host cannot be null");
Objects.requireNonNull(sid, "SID cannot be null");
String connectDescriptorURL =
"jdbc:oracle:thin:@(DESCRIPTION="
+ "(ADDRESS=(PROTOCOL=tcp)(HOST=" + host + ")(PORT=" + port + "))"
+ "(CONNECT_DATA=(SID=" + sid + ")))";
return new JDBCUrlSample(connectDescriptorURL);
}
/**
* Returns a new demo object initialized with a TNS Alias URL.
* The syntax is:
* <p style="white-space:pre;">
* jdbc:oracle:thin:@{tns_alias}
* </p>
* @param alias A tnsnames.ora alias.
* @param tnsAdmin The directory of a tnsnames.ora file.
* @return A newly instantiated demo object.
*/
public static JDBCUrlSample newTNSAliasDemo(String alias, String tnsAdmin) {
Objects.requireNonNull(alias, "Alias cannot be null");
Objects.requireNonNull(tnsAdmin, "TNS Admin cannot be null");
String tnsAliasURL = "jdbc:oracle:thin:@" + alias;
// The directory of tnsnames.ora is defined as a connection property.
Properties connectionProperties = new Properties();
connectionProperties.setProperty(
OracleConnection.CONNECTION_PROPERTY_TNS_ADMIN, tnsAdmin);
return new JDBCUrlSample(tnsAliasURL, connectionProperties);
}
/**
* Use this demo's URL to establish a connection.
*/
public void connectWithURL() {
try {
// oracle.jdbc.pool.OracleDataSource is a factory for Connection
// objects.
OracleDataSource dataSource = new OracleDataSource();
// The data source is configured with database user and password.
setCredentials(dataSource);
// The data source is configured with connection properties.
dataSource.setConnectionProperties(connectionProperties);
// The data source is configured with a database URL.
dataSource.setURL(url);
// The data source is used to create a Connection object.
System.out.println("\nConnecting to: " + url);
Connection connection = dataSource.getConnection();
System.out.println("Connection Established!");
// Close the connection when its no longer in use. This will free up
// resources in the Java client and the database host.
connection.close();
}
catch (SQLException sqlE) {
// The getConnection() call throws a SQLException if a connection could
// not be established.
displayConnectionError(sqlE);
}
}
private void setCredentials(OracleDataSource dataSource) {
String user;
char[] password;
Console console = System.console();
if(console == null) {
System.out.println(
"\nNo console available. Using default user and password.");
user = DEFAULT_USER;
password = DEFAULT_PASSWORD.toCharArray();
}
else {
user = console.readLine("\nUser: ");
password = console.readPassword(user + "'s password: ");
}
dataSource.setUser(user);
dataSource.setPassword(new String(password));
Arrays.fill(password, ' ');
}
private void displayConnectionError(SQLException sqlE) {
System.out.println(
"Connection establishment failed with the following error:");
Throwable cause = sqlE;
do {
System.out.println(cause.getMessage());
cause = cause.getCause();
} while(cause != null);
}
// All code beyond this point is related to command line argument parsing.
private static final String THIN_STYLE_OPTION = "-t";
private static final String DESCRIPTOR_OPTION = "-c";
private static final String TNS_ALIAS_OPTION = "-a";
private static final String USAGE_MESSAGE =
"Please provide command line arguments in one of the following forms:"
+ "\n\nThin-Style URL:\n\t"
+ THIN_STYLE_OPTION + " {host} {port} {sid}\n\t"
+ THIN_STYLE_OPTION + " {host} {port} /{service_name}"
+ "\n\nOracle Net Connect Descriptor:\n\t"
+ DESCRIPTOR_OPTION + " {host} {port} {sid}\n\t"
+ DESCRIPTOR_OPTION + " {host} {port} /{service_name}"
+ "\n\nTNS Names Alias:\n\t"
+ TNS_ALIAS_OPTION + " {alias} {tnsnames_directory}";
private static int ARG_POSITION_HOST = 1;
private static int ARG_POSITION_PORT = 2;
private static int ARG_POSITION_SERVICE = 3;
private static int THIN_STYLE_ARG_COUNT = 4;
private static int DESCRIPTOR_ARG_COUNT = 4;
private static int ARG_POSITION_ALIAS = 1;
private static int ARG_POSITION_TNS_ADMIN = 2;
private static int TNS_ALIAS_ARG_COUNT = 3;
public static void main(String[] args) {
// Read the URL type argument and initialize a demo for it.
JDBCUrlSample demo = null;
String typeOption = args.length > 0 ? args[0] : "";
switch(typeOption) {
case THIN_STYLE_OPTION:
if(args.length == THIN_STYLE_ARG_COUNT) {
String host = args[ARG_POSITION_HOST];
int port = Integer.valueOf(args[ARG_POSITION_PORT]);
String service = args[ARG_POSITION_SERVICE];
demo = service.startsWith("/")
? newThinStyleServiceNameDemo(host, port,
service.substring(1))
: newThinStyleSIDDemo(host, port, service);
}
break;
case DESCRIPTOR_OPTION:
if(args.length == DESCRIPTOR_ARG_COUNT) {
String host = args[ARG_POSITION_HOST];
int port = Integer.valueOf(args[ARG_POSITION_PORT]);
String service = args[ARG_POSITION_SERVICE];
demo = service.startsWith("/")
? newConnectDescriptorServiceNameDemo(host, port,
service.substring(1))
: newConnectDescriptorSIDDemo(host, port, service);
}
break;
case TNS_ALIAS_OPTION:
if(args.length == TNS_ALIAS_ARG_COUNT) {
String alias = args[ARG_POSITION_ALIAS];
String tnsAdmin = args[ARG_POSITION_TNS_ADMIN];
demo = newTNSAliasDemo(alias, tnsAdmin);
}
break;
default:
demo = null;
}
if(demo == null) {
System.out.println(USAGE_MESSAGE);
}
else {
demo.connectWithURL();
}
}
}