Set a jDateChooser [closed]

0

I have a problem with doing insert there in the PostgreSQL database. The code is apparently ok but at the time of compiling the error.
The error is related to the type of data that I try to insert into the database. The error it gives is:

  

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

This error appears for all date insertion rows.

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);
    
asked by anonymous 19.10.2017 / 21:42

1 answer

2

The error happens because JDateChooser works with util.Date as date type, and database persistence in java always uses sql.Date . Although the second extends the first, it is not guaranteed that every type util.Date is a type 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's important to note that java comes with newer and more optimized classes to manipulate dates, so if you're interested, I recommend How to migrate from Date and Calendar to the new date API in Java 8?

    
20.10.2017 / 16:13