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);
}
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);
}
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...
O que eu faço?