1

Tengo esta consulta en JavaFX para generar un pdf con iText, Pero me salta un error de que el documento no tiene paginas.

SQL:

SELECT fechakm, unidad, conductor, kmentrada, kmsalida, kmrecorrido FROM kilometraje WHERE ( unidad = ? AND  between fechakm = ? AND fechakm = ? )

Este es mi codigo Java:

public void Reporte1(String unidad, String fecha1, String fecha2, Connection connection){
    Document documento = new Document();
    try {
        String ruta = System.getProperty("user.home");
        PdfWriter.getInstance(documento, new FileOutputStream(ruta + "/Desktop/ReporteKmRecorridoPorUnidad.pdf"));
        documento.open();

        PdfPTable tabla = new PdfPTable(6);
        tabla.addCell("Fecha");
        tabla.addCell("Unidad");
        tabla.addCell("Conductor");
        tabla.addCell("KmEntrada");
        tabla.addCell("KmSalida");
        tabla.addCell("KmRecorrido");

        try {
          Connection con = DriverManager.getConnection("jdbc:mysql://localhost/dbase", "USER","PASS");
            Conexion.GetDatabaseConnection();
            PreparedStatement pst = con.prepareStatement("SELECT fechakm, unidad, conductor, kmentrada, kmsalida, kmrecorrido FROM kilometraje WHERE ( unidad = ? AND  between fechakm = ? AND fechakm = ? )");
            pst.setString(1, unidad);
            pst.setString(2, fecha1);
            pst.setString(3, fecha2);

            ResultSet rs = pst.executeQuery();
            if (rs.next()) {
                do {
                    tabla.addCell(rs.getString(1));
                    tabla.addCell(rs.getString(2));
                    tabla.addCell(rs.getString(3));
                    tabla.addCell(rs.getString(4));
                    tabla.addCell(rs.getString(5));
                    tabla.addCell(rs.getString(6));
                } while (rs.next());
                documento.add(tabla);
            }

        }catch(DocumentException | SQLException e)  {

        } documento.close();
            Alert mensaje = new Alert(Alert.AlertType.CONFIRMATION);
            mensaje.setTitle("Tramesa");
            mensaje.setContentText("Reporte Generado Correctamente");
            mensaje.setHeaderText("Reporte");
            mensaje.show();
    } catch(DocumentException | FileNotFoundException e) {

    }
}

@FXML
public void AccionReporteRecoPorUnidad() {
    try{
        if(cb_unidad.getSelectionModel().getSelectedIndex() == -1){
            Alert alerta1 = new Alert(Alert.AlertType.ERROR);
            alerta1.setTitle("Tramesa");
            alerta1.setHeaderText("Campo vacio");
            alerta1.setContentText("Debe seleccionar una Unidad");
            alerta1.showAndWait();
        }else if(dp_fechaUni1.getValue()==null){
            Alert alerta1 = new Alert(Alert.AlertType.ERROR);
            alerta1.setTitle("Tramesa");
            alerta1.setHeaderText("Campo vacio");
            alerta1.setContentText("Debe seleccionar una Fecha Inicial");
            alerta1.showAndWait();
        }else if(dp_fechaUni2.getValue()== null){
            Alert alerta1 = new Alert(Alert.AlertType.ERROR);
            alerta1.setTitle("Tramesa");
            alerta1.setHeaderText("Campo vacio");
            alerta1.setContentText("Debe seleccionar una Fecha Final");
            alerta1.showAndWait();
        }else{
            SimpleDateFormat formato = new SimpleDateFormat("dd-MM-yyyy");
            java.lang.String fechaInicio = formato.format(java.sql.Date.valueOf(dp_fechaUni1.getValue()));
            java.lang.String fechaFin = formato.format(java.sql.Date.valueOf(dp_fechaUni2.getValue()));
            Conexion.GetDatabaseConnection();
            Reporte1(cb_unidad.getSelectionModel().getSelectedItem().toString(), fechaInicio, fechaFin, Conexion.GetDatabaseConnection() );

        }


    }catch(Exception e){
        e.printStackTrace();
    }

}

Aqui mi tabla: Tabla MySQL

2 respuestas 2

1

Al parecer tu consulta es errónea por como utilizaste BETWEEN. Su sintaxis es la siguiente:

... WHERE fecha BETWEEN valor1 AND valor2;

Por lo tanto, tu consulta debería ser la siguiente:

SELECT fechakm, unidad, conductor, kmentrada, kmsalida, kmrecorrido FROM kilometraje WHERE unidad = ? AND fechakm BETWEEN ? AND ?
1
  • Gracias ya lo habia solucionado.
    – Riddick
    el 20 feb. 2019 a las 20:38
0

Lo tengo!, Solo era un mal orden en la consulta.

SELECT fechakm, unidad, conductor, kmentrada, kmsalida, kmrecorrido FROM kilometraje WHERE  unidad = ? AND fechakm between   ? AND  ?

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.