public User Login(User loginUser){
String sql ="select * from user where username = ? and password = ?";
User user = template.queryForObject(sql,newBeanPropertyRowMapper<>(User.class),
loginUser.getUsername(), loginUser.getPassword());return user;}
解决方案:捕获查询为空异常,当查询为空时,控制台会自动抛出异常并打印异常信息。
public User Login(User loginUser){try{
String sql ="select * from user where username = ? and password = ?";
User user = template.queryForObject(sql,newBeanPropertyRowMapper<>(User.class),
loginUser.getUsername(), loginUser.getPassword());return user;}catch(DataAccessException e){
e.printStackTrace();//记录日志return null;}}
用户登录验证案例遇到500 Incorrect result size: expected 1, actual 0 报错时的解决方案报错原因:由于用户在登陆时输入的值错误,导致数据库查询不到而报了“结果大小不正确:预期为1,实际为0”的错误,提示查询语句查询为空,但由于没有捕获查询异常的情况,导致并不能很明确的知道哪一行语句错误。 public User Login(User login...