Skip to content

Commit e0ecc7d

Browse files
committed
Removido o método "contatoExiste(...)"; Usado o retorno do método "executeUpdate()" do PreparedStatement nos métodos "update(...)" e "delete(...) para saber se nenhuma linha foi afetada, assim não existindo tal contato.
1 parent 230197a commit e0ecc7d

File tree

2 files changed

+5
-38
lines changed

2 files changed

+5
-38
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ Projeto simples desenvolvido no NetBeans utilizando a linguagem Java fazendo int
55
As operações exemplificadas nesse projeto são as de: inserção, leitura, alteração e remoção (CRUD) de contatos no banco de dados MySQL.
66

77
* Downloads: https://github.com/iagocolodetti/JavaDBExemplo/releases
8-
* [Arquivo de Script MySQL](https://github.com/iagocolodetti/JavaDBExemplo/releases/download/v1.0/contatodb.sql "contatodb.sql")
9-
* [Driver Necessário](https://github.com/iagocolodetti/JavaDBExemplo/releases/download/v1.0/mysql-connector-java-5.1.23-bin.jar "mysql-connector-java-5.1.23-bin.jar")
10-
* [Código-Fonte](https://github.com/iagocolodetti/JavaDBExemplo/archive/v1.0.zip "v1.0.zip")
8+
* [Arquivo de Script MySQL](https://github.com/iagocolodetti/JavaDBExemplo/releases/download/v1.1/contatodb.sql "contatodb.sql")
9+
* [Driver Necessário](https://github.com/iagocolodetti/JavaDBExemplo/releases/download/v1.1/mysql-connector-java-5.1.23-bin.jar "mysql-connector-java-5.1.23-bin.jar")
10+
* [Código-Fonte](https://github.com/iagocolodetti/JavaDBExemplo/archive/v1.1.zip "v1.1.zip")
1111
<br/>
1212
<h3>Conectando o Banco de Dados MySQL no NetBeans</h3>
1313

src/ContatoDAO.java

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)