/* 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; }
/* uaktualnienie danych nt. produktu w lodówce */ public void updateFridge(Fridge fridge) { try { PreparedStatement preparedStatement = connection.prepareStatement( "update fridge set ProductName=?, ProductAmount=?" + " where ProductID=?"); preparedStatement.setString(1, fridge.getName()); preparedStatement.setInt(2, fridge.getAmount()); preparedStatement.setInt(3, fridge.getProductID()); preparedStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } }
/*dodawanie produktu do lodówki Atrybuty: nazwa składnika (varchar) - ProductName ilość (int) - ProductAmount Użytkownik wprowadza posiadane pokarmy wraz z ich ilością. */ public void addProduct(Fridge fridge) { try { PreparedStatement preparedStatement = connection.prepareStatement( "insert into fridge(ProductName,ProductAmount, UserID) values (?, ?, ? )"); preparedStatement.setString(1, fridge.getName()); preparedStatement.setInt(2, fridge.getAmount()); preparedStatement.setInt(3, fridge.getUserID()); preparedStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } }
/* 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; }