gpt4 book ai didi

java - 形式错误 : org. springframework.dao.EmptyResultDataAccessException : Incorrect result size: expected 1, 实际 0

转载 作者:行者123 更新时间:2023-11-30 06:01:41 24 4
gpt4 key购买 nike

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

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

org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 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));
}
}

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

最佳答案

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

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<LoginInfoAdmin> 。这样你就可以返回 Optional.empty()如果没有匹配的记录。

关于java - 形式错误 : org. springframework.dao.EmptyResultDataAccessException : Incorrect result size: expected 1, 实际 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52158230/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com