0
votes

I am bit new to Spring and Spring JDBC Template. I am retrieving some rows from my database using mysql. The query will produce null results in some times. Therefore I need to do a null check for the results retrieved using JDBC Template.

This is my code for retrieving data from database.

try {
        String sqlArrears = "SELECT SUM(total_payable) FROM letter_delaypayments WHERE status = '1' AND customer_order_id = '"+customerOrderIdList.get(j)+"' AND year(row_added_date) = year(curdate()) AND month(row_added_date) = month(curdate())";

        double arrearsAmountForSingleCustomer = getSimpleJdbcTemplate().queryForObject(sqlArrears, Double.class);

    } catch (Exception e) {
                    System.out.println("EXCEPTION: While taking relavant arrears payments for customer order ids : "+e);
    } 

There may not be rows in the database table in some cases. In those cases, this query pass Null pointer exception.

So what I need to know is, can I check for the NULL value it returns and put an exception in that cases or else, Do I have to look for the row count particular query matches with and then do the retrieval.

I.E - look for the row count particular query matches,

  • if it is 0, add an exception.
  • If it is >1, retrieve the results using above query.

What should I do here? Could you please let me know, whether there is a way to check for the null result at the same time query retrieves the results.

Thank you!

1

1 Answers

0
votes

I would suggest that such logic belongs in the service layer. Your DAO can return a Double (not double) and you can check for null in the service layer.