Incorrect result size: expected 1, actual 2

简介: Incorrect result size: expected 1, actual 2

结果大小不正确:预期为1,实际为0(Incorrect result size: expected 1, actual 0)

字符串的Jdbctemplate查询:EmptyResultDataAccessException:结果大小不正确:expected 1,actual 0(Jdbctemplate query for string: EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0)


我使用Jdbctemplate从数据库中检索单个String值。 这是我的方法。

public String test() {
    String cert=null;
    String sql = "select ID_NMB_SRZ from codb_owner.TR_LTM_SLS_RTN 
         where id_str_rt = '999' and ID_NMB_SRZ = '60230009999999'";
    cert = (String) jdbc.queryForObject(sql, String.class); 
    return cert;
}

在我的情况下,完全可以不要对我的查询造成打击,所以我的问题是如何解决以下错误消息。


EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0


在我看来,我应该只是收回一个null而不是抛出一个异常。 如何解决这个问题? 提前致谢。


I am using Jdbctemplate to retrieve a single String value from the db. Here is my method.


public String test() {
    String cert=null;
    String sql = "select ID_NMB_SRZ from codb_owner.TR_LTM_SLS_RTN 
         where id_str_rt = '999' and ID_NMB_SRZ = '60230009999999'";
    cert = (String) jdbc.queryForObject(sql, String.class); 
    return cert;
}

In my scenario it is complete possible to NOT get a hit on my query so my question is how do I get around the following error message.


EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0


It would seem to me that I should just get back a null instead of throwing an exception. How can I fix this? Thanks in advance.


最满意答案


在JdbcTemplate中,queryForInt,queryForLong,queryForObject等所有这样的方法都希望执行的查询将返回一行而只有一行。 如果没有任何行或多行,将导致IncorrectResultSizeDataAccessException 。 现在正确的方法是不能捕获此异常或EmptyResultDataAccessException ,但请确保您正在使用的查询应该只返回一行。 如果根本不可能,则使用query方法。

List<String> strLst  = getJdbcTemplate().query(sql,new RowMapper {
  public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
        return rs.getString(1);
  }
});
if ( strLst.isEmpty() ){
  return null;
}else if ( strLst.size() == 1 ) { // list contains exactly 1 element
  return strLst.get(0);
}else{  // list contains more than 1 elements
  //your wish, you can either throw the exception or return 1st element.    
}


In JdbcTemplate , queryForInt, queryForLong, queryForObject all such methods expects that executed query will return one and only one row. If you get no rows or more than one row that will result in IncorrectResultSizeDataAccessException . Now the correct way is not to catch this exception or EmptyResultDataAccessException, but make sure the query you are using should return only one row. If at all it is not possible then use query method instead.

List<String> strLst  = getJdbcTemplate().query(sql,new RowMapper {
  public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
        return rs.getString(1);
  }
});
if ( strLst.isEmpty() ){
  return null;
}else if ( strLst.size() == 1 ) { // list contains exactly 1 element
  return strLst.get(0);
}else{  // list contains more than 1 elements
  //your wish, you can either throw the exception or return 1st element.    
}


目录
相关文章
RuntimeError: Given groups=1, weight of size 64 128 1 7, expected input[16,
RuntimeError: Given groups=1, weight of size 64 128 1 7, expected input[16,
2938 0
error ‘res‘ is assigned a value but never used no
error ‘res‘ is assigned a value but never used no
|
6月前
|
自然语言处理 数据库
Expected one result (or null) to be returned by selectOne(), but found: 2
Expected one result (or null) to be returned by selectOne(), but found: 2
92 0
|
索引
Result window is too large, from + size must be less than or equal to: [10000]
Result window is too large, from + size must be less than or equal to: [10000]
213 0
Optional int parameter ‘id‘ is present but cannot be translated into a null value due to being ……
Optional int parameter ‘id‘ is present but cannot be translated into a null value due to being ……
304 0
syntax error, expect {, actual [, pos 0 at
syntax error, expect {, actual [, pos 0 at
214 0
ValueError: Sample larger than population or is negative
ValueError: Sample larger than population or is negative
197 0