在Java销售后减少MySQL数据库中的库存数量

时间:2015-08-22 10:38:58

标签: java mysql

以下代码是我在销售后使用新剩余库存数量更新数据库的方法。

在我的表单中, tablesale 包含动态行。意味着用户可以随意添加1个或更多。

所以我使用循环获取当前表行并从tablesale获取相关的ItemID并在db中搜索它,最后减少售出的数量,结果更新到数据库。

我的代码;

search: '=?filter'

但是代码会抛出异常,如下所示;

 <ion-search class="search-wrapper-light" placeholder="Search..." min-length="1" model="results" source="getExamsByName(str)" filter="search"></ion-search>

第1009行是search。帮我解决这个错误。

enter image description here

2 个答案:

答案 0 :(得分:1)

替换此行

for(int rcount=0;rcount<=tableSale.getRowCount();rcount++){

用这个

for(int rcount=0;rcount<tableSale.getRowCount();rcount++){

索引从0开始计数,如果行数为1,则最大索引将为0.它将在索引1上崩溃,这将解决问题。

好的,上面的问题是有效的,另一件事是

rcount = tableSale.getRowCount();

rcount是你的循环变量。为什么要为其分配表行数。行计数始终是最大索引+ 1 t将始终导致索引超出范围。删除此行,然后检查。

答案 1 :(得分:0)

    for(int rcount=0;rcount<tblProduct.getRowCount();rcount++){
        String idsale = (String) tblProduct.getModel().getValueAt(rcount, 0);

        String sql0= "select * from productsinformation where ProductID=?";

        stm=conn.prepareStatement(sql0);
        stm.setString(1, idsale);

        ResultSet rs= stm.executeQuery();

        if(rs.next()){
            String instock = rs.getString("Stock");
            int nowstock=Integer.parseInt(instock);
            int newstock = nowstock - 1;
            String sqlupdate= "update productsinformation set Stock='"+newstock+"' where ProductID='"+idsale+"'";
            stm=conn.prepareStatement(sqlupdate);
            stm.execute();


        }
    }