Excluir

5 respostas
fabiodurgante
seguinte tenho uma classe ClienteDao e o metodo 

        public void excluir(Cliente cli) {
        String sql = "DELETE FROM cliente WHERE id_cliente = ?";
        try {
            PreparedStatement stmt = getConexao().prepareStatement(sql);
            stmt.setInt(1, cli.getid_Cliente() );
            stmt.executeUpdate();
        } catch (SQLException sQLException) {
            System.out.println("Erro ao excluir Cliente.");
        }
    }
ainda no ClienteDao

    public Cliente Consultar(Integer id) {
        String sql = "SELECT * FROM cliente WHERE id = ?";
        try {
            PreparedStatement stmt = getConexao().prepareStatement(sql);
            stmt.setInt(1, id);
            ResultSet rs = stmt.executeQuery();
            List<Cliente> lista = getCliente(rs);
            if(lista.size() > 0)
                return lista.get(0);
        } catch (SQLException sQLException) {
            System.out.println("Erro ao consultar Cliente");
        }
        return null;        
    } 


na consulta tenho o seguinte

private void jExcluirActionPerformed(java.awt.event.ActionEvent evt) {                                         
Cliente cli = getSelecionado();
    ClienteDao cliente = new ClienteDao();   
    if(cli != null)
        cliente.excluir(cli);
    jExcluirActionPerformed(evt);
} 

no action do botao excluir 

aqui eu pego a linha do jtable selecionada para a exclusao

   private Cliente getSelecionado() {
    if(tabela.getSelectedRow() == -1) {
        JOptionPane.showMessageDialog(this, "Selecione uma linha da tabela.",
                "", JOptionPane.ERROR_MESSAGE);
        return null;
    }
    DefaultTableModel modelo = (DefaultTableModel) tabela.getModel();
    Integer id = Integer.parseInt(modelo.getValueAt(tabela.getSelectedRow(), 0).toString());
    System.out.println(id);
    
    ClienteDao cliente = new ClienteDao();
    return cliente.Consultar(id);
} 

testei e ela ta pegando o codigo certinho quando clico em cima de alguma linha do jtable
quando clico em excluir ele fica rodando e aparece a mesagem Erro ao consultar Cliente
e nao para nunca de roda o programa se alguem ajudar

5 Respostas

juliano_FRG

Aparece algum erro ou exceção no console? Colaca pra gente ver, pois se sempre aparece a mensagem de “erro ao excluir…” é porque está tendo um SQLException.

Balena

Isso mesmo melhor ver como está o console

brunocl14

checa o console e da uns logs pra checar o valor das variaveis.

fabiodurgante
public void excluir(Cliente cli) {

String sql = DELETE FROM cliente WHERE id_cliente = ?;

try {

PreparedStatement stmt = getConexao().prepareStatement(sql);

stmt.setInt(1, cli.getid_Cliente() );

stmt.executeUpdate();

} catch (SQLException sQLException) {

System.out.println(Erro ao excluir Cliente.);

}

}

WHERE tava id e na minha tabela é id_cliente ai que dava bug valeu ai a todos falowwww

brunocl14

valeu.

Criado 10 de dezembro de 2008
Ultima resposta 11 de dez. de 2008
Respostas 5
Participantes 4