更新表集列= a-b

时间:2016-11-11 08:20:03

标签: mysql sql

我的产品表有库存栏,我想更新库存的价值(产品可用数量订购)。我必须使用什么更新查询?使用以下查询获取错误

我应该如何编码这部分 - 设置stock = prod-qty ????

public boolean updateProductWhenInsertintoCart(Product p) {


        try
        {
            Connection con=DbConnection.getConnection();
            PreparedStatement ps=con.prepareStatement("select shopping_cart_final.quantity, products.stocks from shopping_cart_final,products where shopping_cart_final.itemName=products.itemName");
            //ps.setString(1,p.getItemName());
            ResultSet rs=ps.executeQuery();
            int qty=0;
            int prod=0;
            System.out.println(rs);
            while(rs.next())
            {
                System.out.println("in while loop");
                qty=rs.getInt("quantity");
                prod=rs.getInt("stocks");
                System.out.println("quantity "+qty);
                System.out.println("stocks "+prod);
                p.setDiff(prod-qty);
                int d = p.getDiff();
            }

            String sql="Update products set stocks=prod-qty where itemName=?";
            PreparedStatement ps1=con.prepareStatement(sql);

            ps1.setString(1, p.getItemName());
            //ps1.setInt(1,p.getStocks());


            int x=ps1.executeUpdate();
            if(x>0)
            {
            return true;    
            }
            else
            {
                return false;
            }
        }

    catch(Exception e)
    {
        e.printStackTrace();
    }

        // TODO Auto-generated method stub
        return false;
    }

enter image description here

0 个答案:

没有答案