删除查询无法正常工作

时间:2014-12-23 20:33:21

标签: java sql

DELETE查询没有正常工作,该行已从JTable中删除但仍可在数据库中使用,当我刷新JTable时,它会再次在其中返回。

  DefaultTableModel m=(DefaultTableModel)tbl.getModel();
    if (tbl.getSelectedRow()==-1){
        if(tbl.getRowCount()==0)
            JOptionPane.showMessageDialog(null, "Table is Empty");
        else
            JOptionPane.showMessageDialog(null,"You Must Select a Row");
        }
    else{
        int rowIndex = tbl.getSelectedRow();

        int n = JOptionPane.showConfirmDialog(null,"Are you sure you want to permanently delete "
                + MainFrame.pArray.get(rowIndex).name+" information ?","Delete Record",JOptionPane.YES_NO_OPTION);
    if(n == JOptionPane.YES_OPTION){

     String id = MainFrame.pArray.get(rowIndex).id;

        String sql = "delete from patient where id =?";
    try {
        connect();
        if( conn != null){
        PreparedStatement sttmnt = (PreparedStatement) conn.prepareStatement(sql);
        sttmnt.setInt(1,Integer.parseInt(id));
        sttmnt.executeUpdate();
        m.removeRow(tbl.getSelectedRow());
        MainFrame.pArray.remove(rowIndex);
        JOptionPane.showMessageDialog(null, "Done");
        conn.close();
        }
    }       catch (ClassNotFoundException ex) {
                Logger.getLogger(Table.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(Table.class.getName()).log(Level.SEVERE, null, ex);
            }
    }
    }

1 个答案:

答案 0 :(得分:1)

尝试在关闭连接之前提交事务。这样做:

conn.commit()

之前

conn.close()