我的问题:如何允许用户通过按钮从jtable执行多次删除。表格为
链接到我的数据库。我在另一个类中创建了一个表模型并链接到我的SalesReceiptTable。
当用户单击删除按钮时,我能够从db和table中删除一行。
我应该为rowSelected ??
//Button to perform delete
JButton btnDelete = new JButton("Delete");
btnDelete.setFont(new Font("Tahoma", Font.PLAIN, 13));
btnDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int rowSelected=SalesReceiptTable.getSelectedRow();
if(rowSelected>=0)
{
String id = SalesReceiptTable.getValueAt(rowSelected, 7).toString();
SalesReceipt1 e1=new SalesReceipt1(Integer.parseInt(id));
e1.deleteSalesReceipt();
ArrayList<SalesReceipt1> result= SalesReceipt1.retrieveAllSalesReceipt();
SalesReceiptTableModel model= new SalesReceiptTableModel(result);
SalesReceiptTable.setModel(model);
}
else{
JOptionPane.showMessageDialog(null,"No record selected","Alert", JOptionPane.ERROR_MESSAGE);
}
//My sql statement in my entity class
public boolean deleteSalesReceipt(){
boolean success = false;
DBController db = new DBController();
String dbQuery;
db.getConnection();
dbQuery = "DELETE FROM SalesReceipt WHERE salesReceiptId ='" + salesReceiptId +"'";
if (db.updateRequest(dbQuery) == 1){
success = true;
}
db.terminate();
return success;
}