JdbcTemplate中queryForObject方法返回空结果或不正确结果数量的解决方法
在使用Spirng提供的JdbcTemplate中名为queryForObject API进行数据库查询时有时会抛出如下异常:
org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0
(不正确的结果大小,预期是1,实际为0)
或者:
org.springframework.dao.IncorrectResultSizeDataAccessException: Incorrect result size: expected 1, actual 2
(不正确的结果大小,预期是1,实际为2)
在解决这些异常之前,我们首先来看看queryForObject API的源代码,假设我们调用的是queryForObject(String sql,Class requiredType)。
JdbcTemplate.class
public <T> T queryForObject(String sql, Class<T> requiredType)
throws DataAccessException
{return queryForObject(sql, getSingleColumnRowMapper(requiredType));} public <T> T queryForObject(String sql, RowMapper<T> rowMapper)
throws DataAccessException {
List<T> results = query(sql, rowMapper);
return DataAccessUtils.requiredSingleResult(results);
} public <T> List<T> query(String sql, RowMapper<T> rowMapper)
throws DataAccessException {
return query(sql, new RowMapperResultSetExtractor<T>(rowMapper));
} public <T> T query(final String sql, final ResultSetExtractor<T> rse)
throws DataAccessException {
Assert.notNull(sql, "SQL must not be null");
Assert.notNull(rse, "ResultSetExtractor must not be null");
if (logger.isDebugEnabled()) {
logger.debug("Executing SQL query [" + sql + "]");
}
class QueryStatementCallback implements StatementCallback<T>, SqlProvider {
public T doInStatement(Statement stmt) throws SQLException {
ResultSet rs = null;
try {
rs = stmt.executeQuery(sql);
ResultSet rsToUse = rs;
if (nativeJdbcExtractor != null) {
rsToUse = nativeJdbcExtractor.getNativeResultSet(rs);
}
return rse.extractData(rsToUse);
}
finally {
JdbcUtils.closeResultSet(rs);
}
}
public String getSql() {
return sql;
}
}
return execute(new QueryStatementCallback());
}
DataAccessUtil.class
public static <T> T requiredSingleResult(Collection<T> results)
throws IncorrectResultSizeDataAccessException {
int size = (results != null ? results.size() : 0);
if (size == 0) {
throw new EmptyResultDataAccessException(1);
}
if (results.size() > 1) {
throw new IncorrectResultSizeDataAccessException(1, size);
}
return results.iterator().next();
}
通过阅读源代码,可以清楚地看到在DataAccessUtils.class中requiredSingleResult方法中,当结果集合的size为0或者大于1时,就会抛出以上两个异常。
解决方法有两个:
(1)通过修改数据库:删除数据库中对应名称(column)相同的记录,留下只剩"1"条。
(2)通过更换方法:使用query方法返回list对象(该方法能返回所有查询记录)
public transient List query(String sql, RowMapper rowMapper, Object args[])
throws DataAccessException
{
return (List)query(sql, args, ((ResultSetExtractor) (new RowMapperResultSetExtractor(rowMapper))));
}
JdbcTemplate中queryForObject方法返回空结果或不正确结果数量的解决方法的更多相关文章
- ios系统微信浏览器、safari浏览器中h5页面上拉下滑导致悬浮层脱离窗口的解决方法
一. 运行环境: iphone所有机型的qq浏览器,safari浏览器,微信内置浏览器(qq浏览器内核)等. 二. 异常现象: 1. 大幅度上下滑动h5页面,然后停止滑动,有时候会影响到页面滚动,如局 ...
- 在Eclipse中运行Jboss时出现java.lang.OutOfMemoryError:PermGen space及其解决方法
在Eclipse中运行Jboss时出现java.lang.OutOfMemoryError:PermGen space及其解决方法 在Eclipse中运行Jboss时,时间太长可能有时候会出现java ...
- ckfinder在IE10,IE9中的弹出框不能选择,或者不能上传解决方法
在IE9,或IE10中ckfinder在IE10,IE9中的弹出框不能选择,或者不能上传解决方法 把弹出框嵌入到jquery dialog中.可以解决 I did: // javascript f ...
- JdbcTemplate中queryForObject的EmptyResultDataAccessException问题
在使用Spring提供的JdbcTemplate中名为queryForObject API进行数据库查询时有时会抛出如下异常提示息,org.springframework.dao.EmptyResul ...
- 返回Json数据浏览器带上<pre></pre>标签解决方法
问题: 当后台获取到前台传来的文件时(例如上传功能, 导入功能), 返回类型为application/json, 这个时候响应到前端的JSON格式的数据格式可能是: <pre style=&q ...
- nginx下运行php的程序时返回200访问却是空白页问题的解决方法
由于nginx与php-fpm之间的一个小bug,会导致这样的现象: 网站中的静态页面 *.html 都能正常访问,而 *.php 文件虽然会返回200状态码, 但实际输出给浏览器的页面内容却是空白. ...
- mybatis返回map类型数据空值字段不显示的解决方法
在日常开发中,查询数据返回类型为map,数据库中有些自动值为null,则返回的结果中没有值为空的字段,则如何显示值为空的字段呢? Spring boot + MyBatis返回map中null值默认不 ...
- Silverlight中文本框添加回车事件后,换行无法清除的解决方法
在开发Silverlight的项目中,为了更好的用户体验,我们常要给一些控件添加一些快捷键.然而,在Silverlight中当用户回车提交后,光标停留在文本框的第二行怎么也清除不掉,经过一段时间研究, ...
- Jquery中使用setInterval和setTimeout会提示缺少对象的错误,解决方法如下:
直接在ready中调用其他方法,会提示缺少对象的错误,解决方法如下: 方法1. 应用jQuery的扩展可以解决这个问题. $(document).ready(function(){ $.extend( ...
随机推荐
- 1 Introduction
1. Introduction 1.1. License Flowable is distributed under the Apache V2 license. 1.2. Download http ...
- Practical Mathematical Handwriting
In this article, I discuss the handwriting of $\mathbb{A}, \mathcal{A}, \mathscr{A}, \mathfrak{A}$'s ...
- H5 俄罗斯方块Demo
链接:http://pan.baidu.com/s/1hrDM0T2
- python线程join方法
转载:http://www.cnblogs.com/cnkai/p/7504980.html Python多线程与多进程中join()方法的效果是相同的. 下面仅以多线程为例: 首先需要明确几个概念: ...
- docker(二) windows10下安装docker
官方安装文档: https://docs.docker.com/docker-for-windows/install/ https://docs.docker.com/docker-for-windo ...
- 在nginx上用FastCGI解析PHP
nginx配置文件: Nginx 默认使用 include enable-php.conf; 通过enable-php.conf 来解析PHP,该文件内容如下 location ~ [^/]\. ...
- sql sever基本命令
创建表: create table stu_info( id ,) not null primary key clustered, name ) not null, score numeric not ...
- SpringBoot2.0的CacheManager配置
http://rickgong.iteye.com/blog/2414263 @Configurationpublic class RedisConfig extends CachingConfigu ...
- T66597 小xzy的任务 题解
T66597 小xzy的任务 题目背景 今天,小xzy的班主任交给他一个严肃的任务,匹配羽毛球运动员! ! ! 题目描述 羽毛球队有男女运动员各n人.给定2个n×n矩阵P和Q.Pij是男运动员i和女 ...
- Python——三级菜单
#三级菜单函数 menu = { '北京':{ 海淀:{ '五道口':{} '中关村':{} '上帝':{} } '昌平':{} '朝阳':{} '东城':{} }, '上海':{} '山东':{} ...