Skip to content

Commit

Permalink
fix: Potential database resource leak
Browse files Browse the repository at this point in the history
Co-authored-by: AlissonGSantos <alisson.gabriel@ufpr.br>
Co-authored-by: Pedro-H108 <pedro.souza.1008@gmail.com>
Co-authored-by: raulbana <raul.ferreira@ufpr.br>
  • Loading branch information
4 people committed Nov 17, 2024
1 parent 3b57509 commit bc29c8a
Showing 1 changed file with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,30 @@ public void removeConta(Cliente cliente) throws Exception {
public void create(Cliente cliente) throws Exception {
try (Connection con = ConnectionFactory.getConnection()) {
con.setAutoCommit(false);
PreparedStatement sql = con.prepareStatement("INSERT INTO endereco (logradouro, bairro, numero, cidade) VALUES (?,?,?,?);", Statement.RETURN_GENERATED_KEYS);
sql.setString(1, cliente.getEndereco().getLogradouro());
sql.setString(2, cliente.getEndereco().getBairro());
sql.setInt(3, cliente.getEndereco().getNumero());
sql.setString(4, cliente.getEndereco().getCidade());
sql.executeUpdate();
ResultSet rs = sql.getGeneratedKeys();
if (rs.next()) {
cliente.getEndereco().setId(rs.getInt(1));
} else {
throw new RuntimeException("Erro na criação de endereço");

try (PreparedStatement sql = con.prepareStatement("INSERT INTO endereco (logradouro, bairro, numero, cidade) VALUES (?,?,?,?);", Statement.RETURN_GENERATED_KEYS)) {
sql.setString(1, cliente.getEndereco().getLogradouro());
sql.setString(2, cliente.getEndereco().getBairro());
sql.setInt(3, cliente.getEndereco().getNumero());
sql.setString(4, cliente.getEndereco().getCidade());
sql.executeUpdate();
ResultSet rs = sql.getGeneratedKeys();
if (rs.next()) {
cliente.getEndereco().setId(rs.getInt(1));
} else {
throw new RuntimeException("Erro na criação de endereço");
}
}
sql = con.prepareStatement("INSERT INTO cliente (cpf, nome, sobrenome, rg, idEndereco) VALUES (?,?,?,?,?);");
sql.setString(1, cliente.getCpf());
sql.setString(2, cliente.getNome());
sql.setString(3, cliente.getSobrenome());
sql.setString(4, cliente.getRg());
sql.setInt(5, cliente.getEndereco().getId());
sql.executeUpdate();

try (PreparedStatement sql = con.prepareStatement("INSERT INTO cliente (cpf, nome, sobrenome, rg, idEndereco) VALUES (?,?,?,?,?);");) {
sql.setString(1, cliente.getCpf());
sql.setString(2, cliente.getNome());
sql.setString(3, cliente.getSobrenome());
sql.setString(4, cliente.getRg());
sql.setInt(5, cliente.getEndereco().getId());
sql.executeUpdate();
}

con.commit();
}
}
Expand Down

0 comments on commit bc29c8a

Please sign in to comment.