Use handler directly as otherwise the output is not being correctly flushed
Extract getConnection() method that consolidates error checking
Improve handling of update sql