【问题标题】:Error of the form: org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0 [duplicate]形式的错误:org.springframework.dao.EmptyResultDataAccessException:不正确的结果大小:预期1,实际0 [重复]
【发布时间】:2018-09-04 03:02:01
【问题描述】:

我已经构建了一个简单的登录页面,其中我使用用户提供的用户名从我的数据库中获取所需的行,并根据用户提供的值检查凭据。

我面临的问题是,如果用户输入数据库中不存在的用户名值,我会遇到此错误

org.springframework.dao.EmptyResultDataAccessException:不正确 结果大小:预期 1,实际 0

这意味着我的数据库没有任何具有所需用户名的行。

代码:

@Controller  
public class LoginController {  
    @Autowired  
    UserDao dao;
    @RequestMapping(value="/login",method = RequestMethod.POST)  
    public ModelAndView loginResult(HttpServletRequest req,HttpServletResponse res,RedirectAttributes redir,Model model) {

    String uname=req.getParameter("username");  
    String pwd=req.getParameter("pwd");  
    String dept = req.getParameter("dept");
    LoginInfoAdmin admin = dao.getEmpById(uname); 

if(uname.equals(admin.getUsername())&&pwd.equals(admin.getPassword())&&dept.equals(admin.getDept()))
            {
                req.getSession().setAttribute("uname",admin.getName());
                if(dept.equals((String)admin.getDept())){
                    return new ModelAndView("adminLoginResult", "message", message1); 
                }
                else if(dept.equals((String)admin.getDept())){
                    return new ModelAndView("EmployeeLoginResult","message",message1);
                }
                else{
                    return new ModelAndView("ManagerLoginResut","message",message1);
                }
            } 
else
        {
            return new ModelAndView("forward:index.jsp","message","Sorry Username Password Error");
}

索引页:

<body>
    <b><span class="heading">LOGIN USER</span></b>
    <div class="container">
        <form action="login.html" method="Post">
            <div class="form_style">
            <input type="text" name="username" placeholder="Enter Username"/>
            <input type="password" name="pwd" placeholder="Enter password"/>
            <select name="dept">
                <option>IT</option>
                <option>ADMIN</option>
                <option>HR</option>
                <option>MARKETING</option>
            </select>
            <input type="Submit" value="submit">
            <span class="disp">${message}</span>
            </div>
        </form>
    </div>
</body>

UserDao(我使用模板连接数据库并触发sql)

public class UserDao {
    JdbcTemplate template;  
    public void setTemplate(JdbcTemplate template) {  
        this.template = template;  
    }  
    public LoginInfoAdmin getEmpById(String username){  
        String sql="select * from ADMINLOGINDETAILS where username=?";  
        return template.queryForObject(sql, new Object[]{username},new BeanPropertyRowMapper<LoginInfoAdmin>(LoginInfoAdmin.class));  
    }  
}

我的问题是如何在查询触发后检查该行是否存在。

【问题讨论】:

标签: java spring eclipse spring-mvc derby


【解决方案1】:

有多种方法可以解决您的问题。一种方法是捕获顶层抛出的异常:

try {
    LoginInfoAdmin admin = dao.getEmpById(uname);
    if(uname.equals(admin.getUsername()) && pwd.equals(admin.getPassword()) && dept.equals(admin.getDept()))
    {
        req.getSession().setAttribute("uname",admin.getName());
        if(dept.equals((String)admin.getDept())){
            return new ModelAndView("adminLoginResult", "message", message1); 
        }
        else if(dept.equals((String)admin.getDept())){
            return new ModelAndView("EmployeeLoginResult","message",message1);
        }
        else{
            return new ModelAndView("ManagerLoginResut","message",message1);
        }
    } 
    else
    {
        return new ModelAndView("forward:index.jsp","message","Sorry Username Password Error");
    }
} catch (EmptyResultDataAccessException erdae) {
    return new ModelAndView("forward:index.jsp","message","Sorry Username Password Error");
}

另一种方法是在记录丢失时将其捕获并返回 null(然后在顶层检查 null):

public LoginInfoAdmin getEmpById(String username){  
    try {
        String sql="select * from ADMINLOGINDETAILS where username=?";  

        return template.queryForObject(sql, new Object[]{username},new BeanPropertyRowMapper<LoginInfoAdmin>(LoginInfoAdmin.class));  
    } catch (EmptyResultDataAccessException erdae) {
        return null;
    }
}

编辑:更好的做法是让您的 DAO 返回 Optional&lt;LoginInfoAdmin&gt;。这样,如果没有匹配的记录,您可以返回 Optional.empty()

【讨论】:

  • 好的,谢谢杰森的建议。我想我会实现这个。
猜你喜欢
  • 1970-01-01
  • 2015-04-24
  • 2015-03-13
  • 2017-06-02
  • 1970-01-01
  • 2012-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多