Once you have cataloged the database connection, connecting to Apache Derby from pyDB2 is relatively easy.
- 1. Create a function library named menu.py that contains the following code:
#!/usr/bin/python import DB2 def connect(database='MYDB', user='lynn', password='5tuff'): return DB2.connect(database, user, password)
- 2. Create a Python script that will import your new function library and create a connection to Apache Derby:
#!/usr/bin/python import sys import DB2 # import the functions in menu.py within this script import menu # Create the connection with default parameter values try: conn = menu.connect() except Exception, e: sys.exit("Failed to connect: %s" % e) # Here is where we would do real work; just print connection status print "Connection succeeded!" # Clean up the connection conn.close()
- 3. Now connect to Apache Derby by running the script from the command line:
bash$ python connect.py