Database connect function

added try/catch and globals