Load image from MySQL to ImageView JavaFX

0

I need to upload an image that is stored in MySQL This table called conductors contains these columns:

I managed to save them but now I need to obtain them when the user searches for that driver by his personal identity number.

I have obtained the other information but I do not know how to get the photo here I show you the code.

public void ConsultarConductor(String cedulaC, Connection claseJava) {

    try {

        PreparedStatement stmt = claseJava.prepareStatement("SELECT nombre, cedula, fechanac, lugarnac, nacionalidad, tiposangre, padecimiento, numerolicencia, tipolicen, licenciaexpedida, licenciaexpira, numempleado, ruta, imagen FROM conductores WHERE cedula = ? ");
        stmt.setString(1, cedulaC);

        ResultSet result = stmt.executeQuery();
        while (result.next()) {
            NombreC.setText(result.getString(1));
            CedC.setText(result.getString(2));
            FechaNac.setValue(result.getDate(3).toLocalDate());
            LugarNac.setText(result.getString(4));
            Nacionalidad.setText(result.getString(5));
            TipoSangre.setValue(result.getString(6));
            Padecimiento.setText(result.getString(7));
            NumLic.setText(result.getString(8));
            TipoLic.setValue(result.getString(9));
            LicExpedida.setValue(result.getDate(10).toLocalDate());
            LicExpira.setValue(result.getDate(11).toLocalDate());
            NumEmpleado.setText(result.getString(12));
            txt_rutafoto.setText(result.getString(13));
            img_verfoto.setText(result.getString(14));
        }
        Actualizar.setDisable(false);
    } catch (SQLException ex) {
        ex.printStackTrace();
    }
}
    
asked by Riddick 08.11.2017 в 16:32
source

1 answer

0

What you have to do is get the Blob object from the image column, then convert it to an array of bytes [] , the latter you use to create the Image to be displayed in the ImageView of JavaFX.

ResultSet result = stmt.executeQuery();

while (result.next()) {
    //...

    byte byteImage[] = null;

    // obtener la columna imagen, luego el arreglo de bytes 
    Blob blob = result.getBlob("imagen");
    byteImage = blob.getBytes(1, (int) blob.length());

    // crear el Image y mostrarlo en el ImageView
    Image img = new Image(new ByteArrayInputStream(byteImage));
    imageView = new ImageView(img);
}

Remember to declare and add the ImageView to the Scene of your application.

    
answered by 13.11.2017 в 16:33