mike mike - 9 months ago 198
Java Question

Query returns boolean value in spring jdbctemplate?

Here I am writing Database application.In that I want to know if the

column becomes null then i want to find that record and if there is any record like that then I want to return false,or if there is no records like that then I want to return true.

Because this query is for validating the user.If the user is alredy logined then
will become null, if he logedout then
column contains date. so if the
column contains null then I will not allow him to login.

So I wrote the following query for that.

public boolean isUserAlreadyLoggedIn(int userId) {
String sql="select USERID from useraudit where USERID=? ENDDATETIME is null";
return false;

Please suggest me the correct way.

Answer Source

You could use this approach:

String sql = "SELECT count(*) from useraudit where USERID=? AND ENDDATETIME is null";
boolean result = false;

int count = getJdbcTemplate().queryForObject(
                    sql, new Object[] { username }, Integer.class);

if (count > 0) {
    result = true;

return result;