Witam, sytuacja wygląda następująco:
Tworzę serwis webowy w springu. Stworzyłem stronę do rejestracji użytkowników. Wszystko bangla ładnie i pięknie dopóki nie spróbujemy dodać użytkownika, który już istnieje w bazie danych, ponieważ kolumna username
jest kluczem głównym.
Moim celem jest obsłużenie tego wyjątku, a następnie dodanie do widoku jakieś informacji, że dana nazwa użytkownika jest już zajęta. No dobra, próbuję ale nie wychodzi:
błąd który wyskkakuje:
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)
Wkleję trochę kodu:
import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import com.packt.webstore.domain.User;
import com.packt.webstore.domain.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@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);
}
}
no to zgłosiłem, że moja metoda może spowodować bigos i w kontrolerze zamierzałem ją obsłużyć:
@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) {
//tutaj jakiś kod co mi doda do widoku komuniukat, że nazwa użytkownika już jest
}
return "redirect:/login";
}
}
No i to nie działa. Co mam zrobić :(?