-2

enter image description hereI tried to print the objects from hashSet collection. the console display only the last object(one object). when I used ArrayList with the same method, I'm able to print all the objects. i've used an iterator method in order to print the collection set, see the attached test.Example of test result

public Set<Coupon> getAllCoupouns() throws Exception {

    Coupon coupon = new Coupon();
    Set<Coupon> coupons = new HashSet<Coupon>();

    // Open a connection
    conn = DriverManager.getConnection(Utils.getDBUrl());
    // Define the Execute query
    java.sql.Statement stmt = null;

    try {
        stmt = conn.createStatement();
        // build The SQL query
        String sql = "SELECT * FROM COUPON";
        // Set the results from the database
        ResultSet resultSet = stmt.executeQuery(sql);
        // constructor the object, retrieve the attributes from the results
        while (resultSet.next()) {

            coupon.setId(resultSet.getLong(1));
            coupon.setTitle(resultSet.getString(2));
            coupon.setStartDate((Date) resultSet.getDate(3));
            coupon.setEndDate((Date) resultSet.getDate(4));
            coupon.setAmount(resultSet.getInt(5));
            CouponType type = CouponType.valueOf(resultSet.getString(6)); // Convert String to Enum
            coupon.setType(type);
            coupon.setMessage(resultSet.getString(7));
            coupon.setPrice(resultSet.getDouble(8));
            coupon.setImage(resultSet.getString(9));

            coupons.add(coupon);

        }

    } catch (SQLException e) {
        throw new Exception("Retriev all the coupons failed");
    } finally {
        // finally block used to close resources
        try {
            if (stmt != null)
                conn.close();
        } catch (SQLException se) {
            // do nothing
        }
        try {
            if (conn != null)
                conn.close();
        } catch (SQLException se) {
            se.printStackTrace();
        }

    }
    return coupons;
}
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
Evi Cohen
  • 27
  • 3

2 Answers2

0

As you are initialising Coupon from outside the while loop, it keeps adding same object every time and hence, overwriting results in only last result being displayed.

What you need to do is to instantiate Coupon from within the while loop, e.g.:

public Set<Coupon> getAllCoupouns() throws Exception {
    Set<Coupon> coupons = new HashSet<Coupon>();

    // Open a connection
    conn = DriverManager.getConnection(Utils.getDBUrl());
    // Define the Execute query
    java.sql.Statement stmt = null;

    try {
        stmt = conn.createStatement();
        // build The SQL query
        String sql = "SELECT * FROM COUPON";
        // Set the results from the database
        ResultSet resultSet = stmt.executeQuery(sql);
        // constructor the object, retrieve the attributes from the results
        while (resultSet.next()) {
            Coupon coupon = new Coupon();
            coupon.setId(resultSet.getLong(1));
            coupon.setTitle(resultSet.getString(2));
            coupon.setStartDate((Date) resultSet.getDate(3));
            coupon.setEndDate((Date) resultSet.getDate(4));
            coupon.setAmount(resultSet.getInt(5));
            CouponType type = CouponType.valueOf(resultSet.getString(6)); // Convert String to Enum
            coupon.setType(type);
            coupon.setMessage(resultSet.getString(7));
            coupon.setPrice(resultSet.getDouble(8));
            coupon.setImage(resultSet.getString(9));

            coupons.add(coupon);

        }

    } catch (SQLException e) {
        throw new Exception("Retriev all the coupons failed");
    } finally {
        // finally block used to close resources
        try {
            if (stmt != null)
                conn.close();
        } catch (SQLException se) {
            // do nothing
        }
        try {
            if (conn != null)
                conn.close();
        } catch (SQLException se) {
            se.printStackTrace();
        }

    }
    return coupons;
}
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
0

cupon is always the same object. You make only one object of class Cupon, so the set contains only one object (you add always the same object). You must make a new object in each iteration in the while loop.

Donat
  • 4,157
  • 3
  • 11
  • 26