0

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?

3
Contributors
5
Replies
8
Views
6 Years
Discussion Span
Last Post by archie.herbias
0

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();

Edited by peter_budo: Keep It Clear - Do wrap your programming code blocks within [code] ... [/code] tags

0
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();

Edited by mike_2000_17: Fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.