org.springframework.dao.EmptyResultDataAccessException

当使用JdbcTemplate的queryForObject方法查询数据库无结果时,会抛出EmptyResultDataAccessException异常。为避免服务器报500错误,解决方案是在dao层捕获该异常并向上抛到service层处理。在service层用try-catch块包围查询代码,捕获异常后返回null,控制器层再根据返回值判断用户登录状态。

JdbcTemplate的queryForObject查询不到结果会抛异常:

login.jsp提交登录name和pwd到user/login的controller访问dao层的selectByNameAndPwd方法查询结果为空,就在dao层抛了异常所以服务器就报了500

 解决方法:dao层向上抛异常,抛到service层try-catch处理:
dao
    @Override
    public User selectByNameAndPwd(String name, String password) throws EmptyResultDataAccessException {
        return jdbcTemplate.queryForObject("select * from sys_user where name = ? and password = ?", new BeanPropertyRowMapper<User>(User.class),name,password);
    }
service 
    @Override
    public User login(String name, String password) {
        try {
            return userDao.selectByNameAndPwd(name, password);
        }catch (EmptyResultDataAccessException e){
            e.printStackTrace();
            return null;
        }
    }
controller 
    @RequestMapping("/login")
    public String login(String name, String password, HttpSession session){
        User user = userService.login(name, password);
        if (user!=null){
            session.setAttribute("user",user);
            return "redirect:/jsp/index.jsp";
        }else{
            return "redirect:/jsp/login.jsp";
        }
    }
从orcale更换到mysql,springbatch运行偶尔会报错: 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:784) at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:809) at org.springframework.batch.core.repository.dao.JdbcJobExecutionDao.synchronizeStatus(JdbcJobExecutionDao.java:308) at org.springframework.batch.core.repository.support.SimpleJobRepository.update(SimpleJobRepository.java:166) at sun.reflect.GeneratedMethodAccessor147.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:366) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:99) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) at com.sun.proxy.$Proxy85.update(Unknown Source) at sun.reflect.GeneratedMethodAccessor147.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:127) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
03-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值