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);
}
}