0

I want to get the selected row from my JTable and update my database with the information, which was written in the textfields (from the user).

Error message:

"Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.sql.Date"

Code:

   int i = tableUsers.getSelectedRow();
   if(i>=0) {
   try {
        boolean IstWirtBoolean;
        Date GeburtsdatumDate;
        try {
        GeburtsdatumDate = new SimpleDateFormat("yyyy-MM-dd").parse(textFieldGeburtsdatum.getText());
        if (textFieldWirt.getText().equals("true")) {
            IstWirtBoolean = true;
            System.out.println((java.sql.Date)(model.getValueAt(i, 5)));
            connect.UpdateUser((Integer.parseInt(model.getValueAt(i, 0).toString())),textFieldBenutzername.getText(), textFieldPasswort.getText(), 
            textFieldName.getText(), textFieldVorname.getText(), new java.sql.Date(GeburtsdatumDate.getTime()), 
            IstWirtBoolean);
            }
    } catch (SQLException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }
    } catch (ParseException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }

Error at:

"new java.sql.Date(GeburtsdatumDate.getTime())"

How can I solve the error? I already transformed the String into a Date and use the java.sql.Date. What I am doing wrong?

Edit: database code:

public void UpdateUser(int ID,String Benutzername,String Passwort,String Name,String Vorname,Date Geburtsdatum,Boolean IstWirt) throws SQLException { 
        try {
            String query = "Update user set Benutzername=?,Passwort=?,Name=?,Vorname=?,Geburtsdatum=?,IstWirt=? WHERE ID=?";
            PreparedStatement stmt = con.prepareStatement(query);
            stmt.setString(1, Benutzername);
            stmt.setString(2, Passwort);
            stmt.setString(3, Name);
            stmt.setString(4, Vorname);
            stmt.setDate(5, Geburtsdatum);
            stmt.setBoolean(6, IstWirt);
            stmt.setInt(7, ID);
            stmt.executeUpdate();
        }
        catch(Exception e) {
            System.out.println(e);
        }

    }
gandaaflf
  • 17
  • 6
  • 2
    Are you sure it isn't `System.out.println((java.sql.Date)(model.getValueAt(i, 5)));`? – Amongalen Nov 25 '19 at 13:33
  • Yes it is there too. It is the same error message. I just inserted the System.out.println to check, where the error come from. – gandaaflf Nov 25 '19 at 13:36
  • Please don't use `Date` and `SimpleDateFormat`. They're like C90 tape – outdated long ago... – MC Emperor Nov 25 '19 at 13:38
  • can you share debug screenshot , what is value of model.getValueAt(i, 5) before casting it to java.sql.Date – Samir Nov 25 '19 at 13:39
  • 1
    1) Please read about java naming conventions. Using say "Benutzername" for a variable makes this almost unreadable to seasoned java programmers. 2) Dont repeat the type of some variable in its name. It adds no information. 3) Use `SimpleDateFormat.parse(String)` to convert a `Date` to `String` – Gyro Gearless Nov 25 '19 at 13:47
  • What value are you parsing in `textFieldGeburtsdatum.getText()`? Does it really have the format `"yyyy-MM-dd"`? – GameDroids Nov 25 '19 at 13:48
  • Geburtsdatum = Birthdate. It has this format - @Gyro Gearless I already using it in my code. GeburtsdatumDate = new SimpleDateFormat("yyyy-MM-dd").parse(textFieldGeburtsdatum.getText()); – gandaaflf Nov 25 '19 at 13:49
  • 2
    Maybe [this SO answer](https://stackoverflow.com/a/18615191/896249) can help you. Instead of parsing the text from your `TextField` into a `Date`, getting the `timeInMilis` and converting it to a `sql.Date` you can create a `sql.Date` directly from your String. – GameDroids Nov 25 '19 at 14:07
  • Thank you very much! You are my hero @GameDroids – gandaaflf Nov 25 '19 at 14:17

0 Answers0