We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,555 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Retrieving image from MySQL

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
1 Year
Discussion Span
1 Year Ago
Last Updated
7
Views
anithajerome
Newbie Poster
12 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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();
anithajerome
Newbie Poster
12 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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();
anithajerome
Newbie Poster
12 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

archie.herbias
Light Poster
39 posts since Sep 2011
Reputation Points: 8
Solved Threads: 0
Skill Endorsements: 0

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

Not sure what you are talking about? Which code?

peter_budo
Code tags enforcer
Moderator
15,812 posts since Dec 2004
Reputation Points: 2,871
Solved Threads: 944
Skill Endorsements: 52

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

archie.herbias
Light Poster
39 posts since Sep 2011
Reputation Points: 8
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.1186 seconds using 2.67MB