Results 1 to 6 of 6
  1. #1
    shaida is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default How to Retrieve Images from Mysql

    Hi dear.
    I am having the problem to fetching jpg images from db. I am using following code
    This is JSF page code....

    <h:panelgrid columns="2">
    <h:outputText value="ID"></h:outputText>
    <h:inputText id="id" value="#{imagebean.id}"></h:inputText>
    <h:graphicImage value="#{dataitem1.fileuplad}"></h:graphicImage>
    <h:commandButton value="ImageLoad" action="#{imagebean.crop }">
    </h:commandButton>

    </h:panelGrid>

    This bean and method from where i am trying to fetch images from db


    private String id;
    FileUpload fileuplad;

    public String getId() {
    return id;
    }
    public void setId(String id) {
    this.id = id;
    }
    public FileUpload getFileuplad() {
    return fileuplad;
    }
    public void setFileuplad(FileUpload fileuplad) {
    this.fileuplad = fileuplad;
    }
    public String crop(){
    FacesContext facescontex =FacesContext.getCurrentInstance();
    HttpServletResponse resp =(HttpServletResponse)facescontex.getExternalConte xt().getResponse();

    String val =getId();
    PreparedStatement ps = null;
    ResultSet rs = null;
    Connection con = null;
    try{
    ConnectionNew conn = new ConnectionNew();
    con =conn.getConnection();
    String qry="select image from pictures where id='"+val+"'";
    ps=con.prepareStatement(qry);
    System.out.println("Query====="+ps.toString());
    rs=ps.executeQuery();
    while(rs.next()){

    Blob blb=rs.getBlob("image");
    byte [] bdata = blb.getBytes(1, (int)blb.length());
    resp.reset();
    resp.setContentType("image/jpg");
    resp.getOutputStream().write(bdata.length);
    resp.getOutputStream().write(bdata);


    }
    resp.getOutputStream().flush();
    ps.close();
    con.close();
    rs.close();
    resp.getOutputStream().close();
    }
    catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
    }

    return null;
    }

    In this i am passing id vlue into db. and cheking the id after that the image will retrive from db..
    Now problm is , I am not able to set image in graphics..
    So plese any guide.. where is main problm and how display image
    If any one please help me.. I'll be thanks to you

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default Re: How to Retrieve Images from Mysql

    What HTML code do you end up with on the client to reference the image?
    That is, what does the img tag look like?

    As an aside, there's no point using a PreparedStatement if you are simply going to concatenate the SQL together and not bind any variables.
    ALso, for scalability purposes, you should getBinaryStream() rather than getBlob() and stream that directly to the response output stream. No point loading the entire image into memory just for the purpose of streaming it to the client.
    And finally, those resources (connection, statement, streams etc) should be closed in a finally block.

  3. #3
    shaida is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default Re: How to Retrieve Images from Mysql

    Thanks for your replies ...

    I am working with JSF for viewing purpose ,so just following are the Jsf coding where the images will display after retrieving from db. I have also changes in bean as per ur suggestion but still image is not displaying. How to set our image into jsf front end. Kindly help

    <body>
    <f:view>
    <h:form>
    <h:panelGrid columns="2">
    <h:outputText value="ID"></h:outputText>
    <h:inputText id="id" value="#{imagebean.id}"></h:inputText>

    <h:outputText value="Images"></h:outputText>
    <h:graphicImage value="#{imagebean.fileuplad}"></h:graphicImage>


    <h:commandButton value="ImageLoad" action="#{imagebean.crop }"></h:commandButton>

    </h:panelGrid>
    </h:form>
    </f:view>
    </body>

    Following are the java bean code....

    public class Imagebean {

    private String id;
    FileUpload fileuplad;

    public String getId() {
    return id;
    }
    public void setId(String id) {
    this.id = id;
    }
    public FileUpload getFileuplad() {
    return fileuplad;
    }
    public void setFileuplad(FileUpload fileuplad) {
    this.fileuplad = fileuplad;
    }
    public String crop(){
    FacesContext facescontex =FacesContext.getCurrentInstance();
    HttpServletResponse resp =(HttpServletResponse)facescontex.getExternalConte xt().getResponse();

    String val =getId();
    PreparedStatement ps = null;
    ResultSet rs = null;
    Connection con = null;
    try{
    ConnectionNew conn = new ConnectionNew();
    con =conn.getConnection();
    String qry="select image from pictures where id='"+val+"'";
    ps=con.prepareStatement(qry);
    System.out.println("Query====="+ps.toString());
    rs=ps.executeQuery();
    while(rs.next()){
    Blob blb=rs.getBlob("image");
    BufferedInputStream bfs = new BufferedInputStream(blb.getBinaryStream());
    resp.getOutputStream();
    }
    catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
    }
    finally
    {
    try {
    resp.getOutputStream().flush();
    resp.getOutputStream().close();
    ps.close();
    con.close();
    rs.close();

    }
    catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    return null;

    }



    }

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default Re: How to Retrieve Images from Mysql

    No, what does the HTML look like on the client, specifically the img tag?
    I don't want to see the JSF stuff, I want to see what the client is actually requesting.

  5. #5
    shaida is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default Re: How to Retrieve Images from Mysql

    Thanks for your reply..

    My Client is passing one id value. In the base of id only one image will retrieve from db and display it into the client side.
    I hope

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default Re: How to Retrieve Images from Mysql

    Gah!
    It's a simple request...what does the img tag look like?
    If it's not an img tag, then what is the submit target?

    View Source in the browser on the page that you are doing this on and show me what it has for this functionality.

Similar Threads

  1. Retrieve file
    By mine0926 in forum NetBeans
    Replies: 2
    Last Post: 06-03-2011, 04:47 AM
  2. Replies: 0
    Last Post: 12-16-2010, 05:14 PM
  3. Replies: 4
    Last Post: 12-16-2010, 04:32 PM
  4. Retrieve value from JTable
    By mcdhappy80 in forum AWT / Swing
    Replies: 5
    Last Post: 12-01-2010, 04:35 AM
  5. MySQL/JDBC Mysql query output
    By thelinuxguy in forum Advanced Java
    Replies: 4
    Last Post: 02-13-2009, 01:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •