我如何删除jtable以及数据库中的单个行。我已经查看了其他网站和其他来源,但我无法弄清楚我的错误

时间:2019-02-13 11:45:52

标签: netbeans

private void deleteActionPerformed(java.awt.event.ActionEvent evt) {                                       
    // TODO add your handling code here:
    try{
        Connection con=null;
        Statement st;
        ResultSet rs; 
        con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mall","root","");
        st=con.createStatement();
        DefaultTableModel model = (DefaultTableModel)speedster.getModel();
        int SelectedRowIndex = speedster.getSelectedRow();
        //model.removeRow(SelectedRowIndex);
        Object o = speedster.getValueAt(SelectedRowIndex,0);
        String query1 = "delete from app where uid="+o+"";
        while(model.getRowCount()>0)
        {
            rs = st.executeQuery("select * from app");
            if(rs.getString("UID")==o)
            {
                //model.removeRow(SelectedRowIndex);
                //String query1 = "delete from app where uid='"+o+"'";
                st.executeQuery(query1);
            }
        }
    } 
    catch(SQLException ex)
    {
        JOptionPane.showMessageDialog(this,"Cannot delete the selected item.");
    }
}

0 个答案:

没有答案