DB2 10.5 for Linux, UNIX, and Windows

Database connections in Perl

The DBD::DB2 driver provides support for standard database connection functions defined by the DBI API.

To enable Perl to load the DBI module, you must include the use DBI; line in your application:

The DBI module automatically loads the DBD::DB2 driver when you create a database handle using the DBI->connect statement with the listed syntax:
   my $dbhandle = DBI->connect('dbi:DB2:dsn', $userID, $password);
where:
$dbhandle
represents the database handle returned by the connect statement
dsn

for local connections, represents a DB2® alias cataloged in your DB2 database directory

for remote connections, represents a complete connection string that includes the host name, port number, protocol, user ID, and password for connecting to the remote host

$userID
represents the user ID used to connect to the database
$password
represents the password for the user ID used to connect to the database

For more information about the DBI API, see http://search.cpan.org/~timb/DBI/DBI.pm.

Example

Example 1: Connect to a database on the local host (client and server are on the same workstation)

use DBI;

$DATABASE = 'dbname';
$USERID = 'username';
$PASSWORD = 'password';

my $dbh = DBI->connect("dbi:DB2:$DATABASE", $USERID, $PASSWORD, {PrintError => 0})
or die "Couldn't connect to database: " . DBI->errstr;

$dbh->disconnect;

Example 2: Connect to a database on the remote host (client and server are on different workstations)

use DBI;

$DSN="DATABASE=sample; HOSTNAME=host; PORT=60000; PROTOCOL=TCPIP; UID=username; 
PWD=password";

my $dbh = DBI->connect("dbi:DB2:$DSN", $USERID, $PASSWORD, {PrintError => 0})
or die "Couldn't connect to database: " . DBI->errstr;

$dbh->disconnect;