Sub topic 10
Saving and Reusing Connections in SQLcl
Assume that you already know basic SQL and can connect to an Oracle Database from a terminal. You also need SQLcl installed and a reachable database account. This chapter shows how to save a connection under a name and reuse it without retyping the full connect identifier.
Why save a connection?
An Oracle connection includes a username, a password, and a connect identifier. The identifier might contain a host, port, and service name, such as:
hr@//dbhost.example.com:1521/orclpdb1.example.comTyping this identifier repeatedly is error-prone. SQLcl can store the connection details under a short name, such as orclpdb_hr.
The saved name is local to the SQLcl environment. It does not create an Oracle user or change the database.
flowchart TD
A[Full connection details] --> B[Save as orclpdb_hr]
B --> C[connmgr list]
C --> D[connect -name orclpdb_hr]
D --> E[Connected SQLcl session]Save a connection
Start SQLcl without connecting. This lets SQLcl prompt for the password instead of placing it in a shell command:
sql /nologAt the SQL> prompt, save the connection under a name:
SQL> connect -save orclpdb_hr hr@//dbhost.example.com:1521/orclpdb1.example.com
Password? (**********?) ********
Connected.Here, -save tells SQLcl to save the connection as orclpdb_hr. The connection identifier still specifies the username, host, port, and service name.
You can also save the password for later reuse:
SQL> connect -save orclpdb_hr -savepwd hr@//dbhost.example.com:1521/orclpdb1.example.comUse -savepwd only on a machine whose local users and files you trust. A saved password reduces prompts, but it also increases the impact of unauthorized access to your local SQLcl configuration.
List and reuse saved connections
Use the connection manager to display saved connection names:
SQL> connmgr listReconnect by using the saved name:
SQL> connect -name orclpdb_hrSQLcl retrieves the saved connection details and opens a new session. The command does not require you to repeat the host, port, service name, or username.
| Command | Purpose |
|---|---|
| `connect -save name user@identifier` | Save a connection, usually prompting for the password |
| `connect -save name -savepwd user@identifier` | Save the connection and password for reuse |
| `connmgr list` | List saved connection names |
| `connect -name name` | Reuse a saved connection |
Update or remove saved details
If a database host, service name, or password changes, save the connection again with the same name and the updated details. Avoid creating several names that point to the same database unless the different names communicate a real purpose, such as reporting_readonly and admin_maintenance.
Treat saved connections as local secrets. Do not commit SQLcl configuration files to source control, copy them into shared directories, or use saved passwords in a shared automation account. For unattended jobs, prefer an approved secret manager or an operating-system credential mechanism.
Exercise
Which command reconnects to a saved SQLcl connection named orclpdb_hr?
Show answer
connect -name orclpdb_hr