如果库存为 0,则尝试将商品的可用性设置为 NO 且不可用(不能发行),但似乎该商品仍在发行

Trying to make item's availability to NO and not available(cant issue) if the stocks are 0 but it seems the item still getting issued

如果库存为 0,我正在尝试将商品的可用性设置为 NO 和不可用(无法发货),但商品似乎仍在发货。

这是物品的发行代码:

String sql = "insert into issueditem(Item_No,Item ID, Item Name, Issued Date, "
           + "Due Date,  Item Number, Stock) values (?,?,?,?,?,?,?)";

try {      
    connect()
    pst = conn.prepareStatement(sql);
    pst.setString(1, txtitemno.getText());
    pst.setString(2, itemid.getText());
    pst.setString(3, itemname.getText());
    pst.setString(4, txtidate.getText());
    pst.setString(5, txtredate.getText());
    pst.setString(6, itemnum.getText());
    pst.setString(7, stock.getText());

    pst.execute();

    JOptionPane.showMessageDialog(null, "Item issued");
    update();

} catch (Exception e) {
    JOptionPane.showMessageDialog(null, e);
} finally {
    try {
        rs.close();
        pst.close();
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }
}

如果库存为 0 到 NO,这是我更新库存和商品可用性的方法

public void update(){ 
    int st = Integer.parseInt(stock.getText()); 
    int q = 1;
    int sup = st - q;
    String s = String.valueOf(sup);
    stock.setText(s);
    try {
        int n = Integer.parseInt(s);
        if (n>=0){
            String val1= itemid.getText();
            String val2 = stock.getText();
            String val3=jt14.getText();
            String sql = "update storageitem set ItemID='"+val1+"', "
                    + "Stock='"+val2+"' where ItemID='"+val1+"'"
                    + "Available= 'NO' where Stock='"+val2+"'";
            pst = conn.prepareStatement(sql);
            pst.executeUpdate();
            JOptionPane.showMessageDialog(null, "Record Updated");
        }else{
            JOptionPane.showMessageDialog(null, "Item is not issued");
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }finally{
        try {
            rs.close();
            pst.close();
        } catch (Exception e) {
              JOptionPane.showMessageDialog(null, e);
        }
    }
}

你的 SQL 查询是错误的,所以如果我理解得很好,你想将可用性更改为 'no' 如果 ItemId = val1 并且如果 val2 = 0

public void update(){ 
    int st = Integer.parseInt(stock.getText()); 
    int q = 1;
    int sup = st - q;
    stock.setText(sup+"");
    try {
        String val1= itemid.getText();
        String val2 = stock.getText();
        String val3=jt14.getText();
        if (sup>=0 && val2.equals("0")){
            String sql =  "update storageitem set Available = 'NO',  Stock= '"+val2+"'"
            + " where ItemID='"+val1+"' ";
            pst = conn.prepareStatement(sql);
            pst.executeUpdate();
            JOptionPane.showMessageDialog(null, "Record Updated");
        }else{
            JOptionPane.showMessageDialog(null, "Item is not issued");
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }finally{
        try {
            rs.close();
            pst.close();
        } catch (Exception e) {
              JOptionPane.showMessageDialog(null, e);
        }
    }
}

您不需要将 sup 转换为字符串值。在 stock.setText() 中,您可以将 sup 的 int 值与空字符串 sup+""
连接起来 sup = st-qn 的值相同,因此无需将其转换为 String 再转换为 int,直接使用 sup (只是为了更好的性能)