beautypg.com

A.3 ruby – IBM Data Server DB2 User Manual

Page 277

background image

Appendix A. Sample applications

263

$conn = db2_connect($dsn, '', '');

if ($conn) {

echo "Connection succeeded.\n";
echo "Closing connection...\n";
db2_close($conn);
echo "Connection closed.\n";

}
else {

echo db2_conn_errormsg(), "\n";
echo "Connection failed.\n";

}

?>

A.3 Ruby

Example A-3 shows the application used to demonstrate the deployment of the
DB2 PHP application.

Example: A-3 Sample Ruby application

# A sample Ruby application.
# Use this program to test connection to a database.
# Database connectivity information is read as command line arguments.
# Give arguments in the following order:
# hostname port_number database_name user password
#
# Run the application as following:
# ruby itso_rubyapp.rb hostname port_number database_name user password
#

require 'ibm_db'

if ARGV.length != 5

puts " ERROR: incorrect command line arguments.\n Use hostname port_number

database_name user_name password\n"
else
# Creating dsn from command line arguments.

dsn = ""
dsn << "HOSTNAME=" << ARGV[0] << \

";PORT=" << ARGV[1] << \
";DATABASE=" << ARGV[2] << \