Delete - MYSQL [RESOLVIDO]

0 respostas
pedroroxd

Pessoal, estou usando uma tabela, na qual tem um botão "Excluir".

Dentro dele tem isso:
DefaultTableModel dtm = (DefaultTableModel)jTable1.getModel();
        
    int k = jTable1.getSelectedRow();
    String id = (String) jTable1.getValueAt(k, 0);

    String nome = "refeições";

    ConectaBanco c = new ConectaBanco();
    Connection conec = null;

        try {
            conec = c.conecta();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(FrameCadastroRefeição.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(FrameCadastroRefeição.class.getName()).log(Level.SEVERE, null, ex);
        }
         
 ExcluiLinha exc = new ExcluiLinha();

        try {
            exc.exclui(conec, id, nome);
            if (jTable1.getSelectedRow() >= 0){
              dtm.removeRow(jTable1.getSelectedRow());
            }
           jTable1.setModel(dtm);
        } catch (SQLException ex) {
            Logger.getLogger(FrameCadastroRefeição.class.getName()).log(Level.SEVERE, null, ex);
        }
Ae eu eu chamei a class exclui linha...
public void exclui (Connection connection,String id, String nome) throws SQLException  {

Statement stmt = connection.createStatement();
 String sql = "Delete from "+nome+" where id = "+id+" ";
stmt.executeUpdate(sql);
    }
Só que ele não consegue executar esse comando:
String sql = "Delete from "+nome+" where id = "+id+" "; //Não deu certo.. ae eu tentei direto: 
String sql = "Delete from refeições where id = 1";  //Mas Não deu certo também...
Mas quando eu executo direto pelo sql o comando acima, dá certo...

O que eu faço?

Criado 5 de setembro de 2009
Respostas 0
Participantes 1