Set a jDateChooser

Asked

Viewed 246 times

0

I’m having a problem doing the insert there in the Postgresql database. The code is apparently ok but time to compile the error.
The error is referring to the type of data I try to insert into the database. The error is:

error: incompatible types: java.util.Date cannot be converted to java.sql.Date

This error appears for all date insertion lines.

modeloIntenrnacao.setDtAdmisPlano((jDateChooserDtAdmissPlano.getDate()));

modeloIntenrnacao.setNome(jTextFieldNomePaciente.getText());
modeloIntenrnacao.setMatricula(Integer.parseInt(jTextFieldMatricula.getText())); 
modeloIntenrnacao.setDtAdmisPlano((jDateChooserDtAdmissPlano.getDate()));
modeloIntenrnacao.setHospOrig(jTextFieldHospOrig.getText());

modeloIntenrnacao.setId(jTextFieldId.getText());
modeloIntenrnacao.setIndicacao(jTextFieldindicacao.getText());
modeloIntenrnacao.setTipoInternac((String)jComboBoxTpInterna.getSelectedItem() );
modeloIntenrnacao.setTipoLeito((String)jComboBoxTpLeito.getSelectedItem());
modeloIntenrnacao.setIdadeAprox(Integer.parseInt(jTextFieldIdadeAprox.getText()));
modeloIntenrnacao.setDataInternac((jDateChooserdDtInternac.getDate()));
modeloIntenrnacao.setDataPrevAlta((jDateChooserdInterAlt.getDate()));
modeloIntenrnacao.setObservacoes(jTextAreaObservacoes.getText());
modeloIntenrnacao.setIdCodInternac(Integer.parseInt(jTextFieldIdInternacao.getText()));
controleInterna.salvaFormInternac(modeloIntenrnacao);
  • Access the link to learn how to make one [mcve] and edit the question by adding it so it is possible to help you.

1 answer

2

The mistake happens because the JDateChooser works with util.Date as date type, and java database persistence always uses sql.Date. Although the second extends the first, it is not guaranteed that every type util.Date be a guy sql.Date, which justifies the error. You need to convert one type to another before saving to your database. In this answer shows how to convert from one type to another.

It is important to note that java provides newer classes and optimized to manipulate dates, if interested, I recommend that you How to migrate from Date and Calendar to the new Java 8 Date API?

  • "it is not guaranteed that every type sql.Date be a guy sql.Date" - I think you’re confused here.

  • @Victorstafusa is so Date that java has now that even confuse me xD

Browser other questions tagged

You are not signed in. Login or sign up in order to post.