Пример #1
0
  // Removing coupon from DB
  @Override
  public void removeCoupon(Coupon c) throws CoupSQLException {
    long id = 0;
    if (c.getId() == 0) {
      id = getCouponID(c);
    } else {
      id = c.getId();
    }

    if (id == 0) {

      throw new CoupSQLException("Coupon not found");
    }

    String sql = "DELETE FROM CouponsL WHERE id = " + id;

    Connection conn = ConnectionPool.getInstance().getConnection();

    try {

      PreparedStatement prpst2 = conn.prepareStatement("SELECT * FROM CouponsL WHERE id=" + id);
      ResultSet rs = prpst2.executeQuery();
      while (rs.next()) {
        long idnum = rs.getLong("id");
        String s = rs.getString("Title");

        c.setId(idnum);
        System.out.print("Removing: " + idnum + ", ");
        System.out.println(s);

        PreparedStatement prpst = conn.prepareStatement(sql);
        prpst.executeUpdate();
      }

    } catch (SQLException e) {
      throw new CoupSQLException("Could not remove coupon. Please try again. ", e);
    }
  }
Пример #2
0
  // getting all coupons as an array from the DB
  @Override
  public Collection<Coupon> getAllCoupons() throws CoupSQLException {

    Collection<Coupon> coupList = new ArrayList<>();
    Connection conn = ConnectionPool.getInstance().getConnection();

    try {
      PreparedStatement prpst2 = conn.prepareStatement("SELECT * FROM CouponsL");
      ResultSet rs = prpst2.executeQuery();

      while (rs.next()) {
        long id = rs.getLong(1);
        String title = rs.getString(2);
        String message = rs.getString(3);
        String image = rs.getString(4);
        int amount = rs.getInt(5);
        java.sql.Date startDate = rs.getDate(6);
        java.sql.Date endDate = rs.getDate(7);
        double price = rs.getDouble(8);
        String type = rs.getString(9);
        Coupon c = new Coupon();
        c.setId(id);
        c.setTitle(title);
        c.setMessage(message);
        c.setImage(image);
        c.setAmount(amount);
        c.setPrice(price);
        c.setStartDate(startDate);
        c.setEndDate(endDate);
        c.setType(enumCheck(type));
        coupList.add(c);

        System.out.print(c.getId() + ", ");
        System.out.print(c.getTitle() + ", ");
        System.out.print(c.getMessage() + ", ");
        System.out.print(c.getImage() + ", ");
        System.out.print(c.getAmount() + ", ");
        System.out.print(c.getPrice() + ", ");
        System.out.print(startDate + ", ");
        System.out.print(endDate + ", ");
        System.out.println(type + ", ");
      }

    } catch (SQLException e) {
      throw new CoupSQLException("Couldn't get all coupons. ", e);
    }
    return coupList;
  }
Пример #3
0
  // Update coupon in database
  @Override
  public void updateCoupon(Coupon c) throws CoupSQLException {

    long id = c.getId();
    System.out.println();
    String title = c.getTitle();
    String message = c.getMessage();
    String image = c.getImage();
    int amount = c.getAmount();
    java.sql.Date startDate = c.getStartDate();
    java.sql.Date endDate = c.getEndDate();
    double price = c.getPrice();
    System.out.println(title + " " + id);
    String type = null;
    if (c.getType() != null) {
      type = c.getType().name();
    }
    String sql =
        "UPDATE CouponsL SET Title = ? , Message = ? , Image = ? ,"
            + " Amount = ? , Price = ?, startDate = ? , endDate = ?, Type = ?";

    Connection conn = ConnectionPool.getInstance().getConnection();

    try {
      PreparedStatement prpst = conn.prepareStatement(sql + " WHERE id  = " + id);
      if (id == 0) {
        throw new CoupSQLException("Couldn't find the coupon.Please try again");
      }
      prpst.setString(1, title);
      prpst.setString(2, message);
      prpst.setString(3, image);
      prpst.setInt(4, amount);
      prpst.setDouble(5, price);
      prpst.setDate(6, startDate);
      prpst.setDate(7, endDate);
      prpst.setString(8, type);

      prpst.executeUpdate();

    } catch (SQLException e) {
      throw new CoupSQLException(
          "Could not update coupon.Customer can purchase only 1  of the coupon. ", e);
    }
  }