A
A
Artem2016-08-21 14:03:01
MySQL
Artem, 2016-08-21 14:03:01

How to pass date from jsp page through spring to mysql database?

Please help me figure out how to pass the date from the jsp page to the database.
It would be desirable that the date could be selected and then sent to the database. Used In the database, the column birthDate is of type date. When sending other values, they are all written to the database, but when I enter the date, it gives an error.
<input type="date" name="birthDate">


The request sent by the client was syntactically incorrect.


jsp page code
<%@ page language="java" contentType="text/html" pageEncoding="utf8"%>
<!DOCTYPE html>
<html lang="ru">
<body>
<form action="${pageContext.servletContext.contextPath}/addclient" method="post">   
    <table >        
            <th><input type="text" name="surname" placeholder="Фамилия" ></th>        
            <th><input type="text" name="name" placeholder="Имя" ></th>       
            <th><input type="text" name="patronymic"  placeholder="Отчество" required></th>
    
            <th><input type="date"  name="birthDate" ></th>
    </table>
    <input type="submit"  value="Создать">
    </div>
</form>
</body>
</html>


Client class code
@Entity
@Table(name = "client")
public class Client {
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @Basic
    @Column(name = "surname")
    private String surname;
    @Basic
    @Column(name = "name")
    private String name;
    @Basic
    @Column(name = "patronymic")
    private String patronymic;

    @Basic
    @Column(name = "birthDate")
    private Date birthDate;

    public Client() {    }


Controller
@Controller
public class ClientController {
    @Autowired
    private ClientService clientService; 

    @RequestMapping(value = "/addclient", method = RequestMethod.POST)
    public String addClient(@ModelAttribute Client client) {
        clientService.addClient(client);
        return "redirect:/client";
    }
}

Dao class
@Repository
public class ClientDaoImpl implements ClientDao{
    @Autowired
    private SessionFactory sessionFactory;    

    public void addClient(Client client){   
        sessionFactory.getCurrentSession().save(client);
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sirs, 2016-08-21
@webserverby

As far as I understand your problem is related to the fact that you are using java.util.Date and trying to store it in mysql. There are several solutions:
1) In the Client class, change the field type to java.sql.Date and do the conversion somewhere before saving to the database
2) Add @Temporal annotation

@Temporal(TemporalType.DATE)
private Date birthDate;

in the Client class.
3) Collect date as string
java.text.SimpleDateFormat sdf = 
     new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String birthDateTime = sdf.format(birthDateInput);

and save to the database. The downside is that with select, you will need to do the reverse.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question