当前位置: 首页 > 知识库问答 >
问题:

表单错误:org.springframework.dao.EmptyResultDataAccessException:结果大小不正确:预期1,实际0[重复]

公孙阳文
2023-03-14

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

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

org.springframework.dao.EmptyResultDataAccess异常: 不正确的结果大小: 预期的 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>

用户道(其中我使用模板连接到数据库并触发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));  
    }  
}

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

共有1个答案

江凯风
2023-03-14

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

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最好返回可选

 类似资料: