gpt4 book ai didi

java - 如何在 spring mvc 应用程序中向数据库添加重复用户名时处理异常

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

我正在构建 spring mvc 应用程序。我想禁止添加重复(用户名是主键)条目并通知以选择另一个用户名。

我正在研究注册用户选项。一切正常,直到我将用户与已经存在的用户名放在一起。好吧,这是故意的,因为用户名列是我数据库中的主键。

我正在寻找一个选项来处理这个:

这是我的 SQL 表

 create table users(
username varchar(50) not null primary key,
password varchar(50) not null);

这是我的仓库:

@Repository

class UserRepositoryImpl implements UserRepository{

@Autowired
private JdbcTemplate jdbcTemplate;


@Override
public void addUser(User user) throws MySQLIntegrityConstraintViolationException {
/*language=SQL*/
String SQL_ADD_USER= "INSERT INTO users VALUES (?,?)";

String username = user.getUsername();
String password = user.getPassword();
jdbcTemplate.update(SQL_ADD_USER, username, password);

}
}

这是我的 Controller 类的一部分:

@RequestMapping(value="/register", method = RequestMethod.GET)
public String registerPage(@ModelAttribute("user") User user){return "register";}

@RequestMapping(value="/register", method=RequestMethod.POST)
public String processRegisterUser(@ModelAttribute("user") User user, BindingResult result){

try {
userRepository.addUser(user);
} catch (com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException e) {

//some line of code that would add information to the view that user with given username already exists.
}


return "redirect:/login";
}

如您所见,我尝试处理在添加重复条目期间发生的异常。这就是为什么我将此子句 throws MySQLIntegrityConstraintViolationException 放在 public void addUser(User user) 中。在下一步中,我想在 public String processRegisterUser 方法中处理这个异常,这样我就可以通知用户给定的用户名已被占用。嗯,这是行不通的。我无法处理 com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException 并且在 View 中添加重复错误而不是警告时我得到:

 HTTP Status 500 - Request processing failed; nested exception is org.springframework.dao.DuplicateKeyException: PreparedStatementCallback; SQL [INSERT INTO users VALUES (?,?)]; Duplicate entry 'user' for key 'PRIMARY'; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'user' for key 'PRIMARY'

type Exception report

message Request processing failed; nested exception is org.springframework.dao.DuplicateKeyException: PreparedStatementCallback; SQL [INSERT INTO users VALUES (?,?)]; Duplicate entry 'user' for key 'PRIMARY'; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'user' for key 'PRIMARY'

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.DuplicateKeyException: PreparedStatementCallback; SQL [INSERT INTO users VALUES (?,?)]; Duplicate entry 'user' for key 'PRIMARY'; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'user' for key 'PRIMARY'
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:115)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)

如果您需要任何详细信息,请发表评论。

最佳答案

你正在使用 spring jdbcTemplate 进行数据库连接,它有自己的异常层次结构,所以你可以 try catch spring 异常 org.springframework.dao.DuplicateKeyException

关于java - 如何在 spring mvc 应用程序中向数据库添加重复用户名时处理异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38771977/

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