コード例 #1
0
  /**
   * Add product to database -Admin code change-Aparna 08/23
   *
   * @throws SQLException
   */
  @Override
  public void addProduct(ProductModel product) throws SQLException {

    PreparedStatement statement = null;
    Connection connection = databaseFactory.getConnection();

    try {
      statement =
          connection.prepareStatement(
              "insert into product(ProductID,ProductName,Category,Price) values(?,?,?,?) ");
      statement.setLong(1, product.getProductId());
      statement.setString(2, product.getProductName());
      statement.setString(3, product.getCategory().toString());
      statement.setDouble(4, product.getProductPrice());

      int rs = statement.executeUpdate();

    } catch (SQLException e) {
      e.printStackTrace();
      throw e;
    } finally {
      DBUtils.closeStatement(statement);
      databaseFactory.closeConnection();
    }
  }
コード例 #2
0
  @Override
  public void updateProduct(ProductModel productModel, long productId) throws SQLException {
    PreparedStatement statement = null;
    Connection connection = databaseFactory.getConnection();

    try {
      statement =
          connection.prepareStatement(
              "update product set ProductName = ?,Category = ?,Price = ? where ProductID = ?");

      statement.setString(1, productModel.getProductName());
      statement.setString(2, productModel.getCategory().toString());
      statement.setDouble(3, productModel.getProductPrice());
      statement.setLong(4, productId);

      int rs = statement.executeUpdate();

    } catch (SQLException e) {
      e.printStackTrace();
      throw e;
    } finally {
      DBUtils.closeStatement(statement);
      databaseFactory.closeConnection();
    }
  }
コード例 #3
0
  /**
   * Test case to display all products from for a given VM ID
   * @throws SQLException
   * @throws EmptyResultException
   */
  @Test
  public void testInsertProduct() throws SQLException {

    ProductModel productModel = new ProductModel();
    productModel.setCategory(ProductCategory.SNACK);
    productModel.setProductId(500);
    productModel.setProductName("Kettle Pop");
    productModel.setProductPrice(5.99);

    productDao.addProduct(productModel);
  }