gpt4 book ai didi

Java Spring MVC 数据库异常未获取

转载 作者:行者123 更新时间:2023-11-30 07:43:12 26 4
gpt4 key购买 nike

在我的 spring 项目中,我想将学生信息保存在 mysql 数据库中。如果有任何错误,则会显示在jsp页面中相应的输入框中。

所以,我有:-

StudentDao

public interface StudentDao {
public void add ( Student student );
}

StudentDaoImpl

public class StudentDaoImpl implements StudentDao {

@Autowired
private SessionFactory sessionFactory;

public void setSessionFactory(SessionFactory sessionFactory){
this.sessionFactory = sessionFactory;
}

public void add( Student student){
sessionFactory.getCurrentSession().save(student);
}}

StudentService

public interface StudentService {
Student add(String name, String email) throws SQLException,
DataAccessException,
DataIntegrityViolationException,
ConstraintViolationException{
}

StudentServiceImpl

public class StudentServiceImpl implements StudentService {


@Autowired
private StudentDao studentDao;

@Transactional(propagation = Propagation.REQUIRED)
public Student add(String name, String email) throws SQLException,
DataAccessException,
DataIntegrityViolationException,
ConstraintViolationException{


Student student = new Student(name,email);
studentDao.add(student);

return student;
}

}

Controller

@RequestMapping(value = "/add", method = RequestMethod.POST)
public String doAdd(@Valid @ModelAttribute("student") Student student,BindingResult result,HttpServletRequest request,
HttpServletResponse response,Model model){

validator.validate(student, result);
if (result.hasErrors()) {

return "signup";
}

@SuppressWarnings("unused")
Student student1;

try{
try {
student1= studentService.add(student.getName(), student.getEmail());
} catch (ConstraintViolationException e) {

model.addAttribute("email", "already exists");
e.printStackTrace();
return "signup";
} catch (DataAccessException e) {

model.addAttribute("email", "already exists");
e.printStackTrace();
return "signup";
} catch (SQLException e) {

model.addAttribute("email", "already exists");
e.printStackTrace();
return "signup";
}
}catch (DataIntegrityViolationException e)
{
model.addAttribute("email", "already exists");

e.printStackTrace();
return "signup";

}

return "signup";
}

但问题是我的数据库电子邮件字段是唯一的。因此,对于重复条目,我收到日志消息:

 WARN JDBCExceptionReporter:100 - SQL Error: 1062, SQLState: 23000
JDBCExceptionReporter:101 - Duplicate entry 'df@gmail.com' for key 'unique_index2'
org.hibernate.exception.ConstraintViolationException: could not insert: [com.myweb.model.Student]
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'df@gmail.com' for key 'unique_index2'

我想将此错误放置到 jsp View 电子邮件字段中,电子邮件已存在我在服务类中使用了 throws block ,并在 Controller 类中使用了 try-catch block ..尽管为什么错误没有显示在jsp View ?

最佳答案

所以,你正在使用 spring 和 hibernate ..异常(exception)是:ConstraintViolationException 这就是为什么它:

org.hibernate.exception.ConstraintViolationException

只需添加:

<h1>${email}</h1>

您可以使用其他标签来显示该消息。

关于Java Spring MVC 数据库异常未获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34367792/

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