Connecting to Db2 databases in embedded SQL applications
Before working with
a database, you must establish a connection to that database. Embedded
SQL provides multiple ways in which to include code for establishing
database connections. Depending on which host programming language
you use, there might be one or more way to establish a database connection.
Database connections can be established implicitly or explicitly. An implicit connection is a connection where the user ID is presumed to be the current user ID. This type of connection is not recommended for database applications. Explicit database connections, which require that a user ID and password be specified, are strongly recommended.
Connecting to Db2 databases in C and C++ Embedded SQL applications
When working with
C and C++ applications, a database connection can be established
by executing the following statement.
EXEC SQL CONNECT TO sample;
If you want to use a specific user id (herrick
)
and password (mypassword
), use the following statement:
EXEC SQL CONNECT TO sample USER herrick USING mypassword;
Note that if the precompiler option COMPATIBILITY_MODE is set to ORA, the
following additional syntax for the CONNECT statement is supported. The Db2 database manager
provides this feature to facilitate the migration of embedded SQL C applications from other database
systems.
EXEC SQL CONNECT [ username IDENTIFIED BY password ][ USING dbname ] ;
The parameters are described in the following table:
Parameter | Description |
---|---|
username | Either a host variable or a string specifying the database user name |
password | Either a host variable or a string specifying the password |
dbname | Either a host variable or a string specifying the database name |
Connecting to Db2 databases in COBOL Embedded SQL applications
When working with COBOL
applications, a database connection is established by executing
the following statement. This statement creates a connection to the
sample database using the default user name.
EXEC SQL CONNECT TO sample END-EXEC.
If you want to use a specific user id (herrick
)
and password (mypassword
), use the following statement:
EXEC SQL CONNECT TO sample USER herrick USING mypassword END-EXEC.
Connecting to Db2 databases in FORTRAN Embedded SQL applications
When working with
FORTRAN applications, a database connection is established by executing
the following statement. This statement creates a connection to
the sample database using the default user name.
EXEC SQL CONNECT TO sample
If you want to use a specific user id (herrick
)
and password (mypassword
), use the following statement:
EXEC SQL CONNECT TO sample USER herrick USING mypassword
Connecting to Db2 databases in REXX Embedded SQL applications
When working with REXX applications, a database connection
is established by executing the following statement. This statement
creates a connection to the sample database using the default
user name.
CALL SQLEXEC 'CONNECT TO sample'
If you want to use a specific user id (herrick
)
and password (mypassword
), use the following statement:
CALL SQLEXEC 'CONNECT TO sample USER herrick USING mypassword'