0

I am getting exception while fatch image from database and display image on screen. Please help me to solve this error..

int questionid ;
Connection conn = null;
conn= new DBFunction().getconnect();
Statement stmt = conn.createStatement();

questionid=4;

try
{  

    conn.setAutoCommit (false);  

    // get the image from the database
    Blob img ;
    byte[] imgData = null ;

    String  req = "Select img From questionlist Where queid = " + questionid ;
    ResultSet rset  = stmt.executeQuery ( req ); 

    while (rset.next ())
    {    
      img = rset.getBlob("img");
      imgData = img.getBytes(1,(int)img.length());
    }    
        // display the image
       response.reset();
       response.setContentType("image/gif");

       OutputStream o = response.getOutputStream();

        o.write(imgData);
        o.flush(); 
        o.close();
}
    catch (Exception e)
    {
      e.printStackTrace();

    }

Let me know where am i wrong ?

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
Chirag
  • 1
  • Possible duplicate of http://stackoverflow.com/questions/1776142/getoutputstream-has-already-been-called-for-this-response – OO7 Oct 15 '14 at 17:48

1 Answers1

0

It is not the better practice to write the java codes inside jsp as jsp calls printWriter by default. Scriptlets are not advised over decades . try moving the code into a java file , preferably a servlet.

Also it is not better option to store images into Database , try to use the diskspace instead. As far as your question try out.clear()

see How to avoid Java code in JSP files?

Community
  • 1
  • 1
Santhosh
  • 8,181
  • 4
  • 29
  • 56