尝试在Java中更新MySQL数据库时出现数组索引越界异常
我有一个jframe形式的销售表,用户一次添加一行 我想通过减少表中的qty列来减少MySQL表saleifo中的instock 保存和打印按钮的代码段尝试在Java中更新MySQL数据库时出现数组索引越界异常,java,mysql,swing,Java,Mysql,Swing,我有一个jframe形式的销售表,用户一次添加一行 我想通过减少表中的qty列来减少MySQL表saleifo中的instock 保存和打印按钮的代码段 DefaultTableModel model = (DefaultTableModel) tableSale.getModel(); if(model.getRowCount()==0){ JOptionPane.showMessageDialog(null, "You have nothing to Print or
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");
}
但显示一个数组索引OutofBoundsException
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行是stringidsale=(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,因此这不会造成问题。除了
值得注意的是,如果表附加了一个行排序器,那么可见的行数(由tableSale.getRowCount()
返回的sa很可能小于底层表模型中的实际行数(由tableSale.getModel().getRowCount()返回)
,表模型中最后一个可见行的行号可能会有很大不同。您可以使用tableSale.convertRowIndexToModel()
将可见行号转换为模型行号。这将使第1039行:
String idsale = (String) tableSale.getModel().getValueAt(
tableSale.convertRowIndexToModel(rcount - 1), 0);
在代码中,您首先检查模型中是否有行。但在第1037行,您读取了可见行数。如果有带筛选器的行排序器,则即使模型中有很多行,也可能返回值0。这也会在此处造成问题。表中有2行(rcount==2
),所以getValueAt(索引,0)
对索引0
和1
有效,但对传递的2
无效。JDBC建议:在创建连接时提前准备好所有语句。每次单击按钮时创建准备好的语句没有意义。另外,准备好的更新语句也应该与一起使用代码>占位符,而不是通过连接字符串。