mike mike - 7 days ago 4
Java Question

Query returns boolean value in spring jdbctemplate?

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

ENDDATETIME
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
ENDDATETIME
will become null, if he logedout then
ENDDATETIME
column contains date. so if the
ENDDATETIME
column contains null then I will not allow him to login.

So I wrote the following query for that.

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


Please suggest me the correct way.

Answer

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;