Spring工程中JDBC抛出"EmptyResultDataAccessException"异常的解决方案

一、 问题日志

严重: Servlet.service() for servlet [spring] in context with path [/XXXXX] threw exception [Request processing failed; nested exception is org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0] with root cause
org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0
	at org.springframework.dao.support.DataAccessUtils.nullableSingleResult(DataAccessUtils.java:97)
	at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:779)

二、 问题原因

顺藤摸瓜,打开"DataAccessUtils":

/**
 * Return a single result object from the given Collection.
 * <p>Throws an exception if 0 or more than 1 element found.
 * @param results the result Collection (can be {@code null}
 * and is also expected to contain {@code null} elements)
 * @return the single result object
 * @throws IncorrectResultSizeDataAccessException if more than one
 * element has been found in the given Collection
 * @throws EmptyResultDataAccessException if no element at all
 * has been found in the given Collection
 * @since 5.0.2
 */
@Nullable
public static <T> T nullableSingleResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException {
	// This is identical to the requiredSingleResult implementation but differs in the
	// semantics of the incoming Collection (which we currently can't formally express)
	if (CollectionUtils.isEmpty(results)) {
		throw new EmptyResultDataAccessException(1);
	}
	if (results.size() > 1) {
		throw new IncorrectResultSizeDataAccessException(1, results.size());
	}
	return results.iterator().next();
}

从注释或者源码可以看出,数据库执行结果为0时,抛出"EmptyResultDataAccessException"异常,大于1时,抛出"IncorrectResultSizeDataAccessException"异常,这样设计有个好处,就是更加详细地反馈了数据信息,便于开发者处理查询结果。


三、 解决方案

在调用"queryForObject"方法的位置捕捉以上两种异常,参考:

public UserVO queryUserByEmail(String email) {
	String SQL = "select * from " + Const.User.TABLE_NAME + " where " + Const.User.COLUMN_EMAIL + " = ?";
	UserVO userVO = null;
	try {
		userVO = jdbcTemplate.queryForObject(SQL, new UserMapper(), email);
		if (userVO != null) {
			userVO.setPassword("");
		}
	} catch (EmptyResultDataAccessException e) {
		e.printStackTrace();
	} catch (IncorrectResultSizeDataAccessException e) {
		e.printStackTrace();
	}
	return userVO;
}

四、 参考文献

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值