Skip to content

Commit

Permalink
Removido o método "contatoExiste(...)"; Usado o retorno do método "ex…
Browse files Browse the repository at this point in the history
…ecuteUpdate()" do PreparedStatement nos métodos "update(...)" e "delete(...) para saber se nenhuma linha foi afetada, assim não existindo tal contato.
  • Loading branch information
iagocolodetti committed Mar 5, 2019
1 parent 230197a commit e0ecc7d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 38 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ Projeto simples desenvolvido no NetBeans utilizando a linguagem Java fazendo int
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.

* Downloads: https://github.com/iagocolodetti/JavaDBExemplo/releases
* [Arquivo de Script MySQL](https://github.com/iagocolodetti/JavaDBExemplo/releases/download/v1.0/contatodb.sql "contatodb.sql")
* [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")
* [Código-Fonte](https://github.com/iagocolodetti/JavaDBExemplo/archive/v1.0.zip "v1.0.zip")
* [Arquivo de Script MySQL](https://github.com/iagocolodetti/JavaDBExemplo/releases/download/v1.1/contatodb.sql "contatodb.sql")
* [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")
* [Código-Fonte](https://github.com/iagocolodetti/JavaDBExemplo/archive/v1.1.zip "v1.1.zip")
<br/>
<h3>Conectando o Banco de Dados MySQL no NetBeans</h3>

Expand Down
37 changes: 2 additions & 35 deletions src/ContatoDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,6 @@ public List<Contato> getContatos() throws ContatoNaoExisteException, SQLExceptio
// <editor-fold defaultstate="collapsed" desc="Comando(s) de Atualização/Alteração (UPDATE)">
public void update(Contato contato) throws ContatoNaoExisteException, SQLException {

if (!contatoExiste(contato.getId())) {
throw new ContatoNaoExisteException("Não existe contato com esse ID.");
}

PreparedStatement ps = null;

try {
Expand All @@ -199,7 +195,7 @@ public void update(Contato contato) throws ContatoNaoExisteException, SQLExcepti
ps.setString(3, contato.getEmail());
ps.setInt(4, contato.getId());

ps.executeUpdate();
if (ps.executeUpdate() == 0) throw new ContatoNaoExisteException("Não existe contato com esse ID.");
} catch (SQLException e) {
throw new SQLException("Não foi possível alterar/atualizar o contato.");
} finally {
Expand All @@ -211,17 +207,13 @@ public void update(Contato contato) throws ContatoNaoExisteException, SQLExcepti
// <editor-fold defaultstate="collapsed" desc="Comando(s) de Exclusão/Remoção (DELETE)">
public void delete(int id) throws ContatoNaoExisteException, SQLException {

if (!contatoExiste(id)) {
throw new ContatoNaoExisteException();
}

PreparedStatement ps = null;

try {
ps = con.prepareStatement("DELETE FROM contato WHERE id = ?");
ps.setInt(1, id);

ps.executeUpdate();
if (ps.executeUpdate() == 0) throw new ContatoNaoExisteException();
} catch (SQLException e) {
throw new SQLException("Não foi possível excluir/remover o contato.");
} finally {
Expand All @@ -230,31 +222,6 @@ public void delete(int id) throws ContatoNaoExisteException, SQLException {
}
// </editor-fold>

private boolean contatoExiste(int contato_id) {

PreparedStatement ps = null;
ResultSet rs = null;

boolean existe = false;

try {
ps = con.prepareStatement("SELECT id FROM contato WHERE id = ?");
ps.setInt(1, contato_id);

rs = ps.executeQuery();

if (rs.next()) {
existe = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionFactory.closeConnection(ps, rs);
}

return existe;
}

public void close() {
ConnectionFactory.closeConnection(con);
}
Expand Down

0 comments on commit e0ecc7d

Please sign in to comment.