JavaFX - Problem when retrieving a database image

1

I'm using a mysql database, just like I'm using JavaFx with the Scene Builder.

My problem is that I have saved an image in a database and when I try to access it to insert it in an ImageView, I get an error:

  

ERROR: "incompatible types: bufferedimage can not be converted to image"

This is the code:

ConexionBD con = new ConexionBD();
Connection cn = con.Conexion();


Statement prep2;
ResultSet result2;

String consulta2 = "SELECT Nombre,Logo FROM datosempresa";

try {
         prep2 = cn.createStatement();
         result2 = prep2.executeQuery(consulta2);


        if (result2.next()) {
            lblNombreEmpresa.setText(result2.getString(1)); //Funciona
            Image i = null;
            Blob blob = result2.getBlob("Logo");
            i = javax.imageio.ImageIO.read(blob.getBinaryStream()); //ERROR!
            imgFotoEmpresa.setImage(i); //imgFotoEmpresa = ImageView
        }
    } catch (SQLException ex) {
        Logger.getLogger(PrincipalController.class.getName()).log(Level.SEVERE, null, ex);
        System.out.println(ex.getMessage());
    } 
    
asked by AlexCs 03.04.2018 в 04:10
source

1 answer

1

Finally I have the answer.

In the line of code where I have commented error simply change it by:

i = SwingFXUtils.toFXImage(javax.imageio.ImageIO.read(blob.getBinaryStream()), null);

This is to capture an image saved in a MYSQL database (BLOB), and insert the image in ImageView .

    
answered by 05.04.2018 / 02:06
source