-1

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

Miomir Pesic
  • 25
  • 1
  • 6
  • Format should be `"dd-MM-yyyy"`. Lowercase `mm` means *minute*, uppercase means *month*. – Andreas Apr 26 '17 at 18:09
  • Duplicate of [Calendar months wrong](http://stackoverflow.com/q/7596760/5221149) – Andreas Apr 26 '17 at 18:11
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html), and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Apr 26 '17 at 23:00

2 Answers2

0

ps.setDate(); asks for a java.sql.Date which is different from java.util.Date

You can try to use

ps.setDate("a", new java.sql.Date(pacijent.getDatum().getTime()));
 or use a timestamp 
ps.setTimestamp(3, new Timestamp(pacijent.getDatum().getTime()));
gdogaru
  • 510
  • 6
  • 24
0

Ok, I've managed to find a way to do this. Here is the correct code. Servlet controller:

Pacijent pacijent = new Pacijent();
try {
   Date dop = new SimpleDateFormat("dd-MM-yyyy").parse(request.getParameter("datum"));
        pacijent.setDatum(dop);
} catch(ParseException e) {
            e.printStackTrace();
}
out.println(pacijent.getDatum());
//not important part of a code ...
//dao.addPacijent(pacijent);

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, new java.sql.Date(pacijent.getDatum().getTime()));
        ps.setString(4, pacijent.getAdresa());
        ps.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
Miomir Pesic
  • 25
  • 1
  • 6