Establishing a Database Connection


When a database is first created, it does not contain anything except the system catalog; before it can be used to store data, data objects such as tables, views, and indexes must be defined. And before new data objects can be defined (or anything else can be done with the database, for that matter), a connection to the database must be established. In most cases, a database connection will be established automatically if the Control Center is used to create data objects and work with data. If the Command Line Processor is used instead, a database connection can be established by executing the CONNECT SQL statement. The basic syntax for this statement is:

 CONNECT <TO [ServerName]> <IN SHARE MODE | IN EXCLUSIVE MODE> <USER [UserID] USING [Password]> <NEW PASSWORD [NewPassword] CONFIRM [NewPassword]> 

or

 CONNECT <USER [UserID] USING [Password]> <NEW PASSWORD [NewPassword] CONFIRM [NewPassword]> 

where:

ServerName

Identifies the name assigned to the application server a connection is to be established with. (A database alias is often used to identify an application server.)

UserID

Identifies the authentication ID (or user ID) assigned to the user attempting to establish the database connection.

Password

Identifies the password assigned to the user trying to establish the database connection. (It is important to note that passwords are case sensitive.)

NewPassword

Identifies the new password the user trying to establish the database connection wishes to have associated with his/her authentication ID.

Thus, in order for a user whose authentication ID is DB2USER and whose password is IBMDB2 to establish a connection to a database named SAMPLE, a CONNECT statement that looks something like this would need to be executed:

 CONNECT TO sample USER db2user USING ibmdb2 

And as soon as the CONNECT statement is successfully executed, you might see a message that looks something like this:

 Database Connection Information Database server         = DB2/NT 9.1.0 SQL authorization ID    = DB2USER Local database alias    = SAMPLE 

Tip 

The CONNECT statement can be executed without specifying a user ID and password (since these are optional parameters). When the CONNECT statement is executed without this information, the DB2 Database Manager will either attempt to use the user ID and password you provided to gain access to the system or prompt you for this information. Such a connect operation is called an implicit connect, as it is implied that the credentials of the current user are to be used; on the other hand, when a user ID and password are specified, the connect operation is called an explicit connect, because the required user credentials have been explicitly provided.

Once a database connection has been established, it will remain in effect until it is explicitly terminated or until the application that established the connection ends. Database connections can be explicitly terminated at any time by executing a special form of the CONNECT statement. The syntax for this form of the CONNECT statement is:

 CONNECT RESET 




DB2 9 Fundamentals Certification Study Guide
DB2 9 Fundamentals: Certification Study Guide
ISBN: 1583470727
EAN: 2147483647
Year: 2007
Pages: 93

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net