extract db connection code