We create a connection to a database by invoking the DBI class method connect().
If there is a failure, connect() returns an undef value and we can check $DBI::err and $DBI::errstr for the reasons why.
#!/usr/bin/perl use strict; use warnings; use DBI; my $database = 'MYDB'; my $user = 'lynn'; my $password = '5tuff'; my $hostname = 'localhost'; my $port = 1527; my $DSN = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=$database;" . "HOSTNAME=$hostname;PORT=$port;PROTOCOL=TCPIP;"; my $conn = DBI->connect("dbi:DB2:$DSN", $user, $password, { AutoCommit => 1, PrintError => 0 } ); # Check to see if the connection was successful if (!$conn) { print "Connection failed: {$DBI::errstr}\n"; } else { print "Connection succeeded.\n"; # Explicitly close our connection $conn->disconnect(); }