return a boolean - jdbcTemplate

bdfios picture bdfios · Apr 4, 2014 · Viewed 28k times · Source

I would like to return a boolean value using in this method:

public Boolean isSizeOk(String transactionId){ 
    String sqlQuery = "SELECT true FROM customer_pool WHERE id = "+ transactionID + " AND level = 13)";

//The next line is the problem. 
    //If I am returning a Boolean List, I can write

    List <Boolean> sizeResult = jdbcTemplate.queryForList(sqlQuery, Boolean.class, transactionId);

    //But since I only want a boolean value, what will the statement be?
     Boolean sizeResult = jdbcTemplate......?

    return sizeResult;
}

Kindly help. Thanks.

Answer

kostya picture kostya · Apr 4, 2014

If you want to write a method that checks that a record exists in the database you can use the following code:

Integer cnt = jdbcTemplate.queryForObject(
    "SELECT count(*) FROM customer_pool WHERE id = ? AND level = 13)", Integer.class, id);
return cnt != null && cnt > 0