It may be a silly question but I have not been able to solve the problem I have the following methods:
public long contarPedidosMeses(int cedulaCliente, int codigoProducto, Date fechaIni, Date fechaFin){
Query q;
q = em.createNativeQuery("SELECT COUNT(*) FROM pedidos, pedidoproducto WHERE fechaDeSolicitud BETWEEN ? AND ? and pedidos.cedulaCliente=? and pedidoproducto.codigoProducto=? and pedidos.idPedido= pedidoproducto.idPedido");
q.setParameter(1,fechaIni);
q.setParameter(2, fechaFin);
q.setParameter(3, cedulaCliente);
q.setParameter(4, codigoProducto);
long valor=Long.parseLong(q.getSingleResult().toString());
return valor;
}
public long valoresMeses(Date fechaI, Date fechaF, int cedula, int codigo){
long valores= productosf.contarPedidosMeses(cedula, codigo, fechaI, fechaF);
return valores;
}
The problem I have is that I don't know how to pass the Date data when calling the method. So the query doesn't work for me.
I have tried as follows:
controladorProducto.valoresMeses('2016/01/01', '2016/01/31', controladorUsuario.objUsuarioLogin.idUsuario, 1)
Also changing the place of the numbers ie (dd/mm/yyyy), but it doesn't work either. In the MonthValues() method I have changed that it does not receive a Date but a String and I try to convert it with SimpleDateFormat but it sends me a very different date when converting it. I don't know what else to do, I would really appreciate your help.
I work in jsf, JPA and the database in MySQL.
Your parameters must be of type
Date
. If you want to construct type objectsDate
from a string (for your tests perhaps), I recommend usingSimpleDateFormat
:You can save yourself work by creating utility methods:
So the above code reduces to: