0

I want to insert "ID, Benutzername, Passwort, Name, Vorname, Geburtsdatum, IstWirt" into my database, but I have an error in this code:

connect.UpdateUser(Integer.parseInt(textFieldId.getText()), 
    textFieldBenutzername.getText(),
    textFieldPasswort.getText(),
    textFieldName.getText(),
    textFieldVorname.getText(),
    new java.sql.Date (GeburtsdatumDate),
    IstWirtBoolean);

Error Message:

"the constructor Date(Date) in undefined"

I know that I need to have "java.sql.Date" in this request but I really do not have any clue how to avoid the error. I already tried to insert the "textFieldGeburtsdatum.getText()" instead of "GeburtsdatumDate" but with no success.

boolean IstWirtBoolean;
if (textFieldWirt.getText() == "1") {
    IstWirtBoolean = true;
}
if (textFieldWirt.getText() == "0") {
    IstWirtBoolean = false;
}

Date GeburtsdatumDate = new SimpleDateFormat("dd/MM/yyyy").parse(textFieldGeburtsdatum.getText());
connect.UpdateUser(Integer.parseInt(textFieldId.getText()),textFieldBenutzername.getText(),textFieldPasswort.getText(),textFieldName.getText(),textFieldVorname.getText(),
        new java.sql.Date (GeburtsdatumDate),IstWirtBoolean);

database code:

public void UpdateUser(int ID,String Benutzername, String Passwort, String Name,
    String Vorname, Date Geburtsdatum, Boolean IstWirt) throws SQLException { 
        try {
            String query = "Insert into user (ID,Benutzername,Passwort,Name,Vorname,Geburtsdatum,IstWirt) Values (?,?,?,?,?,?,?)";
            PreparedStatement stmt = con.prepareStatement(query);
            stmt.setInt(1, ID);
            stmt.setString(2, Benutzername);
            stmt.setString(3, Passwort);
            stmt.setString(4, Name);
            stmt.setString(5, Vorname);
            stmt.setDate(6, Geburtsdatum);
            stmt.setBoolean(7, IstWirt);
            stmt.executeUpdate();

        }
        catch(Exception e) {
            System.out.println(e);
        }

    }

Edit:

    Date GeburtsdatumDate = new SimpleDateFormat("dd/MM/yyyy").parse(textFieldGeburtsdatum.getText());


connect.UpdateUser(Integer.parseInt(textFieldId.getText()),textFieldBenutzername.getText(),textFieldPasswort.getText(),textFieldName.getText(),textFieldVorname.getText(),new java.util.Date (GeburtsdatumDate),IstWirtBoolean);

here i get the same error message.

The method UpdateUser(int, String, String, String, String, Date, Boolean) in the type DBConnect is not applicable for the arguments (int, String, String, String, String, Date, boolean)
    The constructor Date(Date) is undefined
gandaaflf
  • 17
  • 6
  • 1
    You are comparing strings with `==` which is not what you should be doing. [Use `equals` to compare strings](https://stackoverflow.com/a/513839/243373). – TT. Nov 24 '19 at 22:45

2 Answers2

3

new java.sql.Date (GeburtsdatumDate) causes "the constructor Date(Date) is undefined"

Either GeburtsdatumDate is already a java.sql.Date, in which case you just use it, but that is likely not the case, so it must be some other type of Date, likely a java.util.Date.

To convert a java.util.Date to a java.sql.Date, call the getTime() method:

new java.sql.Date(GeburtsdatumDate.getTime())
Andreas
  • 154,647
  • 11
  • 152
  • 247
0

There is no such constructor defined inside of java.sql.Date class. You should try with Java.util.Date. Date GeburtsdatumDate = new SimpleDateFormat("dd/MM/yyyy").parse(textFieldGeburtsdatum.getText()); also return java Date type not sql date type.