Table of Contents Previous Next



7.5.5 CONNECT
Use the CONNECT statement to establish a connection to a database. The CONNECT statement is available in two forms - one form is compatible with Oracle databases, the other is not.
EXEC SQL CONNECT
{{:
user_name IDENTIFIED BY :password} | :connection_id}
[AT
database_name]
[USING :
database_string]
[ALTER AUTHORIZATION :new_password];
user_name is a host variable that contains the role that the client application will use to connect to the server.
password is a host variable that contains the password associated with that role.
connection_id is a host variable that contains a slash-delimited user name and password used to connect to the database.
Include the AT clause to specify the database to which the connection is established. database_name is the name of the database to which the client is connecting; specify the value in the form of a variable, or as a string literal.
Include the USING clause to specify a host variable that contains a null-terminated string identifying the database to which the connection will be established.
The ALTER AUTHORIZATION clause is supported for syntax compatibility only; ECPGPlus parses the ALTER AUTHORIZATION clause, and reports a warning.
Using the first form of the CONNECT statement, a client application might establish a connection with a host variable named user that contains the identity of the connecting role, and a host variable named password that contains the associated password using the following command:
A client application could also use the first form of the CONNECT statement to establish a connection using a single host variable named :connection_id. In the following example, connection_id contains the slash-delimited role name and associated password for the user:
EXEC SQL CONNECT TO database_name
[AS
connection_name] [credentials];
Where credentials is one of the following:
USER user_name password
USER
user_name IDENTIFIED BY password
USER
user_name USING password
database_name is the name or identity of the database to which the client is connecting. Specify database_name as a variable, or as a string literal, in one of the following forms:
database_name[@hostname][:port]
tcp:postgresql://hostname[:port][/database_name][options]
unix:postgresql://hostname[:port][/database_name][options]
hostname is the name or IP address of the server on which the database resides.
port is the port on which the server listens.
You can also specify a value of DEFAULT to establish a connection with the default database, using the default role name. If you specify DEFAULT as the target database, do not include a connection_name or credentials.
connection_name is the name of the connection to the database. connection_name should take the form of an identifier (that is, not a string literal or a variable). You can open multiple connections, by providing a unique connection_name for each connection.
If you do not specify a name for a connection, ecpglib assigns a name of DEFAULT to the connection. You can refer to the connection by name (DEFAULT) in any EXEC SQL statement.
CURRENT is the most recently opened or the connection mentioned in the most-recent SET CONNECTION TO statement. If you do not refer to a connection by name in an EXEC SQL statement, ECPG assumes the name of the connection to be CURRENT.
user_name is the role used to establish the connection with the Advanced Server database. The privileges of the specified role will be applied to all commands performed through the connection.
password is the password associated with the specified user_name.
The following code fragment uses the second form of the CONNECT statement to establish a connection to a database named edb, using the role alice and the password associated with that role, 1safepwd:
The name of the connection is acctg_conn; you can use the connection name when changing the connection name using the SET CONNECTION statement.


Table of Contents Previous Next