Image object from BLOB?

Viewed 23

I'm having trouble getting an image object from a BLOB stored in a mySQL table.

Table "images" has values: "idImage" (int), "idArticle" (int), "image" (BLOB).

This is a method from the database access object:

public MyImage findImageById(int idImage) {
    executor = new DbOperationExecutor();
    sql = "SELECT * FROM images WHERE idImage = '" + idImage + "';";
    dbOperation = new ReadOperation(sql);
    ResultSet rs = executor.executeOperation(dbOperation).getResultSet();
    MyImage i = new MyImage();
    try {
        rs.next();
        if (rs.getRow() == 1) {
            i.setIdImage(rs.getInt("idImage"));
            i.setIdArticle(rs.getInt("idArticle"));
            i.setImage(ImageBusiness.getInstance().getImageByBlob(rs.getBlob("image")));
        }
        return immagine;
    } catch (SQLException e) {...} catch (NullPointerException e) {...}
    finally {
        executor.close(dbOperation);
    }
    return null;
}

Method "getImageByBlob" in ImageBusiness class is:

public Image getImageByBlob(Blob blob){
    try {
        InputStream stream = blob.getBinaryStream();
        BufferedImage image = ImageIO.read(stream);
        return image;
    } catch (IOException | SQLException e) {
        System.out.println("Blob-Image conversion Error: " + e.getMessage());
        return null;
    }
}

When I debug this code I notice that Image.read(stream) return null, why?? Help me please.

1 Answers

Only one "return" statement ever allowed in a method!.

public Image getImageByBlob(Blob blob)throws IOException,SQLException {
        InputStream stream = blob.getBinaryStream();
        BufferedImage image = ImageIO.read(stream);
return image;
}
Related