Reputation: 43
I'm almost sure that there is a very simple explanation why my codes are not behaving as I thought they would but basically my problem is that the resultSet still go through the code block that it should not go so it comes back with an error because the rs is empty.
To explain further, my sql statement retrieves data with records of the selected date and payment method.
If no data, it should just display 0.00 but it does not.
It still goes through after if(rs.next()) - an explanation on why my codes behave like this would be greatly appreciated.
Thank you.
pstmt = conn.prepareStatement("Select sum(bdPrice) from tblReceipt where date=? and paymentMethod=?");
pstmt.setString(1, String.valueOf(tdf.format(datePicker.getDate())));
pstmt.setString(2,"CARD");
rs = pstmt.executeQuery();
if (rs.next()) {
lblDailyCard.setText(String.valueOf(df.format(Double.valueOf(rs.getString(1)))));
} else {
lblDailyCard.setText("0.00");
}
pstmt.close();
Upvotes: 0
Views: 63
Reputation: 495
I think this might be the problem:
You try to select sum(bdPrice)
which is a integer/double, yet when you try to set the text, you use rs.getString(1)
. This should be rs.getDouble(1)
since the sum is a double (assuming that bdPrice
is also a double).
Upvotes: 2