Sub topic 3
Ways to identify an Oracle database: Easy Connect, TNS alias, JDBC URL
Assume you know basic SQL, can use a terminal, and have an Oracle Database account. You also need the database host, listener port, and service name, or a connection configuration that supplies them.
The connection identifier
An Oracle client needs a connect identifier, which tells it where to find the database. A typical connection also includes:
- Host: the DNS name or IP address of the database server.
- Port: the listener port, commonly
1521. - Service name: the logical database service requested from the listener.
- Credentials: the database username and password.
A service name is usually preferred for modern Oracle Database connections. A SID identifies a particular database instance, while a service can route sessions to one or more instances. Do not substitute a SID for a service name unless your environment specifically requires it.
Easy Connect syntax
Easy Connect puts the host, port, and service name directly in the connection string. Its common form is:
//host:port/service_nameFor example, SQLcl can use this identifier:
sql hr@//dbhost.example.com:1521/orclpdb1.example.comHere, dbhost.example.com is the host, 1521 is the listener port, and orclpdb1.example.com is the service name. If the port is the default, some clients allow you to omit it:
//dbhost.example.com/orclpdb1.example.comEasy Connect is useful for testing and small scripts because it needs no tnsnames.ora file. It also makes the destination visible in the command. Keep passwords out of the command line because shells, process listings, and command histories may expose them.
TNS aliases
A TNS alias is a short name that refers to a longer connection description stored in a tnsnames.ora file. The file can define the host, port, protocol, and service name once.
ORCLPDB =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = dbhost.example.com)(PORT = 1521))
(CONNECT_DATA = (SERVICE_NAME = orclpdb1.example.com))
)A client can then connect with:
hr@ORCLPDBSQL Developer and SQLcl must know where to find tnsnames.ora. The Oracle network configuration directory is often selected through the TNS_ADMIN environment variable. If the alias is not found, check the file location, spelling, and the active Oracle configuration directory.
TNS aliases are convenient when many tools or users share standard database destinations. Changing the host or service in one file updates every client that uses the alias.
JDBC URLs
A JDBC URL is the connection format used by Java applications and tools that use JDBC, the Java Database Connectivity API. The equivalent URL for the earlier example is:
jdbc:oracle:thin:@//dbhost.example.com:1521/orclpdb1.example.comThe thin driver connects directly without requiring a local Oracle client installation. A tool may ask for this URL separately from the username and password.
| Format | Example | Best use |
|---|---|---|
| Easy Connect | `//host:1521/service` | Direct client connections and quick tests |
| TNS alias | `ORCLPDB` | Shared, centrally managed configuration |
| JDBC URL | `jdbc:oracle:thin:@//host:1521/service` | Java applications and JDBC-based tools |
Exercise
Which connect identifier would you choose for a Java application that must connect directly to dbhost.example.com on port 1521 using service salespdb?
Show answer
Use jdbc:oracle:thin:@//dbhost.example.com:1521/salespdb.