ex17: Close db connection on error

Also added:
    * Function prototypes
    * Account for negative IDs