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:
my $dbhandle = DBI->connect('dbi:DB2:dsn', $userID, $password);
where: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
For more information about the DBI API, see http://search.cpan.org/~timb/DBI/DBI.pm.
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;