Connecting programmatically with ODBC or CLI

Define a connection between a Microsoft Windows ODBC/CLI application and the Db2 database, BLUDB.

Before you begin

Before you can connect to your database, you must carry out the following steps:
  • Verify prerequisites, including installing driver packages, configuring your local environment, and downloading SSL certificates (if needed)

  • Collect connect information, including database details such as host name and port numbers, and connect credentials such as user ID and password

Procedure

  1. In a command shell on Linux operating systems, at the Windows command prompt, or in the Db2 command window on Windows operating systems, enter the following commands.

    These commands create new entries in the driver configuration file (db2dsdriver.cfg) on your computer and set the connection attributes. You need to do this step only one time.

    • For a connection without SSL:
      db2cli writecfg add -database BLUDB -host hostname -port 50000
      db2cli writecfg add -dsn alias -database BLUDB -host hostname -port 50000
    • For a connection with SSL:
      db2cli writecfg add -database BLUDB -host hostname -port 50001 -parameter "SecurityTransportMode=SSL"
      db2cli writecfg add -dsn alias -database BLUDB -host hostname -port 50001
    where:
    • hostname is the host name of the server
    • alias is an alias you choose
  2. Optional: To test the connection to the database, run this command from the command prompt:
    db2cli validate -dsn alias -connect -user userid -passwd password
    where:
    • alias is the alias you created with the db2cli writecfg command
    • userid is from the connect credentials you collected beforehand
    • password is from the connect credentials you collected beforehand
  3. Optional: To register the data source name (DSN) with Microsoft ODBC Driver Manger and to work with Microsoft ODBC applications, run the following command.
    By default, the DSN is created as user DSN.
    db2cli registerdsn -add -dsn  alias
    where
    • alias is the alias that you created with the db2cli writecfg command