mysql - How to handle jdbc.queryForObject if it doesn't return a row -
i know how use jdbc in case.
the savelinkhistory
column bit(1) type in mysql.
public boolean getissavedlinkhistory(string name) { string sql = "select savelinkhistory users name = ?"; boolean istracked = jdbctemplateobject.queryforobject(sql, new object[] { name }, boolean.class); return istracked; }
the query worked until got error of incorrect result size: expected 1, actual 0
because name
didn't exist, queryforobject
method expects 1 row result.
how can handle case, throw exception says "name" doesn't exist ? , way, boolean
ok here ? because didn't see such code before.
queryforobject method of jdbctemplate expects query should return 1 row else throw emptyresultdataaccessexception. can use query method resultsetextractor instead of queryforobject. resultsetextractor - extractdata(resultset rs) method return arbitrary result object, or null if no data returned. can throw relevant exception if returns null.
return jdbc.query(sql, new resultsetextractor<boolean>() { @override public boolean extractdata(resultset rs) throws sqlexception, dataaccessexception { return rs.next() ? rs.getboolean("column_name") : null; } });
Comments
Post a Comment