Extracted database connection code from K2.