Ok, I have a form with a date picker that I made which brings the input value in a dd-mm-yyyy format.
I am checking the output on every step but I'm stuck on a first. So part of the code like this.
Servlet controller:
Pacijent pacijent = new Pacijent();
try {
Date datum = new SimpleDateFormat("dd-mm-yyyy").parse(request.getParameter("datum"));
pacijent.setDatum(datum);
} catch (ParseException e) {
e.printStackTrace();
}
out.println(pacijent.getDatum());
//not important part of a code ...
//dao.addPacijent(pacijent);
Pacijent class:
public class Pacijent {
private int id;
private String ime;
private String prezime;
private Date datum;
private String adresa;
// Getters and Setters...
}
Insert function in PacijentDao class
public void addPacijent(Pacijent pacijent) {
try {
PreparedStatement ps = connection.prepareStatement("INSERT INTO pacijent (ime, prezime, datum, adresa) VALUES (?,?,?,?)");
ps.setString(1, pacijent.getIme());
ps.setString(2, pacijent.getPrezime());
ps.setDate(3, (Date) pacijent.getDatum());
ps.setString(4, pacijent.getAdresa());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
And after I click submit button it outputs me date like this Wed Jan 18 00:04:00 CET 2017. I need help to parse it in a way that mysql accepts it. Datum field in database is Date format.
Thanks