我有一个 jframe 形式的销售表,用户一次添加一行。
我想通过减少表中的 qty 列来减少 MySQL 表 saleifo 中的 instock。
保存和打印按钮的代码段;
DefaultTableModel model = (DefaultTableModel) tableSale.getModel();
if(model.getRowCount()==0){
JOptionPane.showMessageDialog(null, "You have nothing to Print or Save ");
}else{
int save = JOptionPane.showConfirmDialog(null, "Do you really want to Save the Invoice Data ?","Save Confirmation",JOptionPane.YES_NO_OPTION);
if((save)==0){
try{
String saledate = ((JTextField)dayChooser.getDateEditor().getUiComponent()).getText();
String invoice = InvoiceNo_txt.getText();
String citems = countitems_txt.getText();
String tDis =totalDiscount_txt.getText();
String ntotal = NetTotal_txt.getText();
//setting data to saleinfo db table
try{
String sql = "Insert into saleinfo (SaleDate,InvoiceNo,TotalItems,TotalDiscount,NetTotal)values (?,?,?,?,?)";
pst=conn.prepareStatement(sql);
pst.setString(1, saledate);
pst.setString(2, invoice);
pst.setString(3, citems);
pst.setString(4, tDis);
pst.setString(5, ntotal);
pst.execute();
}catch(Exception e){
}
//redusing stock in db
int rcount = tableSale.getRowCount();
String idsale = (String) tableSale.getModel().getValueAt(rcount, 0);
String sql0= "select * from druginfo where ItemID=?";
pst0=conn.prepareStatement(sql0);
pst0.setString(1, idsale);
rs0= pst0.executeQuery();
if(rs0.next()){
String instock = rs0.getString("InStock");
int nowstock=Integer.parseInt(instock);
int soldqty = (int) tableSale.getModel().getValueAt(rcount, 3);
int newstock = nowstock - soldqty;
System.out.println("new :"+newstock);
String sqlupdate= "update druginfo set InStock='"+newstock+"' where ItemID='"+idsale+"'";
pst=conn.prepareStatement(sqlupdate);
pst.execute();
System.out.println("Done");
}
但是显示一个ArrayIndexOutOfBoundsException
。
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
at java.util.Vector.elementAt(Vector.java:474) at java.util.Vector.elementAt(Vector.java:474)
at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:648)
at com.bit.project.Newsale.saveprint_btnActionPerformed(Newsale.java:1039)
at com.bit.project.Newsale.access$1300(Newsale.java:57)
at com.bit.project.Newsale$16.actionPerformed(Newsale.java:683)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
第 1039 行是 String idsale = (String) tableSale.getModel().getValueAt(rcount, 0);
。
如何解决这个问题?
最佳答案
在第 1037 行,您将表中的可见行数分配给 int rcount,在本例中为 2。但是在第 1039 行,您使用此值尝试获取“第 2 行,第 0 列”的值来自表模型。
由于行和列的索引都是从0开始的,而且只有0和1两行,难怪求第2行会出问题!
如果您的目的是获取表格第一列和最后一行的单元格的值,那么您需要将第 1039 行更改为:
String idsale = (String) tableSale.getModel().getValueAt(rcount-1, 0);
当执行到达这一点时,您已经在检查 rcount
> 0,因此这不会造成麻烦。除了...
值得注意的是,如果表格附加了一个 RowSorter,那么可见 行的数量(tableSale.getRowCount()
返回的 sa 可能会更少比底层表模型中的实际行数(由tableSale.getModel().getRowCount()
返回),最后可见行的行号可能是在表格模型中完全不同。您可以使用 tableSale.convertRowIndexToModel()
将可见行号转换为模型行号。这将使第 1039 行:
String idsale = (String) tableSale.getModel().getValueAt(
tableSale.convertRowIndexToModel(rcount - 1), 0);
在您的代码中,您首先检查模型中是否有任何行。但是在第 1037 行,您读取了可见行数。如果有一个带过滤器的 RowSorter,那么即使模型中有很多行,它也可能返回 0 值。这也会在这里引起麻烦。
关于java - 尝试在 Java 中更新 MySQL 数据库时数组索引越界异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32383273/