Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.6 kB
1
Indexable
Never
public void searchProduct(){
        String selectQuery = "SELECT * FROM jwrms.stockandinventory WHERE productCode = ?";
        String updateQuery = "UPDATE jwrms.stockandinventory SET itemsInStock = itemsInStock-? WHERE productCode = ?";
        try{

            var product = (StockAndInventory) objIs.readObject();
            int amount = (int) objIs.readObject();
            PreparedStatement ps = dBConn.prepareStatement(updateQuery);
            ps.setString(2,product.getProductCode());
            ps.setInt(1,amount);
            ps.executeUpdate();
            ps = dBConn.prepareStatement(selectQuery);
            ps.setString(1, product.getProductCode());
            var result = ps.executeQuery();
                if(result.next()){
                    product.setProductCode(result.getString(1));
                    product.setProductName(result.getString(2));
                    product.setShortDescription(result.getString(3));
                    product.setLongDescription(result.getString(4));
                    product.setItemsInStock(result.getInt(5));
                    product.setUnitPrice(result.getFloat(6));
                    objOs.writeObject(true);
                    objOs.writeObject(product);
                    objOs.flush();
                    logger.info("Product has been found and updated");
                    return;
                }
            objOs.writeObject(false);
        }catch(Exception e){
            logger.error("Exception:" +e.getMessage());
            e.printStackTrace();
        }
    }