Spring jdbcTemplate 更新中的瞬态数据访问资源异常
Spring Transient Data Access Resource Exception in jdbcTemplate update
我有一种方法可以检测列的重复条目:
(我正确注入 jdbcTemplate
)
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;
}
但是我在运行时遇到了这个异常:
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
我们可以这样使用jdbcTemplate
的queryForList()
方法:
results = jdbcTemplate.queryForList(sql,new Object[]{username},String.class);
if(results.isEmpty(){
//no duplicate
}
else{
//duplicate
}
其中 results
是 List<String>
。
我有一种方法可以检测列的重复条目:
(我正确注入 jdbcTemplate
)
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;
}
但是我在运行时遇到了这个异常:
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
我们可以这样使用jdbcTemplate
的queryForList()
方法:
results = jdbcTemplate.queryForList(sql,new Object[]{username},String.class);
if(results.isEmpty(){
//no duplicate
}
else{
//duplicate
}
其中 results
是 List<String>
。