无法从jTable中删除行

时间:2016-05-26 17:58:25

标签: java swing

这是我用于删除功能的代码,但它似乎没有删除我选择的任何行。我首先使用jTextField searchTxt搜索所需记录,然后删除匹配记录。匹配或搜索工作正常,其删除没有。

private void deleteSelectedRows(){
   try{
            if(searchCombo_User.getSelectedItem().toString().equals("User ID")){
                dlt = "DELETE from user where user_id = ?";
            }
            else if(searchCombo_User.getSelectedItem().toString().equals("Username")){
                dlt = "DELETE from user where username = ?";
            }

            pst = conn.prepareStatement(dlt);
            pst.setString(1, searchTxt.getText());
            ((DefaultTableModel)userTable.getModel()).removeRow(userTable.getSelectedRow());
            pst.execute();
    }
    catch (Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
}

private void deleteBttnActionPerformed(java.awt.event.ActionEvent evt) {                                           
       if(!searchTxt.getText().isEmpty()){   
            deleteSelectedRows();
            Update_table();
       }
       else{
        JOptionPane.showMessageDialog(null, "Please search for the record you wish to delete");
    } 
} 

0 个答案:

没有答案