How to make a report or query by range of dates in java?

0

Good day, I would like to know how to make a report using range of dates (start date, end date) in the GUI Method ... Any help?

- process

public class GestionReporteVentas implements ReporteVentasInterface{

@Override
public ArrayList<ReporteVentas> listado() {
    ArrayList<ReporteVentas> lista = new ArrayList<ReporteVentas>();
    ResultSet rs = null; // tipo de resultado
    Connection con = null;
    PreparedStatement pst = null;
    try {
       con = MySQLConexion.getConexion(); 
       String sql = "{CALL usp_reporte1}"; // sentencia sql

       pst = con.prepareStatement(sql);
       // parámetros según la sentencia        

       rs = pst.executeQuery(); // tipo de ejecución

       // Acciones adicionales en caso de consultas
       while (rs.next()){
           ReporteVentas rv = new ReporteVentas();
           rv.setNumvta(rs.getString(1));
           rv.setFechavta(rs.getString(2));
           rv.setNomproducto(rs.getString(3));
           rv.setNomvendedor(rs.getString(4));
           rv.setMontoventa(rs.getDouble(5));
           lista.add(rv);
       }
    } catch (Exception e) {
       System.out.println("Error en la sentencia " + e.getMessage());
    } finally {
      try {
          if (pst != null) pst.close();
          if (con != null) con.close();
       } catch (SQLException e) {
          System.out.println("Error al cerrar ");
       }
    }
   return lista;
}}

- IN THE GUI

void listarReporte(){


    // llamar la gestion
    GestionReporteVentas gr = new GestionReporteVentas();

    ArrayList<ReporteVentas> lista = gr.listado();

    if (lista == null){
        txtListado.setText("LISTA VACIA");
    }else {
        txtListado.setText("NumVenta\tFechaV\tNomProducto\tNomVendedor\tMonto\n");
        for (ReporteVentas rv : lista){
            txtListado.append(rv.getNumvta() + "\t" + rv.getFechavta() + "\t" +
                    rv.getNomproducto() + "\t" + rv.getNomvendedor() + "\t" + rv.getMontoventa() +"\n");
    }

} }

    
asked by Pedro Valverde 03.06.2017 в 03:28
source

2 answers

0

You have two alternatives.

1) Change the stored procedure to pass from java the deadlines as a parameter and that the stored procedure returns only what needs to be included in the report.

O 2) Assuming that the date we are talking about is the sale date. When the result set is traversed, only add to lista those rv where the rv.getFechaventa() are greater than or equal to the start date and less than or equal to the end date.

    
answered by 03.06.2017 в 07:07
0

You can try passing your date to LocalDate which is the new class to manipulate dates in Java 8, then do the verification if the date of the list is within your desired range, code similar to this:

java.time.format.DateTimeFormatter formateador = java.time.format.DateTimeFormatter.ofPattern("dd/MM/yyyy"); //el formato de tu fecha
java.time.LocalDate fechaInicio = java.time.LocalDate.parse("01/06/2017",formateador);//debes reemplazar el String por la fecha de inicio de tu GUI
java.time.LocalDate fechaTermino = java.time.LocalDate.parse("10/06/2017",formateador);//debes reemplazar el String por la fecha de termino de tu GUI

for (ReporteVentas rv : lista)
{   
    java.time.LocalDate fecha = java.time.LocalDate.parse(rv.getFechavta());
    if(     (fecha.isBefore(fechaInicio) || fecha.isEqual(fechaInicio))
                                         &&
            (fecha.isAfter(fechaTermino) || fecha.isEqual(fechaTermino) )                            
     )
    {
        txtListado.append(rv.getNumvta()+"\t"+rv.getFechavta()+"\t"+rv.getNomproducto()+"\t"+rv.getNomvendedor()+ "\t"+rv.getMontoventa()+"\n");        
    }
}
    
answered by 03.06.2017 в 16:52