0
The idea is to consult the data by dates in the database (Mysql): I have 2 fields JXDatePicker
and I want the return of the data between the initial date and the final date. In Mysql, the field date is in the format DATE
(example: 2017-10-11
) and in Java the field date is as java.util.Date
.
I tried to do the conversion but it does not filter the dates and does not return the result. What is wrong?
Consultation method:
public List<FinancasModel> listaPorDatas(Date dataInicio, Date dataFinal) throws ParseException {
System.out.println("Datas " + dataInicio + " " + dataFinal);
String sql = "select * from financas where dataPagamento between ? and ? ";
List<FinancasModel> listaPorDatas = new ArrayList<>();
try {
PreparedStatement ps = getCon().prepareStatement(sql);
ps.setDate(1, new java.sql.Date(dataInicio.getTime()));
ps.setDate(2, new java.sql.Date(dataFinal.getTime()));
ResultSet rs = ps.executeQuery();
if (rs != null) {
while (rs.next()) {
FinancasModel fm = new FinancasModel();
fm.setCodFinancas(rs.getInt(1));
fm.setValor(rs.getDouble(2));
fm.setDataPagamento(rs.getDate(3));
fm.setDataCadastro(rs.getDate(4));
fm.setTipo(rs.getString(5));
fm.setDescricao(rs.getString(6));
fm.setObservacao(rs.getString(7));
listaPorDatas.add(fm);
}
} else {
return null;
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Erro: " + e);
}
return null;
}
Method of listing the query:
public void listaPorDatas(Date dataInicio, Date dataFinal) throws ParseException {
FinancasDAO cd = new FinancasDAO(con);
DefaultTableModel model = (DefaultTableModel) tbl_movimentacao.getModel();
model.setNumRows(0);
List<FinancasModel> listaPorDatas = cd.listaPorDatas(dataInicio, dataFinal);
double despesa = 0;
double renda = 0;
for (FinancasModel fb : listaPorDatas) {
if (fb.getTipo().equals("DESPESA")) {
despesa += fb.getValor();
} else {
renda += fb.getValor();
}
model.addRow(new Object[]{
fb.getCodFinancas(),
Utils.ConverteDouble(fb.getValor()),
fb.getDataPagamento(),
fb.getTipo(),
fb.getDescricao(),
fb.getObservacao()
});
}
txt_despesa.setText(Utils.ConverteDouble(despesa));
txt_renda.setText(Utils.ConverteDouble(renda));
txt_saldo.setText(Utils.ConverteDouble(renda - despesa));
}
Errors returned:
Dates Fri Sep 01 00:00:00 BRT 2017 Sun Oct 01 00:00:00 BRT 2017 Exception in thread "AWT-Eventqueue-0" java.lang.Nullpointerexception at br.com.smartadvocacy.telas.Moving.listaPorData(Moving.java:44) at br.com.smartadvocacy.telas.Movement.Buscarmouseclicked(Motion.java:348) at br.com.smartadvocacy.telas.Moving.access$200(Moving.java:25) at br.com.smartadvocacy.telas.Movement$4.mouseClicked(Movement.java:252) at java.awt.Awteventmulticaster.mouseClicked(Awteventmulticaster.java:270) at java.awt.Component.processMouseEvent(Component.java:6536) at javax.swing.jcomponent.processMouseEvent(Jcomponent.java:3324) at java.awt.Component.processEvent(Component.java:6298) at java.awt.Container.processEvent(Container.java:2236) at java.awt.Component.dispatchEventImpl(Component.java:4889) at java.awt.Container.dispatchEventImpl(Container.java:2294) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.Lightweightdispatcher.retargetMouseEvent(Container.java:4888) at java.awt.Lightweightdispatcher.processMouseEvent(Container.java:4534) at java.awt.Lightweightdispatcher.dispatchEvent(Container.java:4466) at java.awt.Container.dispatchEventImpl(Container.java:2280) at java.awt.Window.dispatchEventImpl(Window.java:2746) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.Eventqueue.dispatchEventImpl(Eventqueue.java:758) at java.awt.Eventqueue.access$500(Eventqueue.java:97) at java.awt.Eventqueue$3.run(Eventqueue.java:709) at java.awt.Eventqueue$3.run(Eventqueue.java:703) at java.security.Accesscontroller.doPrivileged(Native Method) at java.security.Protectiondomain$Javasecurityaccessimpl.doIntersectionPrivilege(Protectiondomain.java:80) at java.security.Protectiondomain$Javasecurityaccessimpl.doIntersectionPrivilege(Protectiondomain.java:90) at java.awt.Eventqueue$4.run(Eventqueue.java:731) at java.awt.Eventqueue$4.run(Eventqueue.java:729) at java.security.Accesscontroller.doPrivileged(Native Method) at java.security.Protectiondomain$Javasecurityaccessimpl.doIntersectionPrivilege(Protectiondomain.java:80) at java.awt.Eventqueue.dispatchEvent(Eventqueue.java:728) at java.awt.Eventdispatchthread.pumpOneEventForFilters(Eventdispatchthread.java:201) at java.awt.Eventdispatchthread.pumpEventsForFilter(Eventdispatchthread.java:116) at java.awt.Eventdispatchthread.pumpEventsForHierarchy(Eventdispatchthread.java:105) at java.awt.Eventdispatchthread.pumpEvents(Eventdispatchthread.java:101) at java.awt.Eventdispatchthread.pumpEvents(Eventdispatchthread.java:93) at java.awt.Eventdispatchthread.run(Eventdispatchthread.java:82)
You can tell which version of Java you use?
– José
Java: 8, Update 144
– Dayane Mendes
Although your question is how to convert date object types, your problem does not seem to be linked to conversion. You had a
Datas Fri Sep 01 00:00:00 BRT 2017 Sun Oct 01 00:00:00 BRT 2017 Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at br.com.smartadvocacy.telas.Movimentacao.listaPorDatas(Movimentacao.java:44) at
. What does not fit with date type X or Y, but a null object. Check what you have on line 44 of the Movement class.– romarcio