0

I want to get input value from Spring Boot Application thymeleaf(HTML) to MySQL Database in format Date. But I get this error message : "Failed to convert value of type 'java.lang.String' to required type 'java.sql.Date'".

This is my Controller path code below. Thanks in advance.

@GetMapping("/saveApplication")
    public String saveApplication(@RequestParam(name="name", required = false) String name,
            @RequestParam(name="surname", required = false) String surname,
            @RequestParam(name="place", required = false) String place,
            @RequestParam(name="start", required = false) @DateTimeFormat(pattern = "dd.MM.yyyy") Date start,
            @RequestParam(name="end", required = false) @DateTimeFormat(pattern = "dd.MM.yyyy") Date end,
            @RequestParam(name="identity", required = false) Integer identity,
            @RequestParam(name="tel", required = false) Integer tel,
            Model model) 
    {

        final Integer id = 1;
        
        
        String sql = "INSERT INTO `user`.`application` (`kullaniciid`, `name`, `surname`, `place`,`start`, `end`,`identity`,`tel`) VALUES ( ?, ?, ?,  ?, ?, ?, ?, ?)";
        
        jdbcTemplate.update(sql, id,name, surname, place,start, end,identity,tel);
        
        
        return "screen3";
    }
  • Looks like you are mixing String and Date classes. Either use String everywhere, which can easily handle Date as well, or use 'java.sql.Date' where you have defined date to be String. – dhakalkumar Dec 24 '21 at 11:12
  • check https://stackoverflow.com/a/61469272/175554 – ozkanpakdil Jan 01 '22 at 17:28

1 Answers1

0

I fixed it change @DateTimeFormat to @Date and import Date.

Zoe
  • 27,060
  • 21
  • 118
  • 148