in this code it just delete row from JTable
and i want to delete from DB and when run this it delete from JTable
and apper error : java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
but not delete from DB how to solve this ?
在这段代码中,它只是从JTable中删除行,我想从DB中删除,并且在运行时从JTable和apper错误中删除:java.lang.ArrayIndexOutOfBoundsException:2> = 2但是没有从DB中删除如何解决这个问题?
private final JPanel panel_09 = new JPanel();
JScrollPane scrollPane_09 = new JScrollPane();
final DefaultTableModel TableModel09 = new DefaultTableModel(new String[]{"Picture", "Item", "Price", "After Discount"}, 0);
final JTable table_09 = new JTable(TableModel09);
JButton btnNewButton_09 = new JButton("Delete Item");
btnNewButton_09.addActionListener ( new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
int selectedPlanet = table_09.getSelectedRow();
TableModel09.removeRow((int) selectedPlanet);
Object desc = table_09.getModel().getValueAt(selectedPlanet, 4);
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/login", "root", "123");
String query = "delete from flyer_item where discount=desc";
java.sql.PreparedStatement ps = conn.prepareStatement(query);
ps.setString(1, (String) desc);
ps.executeUpdate();
} catch (Exception ex) {
System.err.println(ex);
}
}
}
);
1 个解决方案
#1
2
You removed your Object from your model before you get the desc
:
在获得desc之前,您已从模型中删除了Object:
int selectedPlanet = table_09.getSelectedRow();//<<--this will equal 2
TableModel09.removeRow((int) selectedPlanet);//<<----remove 2
So now
Object desc = table_09.getModel().getValueAt(selectedPlanet, 4);
//when you try to `getValueAt(2,4)` you get this error
//"java.lang.ArrayIndexOutOfBoundsException: 2 >= 2"
To avoid this problem, you have tp get your Object before you remove the row :
要避免此问题,请在删除行之前获取对象:
int selectedPlanet = table_09.getSelectedRow();
Object desc = table_09.getModel().getValueAt(selectedPlanet, 4);
TableModel09.removeRow((int) selectedPlanet);
Then, you will get another error of SQL, because to set Parameters to PreparedStatement you have to use ?
:
然后,您将得到另一个SQL错误,因为要将参数设置为PreparedStatement,您必须使用? :
String query="delete from flyer_item where discount = ?";
//----------------------------------------------------^
java.sql.PreparedStatement ps = conn.prepareStatement(query);
ps.setString(1,(String) desc);
#1
2
You removed your Object from your model before you get the desc
:
在获得desc之前,您已从模型中删除了Object:
int selectedPlanet = table_09.getSelectedRow();//<<--this will equal 2
TableModel09.removeRow((int) selectedPlanet);//<<----remove 2
So now
Object desc = table_09.getModel().getValueAt(selectedPlanet, 4);
//when you try to `getValueAt(2,4)` you get this error
//"java.lang.ArrayIndexOutOfBoundsException: 2 >= 2"
To avoid this problem, you have tp get your Object before you remove the row :
要避免此问题,请在删除行之前获取对象:
int selectedPlanet = table_09.getSelectedRow();
Object desc = table_09.getModel().getValueAt(selectedPlanet, 4);
TableModel09.removeRow((int) selectedPlanet);
Then, you will get another error of SQL, because to set Parameters to PreparedStatement you have to use ?
:
然后,您将得到另一个SQL错误,因为要将参数设置为PreparedStatement,您必须使用? :
String query="delete from flyer_item where discount = ?";
//----------------------------------------------------^
java.sql.PreparedStatement ps = conn.prepareStatement(query);
ps.setString(1,(String) desc);