Hi,

I store images in my MYSQL database. I need to retrieve that image to my jsp along with datas.Response.write() function only displays the picture and not the data. How to do that?

Hi
i use the following code to retrieve image which displays only image. not any other datas.

Connection connection = null;
  //login is the name of the database
  String connectionURL = "jdbc:mysql://localhost:3306/login";
 
  ResultSet rs = null;
 
  PreparedStatement psmnt = null;
 
  InputStream sImage;
  try 
  {
  Class.forName("com.mysql.jdbc.Driver").newInstance(); 
  connection = DriverManager.getConnection(connectionURL, "root", "root");  
//Student is the table name
  psmnt = connection.prepareStatement("SELECT image FROM student WHERE id = ?");
//In id "6" i have the image.
  psmnt.setString(1, "6"); 
  rs = psmnt.executeQuery();
  if(rs.next()) 
  {
  byte[] bytearray = new byte[1048576];
  int size=0;
  sImage = rs.getBinaryStream(1);
  response.reset();
  response.setContentType("image/jpeg");
  while((size=sImage.read(bytearray))!= -1 )
  {
  response.getOutputStream().write(bytearray,0,size);
  }
  response.flushBuffer();
  sImage.close();
  rs.close();

  }
  }
  catch(Exception ex)
  {
  out.println(ex);
  }
  
  
  psmnt.close();
  connection.close();
while((size=sImage.read(bytearray))!= -1 )  {  response.getOutputStream().write(bytearray,0,size);  }  response.flushBuffer();  sImage.close();  rs.close();   }  }  catch(Exception ex)  {  out.println(ex);  }    psmnt.close();  connection.close();

while((size=sImage.read(bytearray))!= -1 )  {  response.getOutputStream().write(bytearray,0,size);  }  response.flushBuffer();  sImage.close();  rs.close();   }  }  catch(Exception ex)  {  out.println(ex);  }    psmnt.close();  connection.close();

why is it that the code above displays only a blank page??

why is it that the code above displays only a blank page??

Not sure what you are talking about? Which code?

the one that is longer.. it only displays a blank page and no image..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.