@@ -186,10 +186,6 @@ public List<Contato> getContatos() throws ContatoNaoExisteException, SQLExceptio
186186 // <editor-fold defaultstate="collapsed" desc="Comando(s) de Atualização/Alteração (UPDATE)">
187187 public void update (Contato contato ) throws ContatoNaoExisteException , SQLException {
188188
189- if (!contatoExiste (contato .getId ())) {
190- throw new ContatoNaoExisteException ("Não existe contato com esse ID." );
191- }
192-
193189 PreparedStatement ps = null ;
194190
195191 try {
@@ -199,7 +195,7 @@ public void update(Contato contato) throws ContatoNaoExisteException, SQLExcepti
199195 ps .setString (3 , contato .getEmail ());
200196 ps .setInt (4 , contato .getId ());
201197
202- ps .executeUpdate ();
198+ if ( ps .executeUpdate () == 0 ) throw new ContatoNaoExisteException ( "Não existe contato com esse ID." );
203199 } catch (SQLException e ) {
204200 throw new SQLException ("Não foi possível alterar/atualizar o contato." );
205201 } finally {
@@ -211,17 +207,13 @@ public void update(Contato contato) throws ContatoNaoExisteException, SQLExcepti
211207 // <editor-fold defaultstate="collapsed" desc="Comando(s) de Exclusão/Remoção (DELETE)">
212208 public void delete (int id ) throws ContatoNaoExisteException , SQLException {
213209
214- if (!contatoExiste (id )) {
215- throw new ContatoNaoExisteException ();
216- }
217-
218210 PreparedStatement ps = null ;
219211
220212 try {
221213 ps = con .prepareStatement ("DELETE FROM contato WHERE id = ?" );
222214 ps .setInt (1 , id );
223215
224- ps .executeUpdate ();
216+ if ( ps .executeUpdate () == 0 ) throw new ContatoNaoExisteException ();
225217 } catch (SQLException e ) {
226218 throw new SQLException ("Não foi possível excluir/remover o contato." );
227219 } finally {
@@ -230,31 +222,6 @@ public void delete(int id) throws ContatoNaoExisteException, SQLException {
230222 }
231223 // </editor-fold>
232224
233- private boolean contatoExiste (int contato_id ) {
234-
235- PreparedStatement ps = null ;
236- ResultSet rs = null ;
237-
238- boolean existe = false ;
239-
240- try {
241- ps = con .prepareStatement ("SELECT id FROM contato WHERE id = ?" );
242- ps .setInt (1 , contato_id );
243-
244- rs = ps .executeQuery ();
245-
246- if (rs .next ()) {
247- existe = true ;
248- }
249- } catch (SQLException e ) {
250- e .printStackTrace ();
251- } finally {
252- ConnectionFactory .closeConnection (ps , rs );
253- }
254-
255- return existe ;
256- }
257-
258225 public void close () {
259226 ConnectionFactory .closeConnection (con );
260227 }
0 commit comments