Skip to content
Advertisement

Spring Transient Data Access Resource Exception in jdbcTemplate update

I have a method to detect duplicate entry for a column:

(I inject to jdbcTemplate correctly)

private boolean isDuplicate(String username) {

    String sql = " select username from users where username=?";
    int result = jdbcTemplate.update(sql, new Object[]{username}, String.class);

    return result;
}

But i got this exception in runtime:

org.springframework.dao.TransientDataAccessResourceException: 

PreparedStatementCallback; SQL [ select username from users where username=?]; Invalid argument value: java.lang.ArrayIndexOutOfBoundsException; 

nested exception is java.sql.SQLException: Invalid argument value: java.lang.ArrayIndexOutOfBoundsException

Advertisement

Answer

We can use the queryForList() method of jdbcTemplate like this:

results = jdbcTemplate.queryForList(sql,new Object[]{username},String.class);

if(results.isEmpty(){
  //no duplicate
}

else{
  //duplicate
}

Where results is a List<String>.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement