Deletion involving Java and SQL

2

The program I'm doing needs to delete users from the database!

In the code I determine:

Menu:

static private void Excluir(Connection con) throws SQLException
{
    String cpf;
    Scanner s = new Scanner(System.in);
    Cliente cli = new Cliente();

    System.out.println("Informe o CPF a ser Excluido:");
    cli.cpf = s.next();
    System.out.println();
    cli.ExcluirPess(cli, con);
}

In the client class I determine:

public void ExcluirPess(Cliente cli, Connection con) throws SQLException {
      String sql = "delete from Cliente where CPF_cliente = ?"; 
      PreparedStatement stmt = con.prepareStatement(sql);  
      stmt.setString(1, cli.getCpf());
      stmt.executeUpdate();
      stmt.close();
}   

I cleaned up as they demonstrated, but exclusion is not yet done! Does the fact that the PK CPF_cliente in% FK Endereco Telefone , interfere with deletion?     

asked by anonymous 26.05.2014 / 14:28

1 answer

2

You should add the parameter and then run your PreparedStatement.

String sql = "delete from Cliente where CPF_cliente = ?"; 
PreparedStatement stmt = con.prepareStatement(sql);  
stmt.setString(1, cli.cpf);
stmt.executeUpdate();
  

Does the fact that in the bank the PK CPF_customer be FK in Address and Phone interfere with the deletion?

For sure, the database does not allow you to do such an exclusion so as not to generate an inconsistency in the database, otherwise the Address and Phone tables would have a foreign key that does not exist in the Client table.

To fix, first delete the records in the tables that have the foreign key and in the last delete the record in the table Customer.

    
26.05.2014 / 14:37