I have an issue with a boolean statement while trying to use a custom exception. when using my code to check if a coupons exists in the db, it returns true when it exists. however, when coupon does not exist it throws me an sql exception: "Illegal operation on empty result set." instead of my custom one.
My boolean Method:
@Override
public boolean isCouponExist(int couponID) throws SQLException {
Connection connection = pool.getConnection();
try {
PreparedStatement statement = connection.prepareStatement(CHECK_EXISTS);
statement.setInt(1, couponID);
ResultSet rs = statement.executeQuery();
return rs.next();
} finally {
pool.restoreConnection(connection);
}
}
Method to get One Coupon:
@Override
public Coupon getOneCoupon(int couponID) throws SQLException {
Connection connection = pool.getConnection();
Coupon result = null;
try {
PreparedStatement statement = connection.prepareStatement(GET_ONE_COUPON);
statement.setInt(1, couponID);
ResultSet resultSet = statement.executeQuery();
resultSet.next();
result = new Coupon(resultSet.getInt(1), resultSet.getInt(2), Category.categoryFor(resultSet.getInt(3)),
resultSet.getString(4), resultSet.getString(5), resultSet.getDate(6), resultSet.getDate(7),
resultSet.getInt(8), resultSet.getDouble(9), resultSet.getString(10));
} finally {
pool.restoreConnection(connection);
}
return result;
}
Method used in the facade for validation:
public void purchaseCoupon(Coupon coupon) throws CouponExceptions, SQLException {
if(!coup.isCouponExist(coupon.getId())) {
throw new CouponExceptions("ERROR: Coupon " + coupon.getId() + " does not exist.");
}
}
Running the code:
CustomerFacade customer = (CustomerFacade) login.login("rotemb@gmail.com", "1378", ClientType.CUSTOMER);
Coupon p1 = coupDAO.getOneCoupon(55);
customer.purchaseCoupon(p1);
as you can see I gave it a non-existing ID number in order to get an exception.
StackTrace:
Exception in thread "main" java.sql.SQLException: Illegal operation on empty result set.
at mysql.connector.java@8.0.18/com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at mysql.connector.java@8.0.18/com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at mysql.connector.java@8.0.18/com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at mysql.connector.java@8.0.18/com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at mysql.connector.java@8.0.18/com.mysql.cj.jdbc.result.ResultSetImpl.checkRowPos(ResultSetImpl.java:484)
at mysql.connector.java@8.0.18/com.mysql.cj.jdbc.result.ResultSetImpl.getObject(ResultSetImpl.java:1283)
at mysql.connector.java@8.0.18/com.mysql.cj.jdbc.result.ResultSetImpl.getInt(ResultSetImpl.java:786)
at coupon.dbdao.CouponsDBDAO.getOneCoupon(CouponsDBDAO.java:155)
at coupon.Program.main(Program.java:49)
when trying to give a legal coupon ID I have no issues. when trying to give an illegal coupon ID instead of my custom exception I get SQLException: "Illegal operation on empty result set."
Still new to this.