前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >Incorrect result size: expected 1, actual 2

Incorrect result size: expected 1, actual 2

作者头像
猫头虎
发布2024-04-07 20:46:35
发布2024-04-07 20:46:35
23600
代码可运行
举报
运行总次数:0
代码可运行

问题描述:

结果大小不正确:预期为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值。 这是我的方法。

代码语言:javascript
代码运行次数:0
运行
复制
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;
}

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

代码语言:javascript
代码运行次数:0
运行
复制
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.

代码语言:javascript
代码运行次数:0
运行
复制
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方法。

代码语言:javascript
代码运行次数:0
运行
复制
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.

代码语言:javascript
代码运行次数:0
运行
复制
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.    
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-08-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 问题描述:
  • 我使用Jdbctemplate从数据库中检索单个String值。 这是我的方法。
  • 在我的情况下,完全可以不要对我的查询造成打击,所以我的问题是如何解决以下错误消息。
  • 最满意答案
    • 总结:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档