Solution 1 :

Your Java Server Faces graphicImage tag

<h:graphicImage value="#{mbBrowse.versicherungskarte}" style="height:100px;width:200px"/>

when delivered to your users’s browsers becomes

<img src="theContentOfYourBLOB" style="height:100px;width:200px"/>

But a well-formed tag looks like this:

<img src="" style="height:100px;width:200px" />

In other words, you can’t just pour a BLOB into that tag and expect it to work. You need a URL for the image there.

You can store the image into a uniquely named file in your server’s static object filesystem (the filesystem from which you serve .css, .js, and other image files). Then you will put the URL for the file as the value of the <h:graphicImage … > tag.

Or, you can use a so-called Data URL to represent your image inline. In that case you’ll put a URL something like this into the value parameter.


Here are some suggestions about how to do that. You basically must encode the binary data in the BLOB as Base64 and prepend data:image/jpeg; to it.

Pro tip Many web app designers don’t store images in database BLOBS, but rather store them in a file system, and store their URLs in database strings. Why? Database servers often become a performance bottleneck when an application scales up. If you load them with images, they’ll become even bigger bottlenecks.

Problem :

I am quite unexperienced when it comes to web development, so I am sorry to have uploaded so much possibly unnecessary code, but i guessed better too much than too little information..

I want to display a BLOB i got from a connected MySQL Database. The BLOB is not displayed properly as an image. Instead i get it like this:
enter image description here

In MySQl the BLOBs all are jpg format. I retrieve them from MySQL database with other Strings and Integers. The displaying of them works fine. I have no idea why it is not properly displayed. Hopefully you can help me!

    public class MbBrowse implements Serializable {

    private static final long serialVersionUID = 1L;

    final String SQL_SELECT = "select Vorname, Nachname, Geburtsdatum,Email, Telefon, Krankenkasse, Versicherungskarte, Einlieferung, Entlassung, Station,Diagnose, Therapie, Medikamente from stammdaten";
    final String SQL_SELECT2 = "select Vorname, Nachname, Diagnose, Therapie, Medikamente from stammdaten";

    private boolean connected = false;
    private boolean prevButtonDisabled = true;
    private boolean nextButtonDisabled = true;

    private Util util = new Util();

    private Connection con = null;
    private Statement stm = null;
    private ResultSet rs = null;

    private int telefon = 0;
    private String nachname = "";
    private String vorname = "";
    private Date geburtsdatum = new Date(0L);
    private String krankenkasse = "";
    private Blob versicherungskarte;
    private String email = "";

public int getTelefon() {
        return telefon;

    public void setTelefon(int n) {
        telefon = n;

    public String getNachname() {
        return nachname;

    public void setNachname(String s) {
        nachname = s;

    public String getVorname() {
        return vorname;

    public void setVorname(String s) {
        vorname = s;

    public String getKrankenkasse() {
        return krankenkasse;

    public void setKrankenkasse(String s) {
        krankenkasse = s;


    public String getEmail() {
        return email;

    public void setEmail(String s) {
        email = s;


    public java.util.Date getGeburtstag() {
        return geburtsdatum;

    public void setGeburtstag(java.util.Date dt) {
        if (dt != null)
            geburtsdatum = new Date(dt.getTime());
            geburtsdatum = new Date(0L);

    public Blob getVersicherungskarte() {
        return versicherungskarte;

    public void setVersicherungskarte(Blob karte) {
        karte = versicherungskarte;

    public boolean getPrevButtonDisabled() {
        return prevButtonDisabled;

    public boolean getNextButtonDisabled() {
        return nextButtonDisabled;

    public boolean getConnected() {
        return connected;

    public void setConnected(boolean b) {
        connected = b;


    private void showData() throws SQLException {




     * Verbindung zur Datenbank herstellen und disconnect button und browse buttons
     * freigeben
     * @param ae ActionEvent
    public void connect(ActionEvent ae) {

        // out.println( "connect()..." );

        if (util != null)
            con = util.getCon();
        if (con != null) {
            try {
                stm = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
                rs = stm.executeQuery(SQL_SELECT);
                if (rs.first())
                connected = true;
                prevButtonDisabled = false;
                nextButtonDisabled = false;
            } catch (Exception ex) {
                        new FacesMessage(FacesMessage.SEVERITY_ERROR, "SQLException", ex.getLocalizedMessage()));
                out.println("Error: " + ex);
        } else {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    "Exception", "Keine Verbindung zur Datenbank (Treiber nicht gefunden?)"));
            out.println("Keine Verbingung zur Datenbank");



            <h:outputStylesheet library="css" name="myCss.css" />
            <meta charset="utf-8" />


<h:body style="background-color:#D5D8DC;">



            <h:graphicImage library="img" name="logo.png" />

            <font face="Verdana" size="5">Stammdaten und Behandlungsdaten</font><br />

            <h:panelGrid id="panel" columns="4" border="0" cellpadding="10"
                cellspacing="1" styleClass="text">

    <h:inputText label="Vorname" 
                    value="#{mbBrowse.vorname}" maxlength="45" />   

     <h:inputText label="Name" value="#{mbBrowse.nachname}"
                    maxlength="45" /> 

    <h:inputText id="id_geburtstag" label="Geburtstag" maxlength="10"
                    <f:convertDateTime locale="de_DE" pattern="dd.MM.yyyy" type="date"
                        dateStyle="short" timeZone="MET" />
    <h:inputText label="Telefon" maxlength="10"
                    value="#{mbBrowse.telefon}" />
    <h:inputText label="Krankenkasse" maxlength="60"
                    value="#{mbBrowse.krankenkasse}" /> 
    <h:inputText label="Email" maxlength="60" value="#{}" /> 

     <h:graphicImage value="#{mbBrowse.versicherungskarte}"
                    style="height:100px;width:200px" />



                <h:commandButton value="&lt;&lt;"
                    disabled="#{mbBrowse.prevButtonDisabled}" title="Zurück blättern"
                    style="font-size: 130%;" actionListener="#{mbBrowse.prev}" />

                <h:commandButton value="&gt;&gt;"
                    disabled="#{mbBrowse.nextButtonDisabled}" title="Vorwärts blättern"
                    style="font-size: 130%;" actionListener="#{}" />

                <h:commandButton value="Connect" disabled="#{mbBrowse.connected}"
                    actionListener="#{mbBrowse.connect}" style="font-size: 130%;" />




Comment posted by Shadow

Or you have a separate jsp page that serves the content of the blob as image based on a get parameter, so you do not have to save its content to the file system.