Ejemplo n.º 1
0
  /*
   wyświetlenie zawartości lodówki w formie listy
  */
  public List<Fridge> getAllProducts(int userid) {
    List<Fridge> products = new ArrayList<Fridge>();

    try {
      PreparedStatement preparedStatement =
          connection.prepareStatement("select * from fridge where userid=?");
      preparedStatement.setInt(1, userid);
      ResultSet rs = preparedStatement.executeQuery();
      // ResultSet rs = preparedStatement.executeQuery();
      // Statement statement = connection.createStatement();
      // ResultSet rs = statement.executeQuery("select * from fridge where userid=?");
      while (rs.next()) {
        // wypełnianie listy
        Fridge product = new Fridge();
        product.setProductID(rs.getInt("ProductID"));
        product.setName(rs.getString("ProductName"));
        product.setAmount(rs.getInt("ProductAmount"));
        product.setUserID(rs.getInt("UserID"));
        products.add(product);
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }

    return products;
  }
Ejemplo n.º 2
0
  /*
  wyświetlenie jednego produktu na podstawie ID
  */
  public Fridge getProductById(int ProductID) {
    Fridge product = new Fridge();

    try {
      PreparedStatement preparedStatement =
          connection.prepareStatement("select * from fridge where ProductID=?");
      preparedStatement.setInt(1, ProductID);
      ResultSet rs = preparedStatement.executeQuery();

      if (rs.next()) {
        product.setProductID(rs.getInt("ProductID"));
        product.setName(rs.getString("ProductName"));
        product.setAmount(rs.getInt("ProductAmount"));
        product.setUserID(rs.getInt("UserID"));
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }

    return product;
  }