title: How to handle exception while adding duplicate username to a database in spring mvc application
body: <p>I am building spring mvc application. I want to dissalow adding duplicate(username is primary key) entry and inform in view to choose another username.</p>
  
  <p>I am working on register user option. Everything works until I put user with username that already exist. Well, It was deliberate because username column is a primary key in my database. </p>
  
  <p>I am looking for an option to handle this so:</p>
  
  <p>This is my SQL table</p>
  
  <pre><code> create table users(
    username varchar(50) not null primary key,
    password varchar(50) not null);
  </code></pre>
  
  <p>This is my repository:</p>
  
  <pre><code>@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);
  
      }
  }
  </code></pre>
  
  <p>and this is part of my Controller class:</p>
  
  <pre><code>@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";
  }
  </code></pre>
  
  <p>As you can see I tried to handle an exception that occurs during adding duplicate entry. This is why I put this clause <code>throws MySQLIntegrityConstraintViolationException</code> in public void addUser(User user). In the next step I wanted to handle this exception in <code>public String processRegisterUser</code> method so that I could inform user in view that given username is occupied. Well, It doesn't work. I can't handle <code>com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException</code> and during adding duplicate error instead of warning in view I get:</p>
  
  <pre><code> 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)
  </code></pre>
  
  <p>If you need any details please comment. </p>