使用javagui从数据库中删除一行

blmhpbnm  于 2021-08-09  发布在  Java
关注(0)|答案(0)|浏览(180)

我在从数据库中删除行时遇到问题。当我从模型中获取所选行并将其传递给数据库函数时,它会给我nullpointerexception。下面是gui的代码

// getting the selected row
                int selectedRow = table.getSelectedRow();

                //converting the row index
                selectedRow = table.convertRowIndexToModel(selectedRow);

                // getting values of the selected row
                String id = (String)table.getModel().getValueAt(selectedRow, 0);

                db.DeleteBooking(id);

                model.removeRow(selectedRow);

                JOptionPane.showMessageDialog(frame, "Booking has successfully Cancelled");

这是传递值的db函数。

public void DeleteBooking(String x) throws SQLException
    {
        // openConnection is working successfully as my other queries are working.
     openConnection();
    PreparedStatement ps1=null;

    String query1="DELETE FROM booking WHERE id = ?";

        ps1 = con.prepareStatement(query1);

    int y = Integer.valueOf(x);
    ps1.setInt(1, y);

    int row=ps1.executeUpdate();

    if(con!=null)
    {
        con.close();
    }

    }

谁能帮我诊断一下这个问题吗。先谢谢你

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题