【问题标题】:Spring Transient Data Access Resource Exception in jdbcTemplate updatejdbcTemplate 更新中的 Spring 瞬态数据访问资源异常
【发布时间】:2015-06-27 09:02:14
【问题描述】:

我有一种方法可以检测列的重复条目:

(我正确地注入到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

【问题讨论】:

  • 一开始你不是通过模板update,而是queryForObject。其次,您返回一个用户名,即String。不是整数或布尔值。

标签: sql spring jdbc


【解决方案1】:

我们可以像这样使用jdbcTemplatequeryForList()方法:

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

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

else{
  //duplicate
}

其中resultsList<String>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-21
    • 1970-01-01
    • 2021-05-23
    • 2013-07-25
    相关资源
    最近更新 更多