System.out.println("item quantity "+quantity); System.out.println("buy quantity "+qty);
  public void createBuyNow(int itemId, int userId, int qty) throws RemoteException  {    PreparedStatement stmt = null;    ResultSet rs           = null;    Connection conn        = null;    utx = messageDrivenContext.getUserTransaction();    try    {      utx.begin();      // Try to find the Item corresponding to the Item ID      String now = TimeManagement.currentDateToString();      int quantity;      try       {        conn = dataSource.getConnection();        stmt = conn.prepareStatement("SELECT quantity, end_date FROM items WHERE id=?");        stmt.setInt(1, itemId);        rs = stmt.executeQuery();      }      catch (SQLException e)      {        try { stmt.close(); } catch (Exception ignore) {}        try { conn.close(); } catch (Exception ignore) {}        throw new RemoteException("Failed to execute Query for the item: " +e+"<br>");      }      PreparedStatement update = null;      try      {          if (rs.first())        {          quantity = rs.getInt("quantity");          System.out.println("item quantity "+quantity);          System.out.println("buy quantity "+qty);          quantity = quantity -qty;          System.out.println("new item quantity "+quantity);          if (quantity == 0)          {            update = conn.prepareStatement("UPDATE items SET end_date=? AND quantity=? WHERE id=?");            update.setString(1, now);            update.setInt(2, quantity);            update.setInt(3, itemId);            update.executeUpdate();          System.out.println("update date "+now);          System.out.println("update quantity "+quantity);          System.out.println("update itemId "+itemId);          }          else          {            update = conn.prepareStatement("UPDATE items SET quantity=? WHERE id=?");            update.setInt(1, quantity);            update.setInt(2, itemId);            update.executeUpdate();          }          update.close();        }        stmt.close();      }      catch (Exception e)      {        try { update.close(); } catch (Exception ignore) {}        try { stmt.close(); } catch (Exception ignore) {}        try { conn.close(); } catch (Exception ignore) {}        throw new RemoteException("Failed to update item's quantity: " +e+"<br>");      }      try       {      System.out.println("5");        stmt = conn.prepareStatement("INSERT INTO buy_now VALUES (NULL, \""+userId+                                     "\", \""+itemId+"\", \""+qty+"\", \""+now+"\")");        stmt.executeUpdate();      }      catch (Exception e)      {        try { stmt.close(); } catch (Exception ignore) {}        try { conn.close(); } catch (Exception ignore) {}        throw new RemoteException("Failed to create buy_now item: " +e+"<br>");      }      if (stmt != null) stmt.close();      if (conn != null) conn.close();      utx.commit();    }     catch (Exception e)    {      try { stmt.close(); } catch (Exception ignore) {}      try { conn.close(); } catch (Exception ignore) {}      try      {        utx.rollback();        throw new RemoteException("Cannot insert the item into buy_now items table: " +e+"<br>");      }      catch (Exception se)       {        throw new RemoteException("Transaction rollback failed: " + e +"<br>");      }    }  }