Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

insert view, images from mysql database using jsp program

 
maheshm morem
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i am trying to insert , view the images from mysql database but it inserts the images but does not display the all images , it display only one first image , so what i have to do?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show us some of your code?
 
maheshm morem
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<%@page language="java" import="java.sql.*, java.io.*" %>
<html>
<body>

<%
Connection connection = null;
ResultSet rs = null;
Statement st = null;
try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
connection = DriverManager.getConnection("jdbcdbc:mm1", "root", "mahesh");
st=connection.createStatement();
InputStream sImage;
rs = st.executeQuery("select * from save_image");
while(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);
}

}
}

catch (Exception ex) {
out.println("Found some error : "+ex);
}
finally {
// close all the connections.
//connection.close();
//psmnt.close();
}

%>
</body>
</html>
 
Jan Cumps
Bartender
Posts: 2584
11
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the effect of this line, which you call every time you loop:
response.reset();
?

JSPs are not made to create binary output. They are not your tool to display images.
Would it not be easier to create a servlet that serves the images from the database,
and that your jsp page calls that servlet in an <img> tag?

Another thread on the same topic:
http://www.coderanch.com/t/292437/JSP/java/display-image-after-retrieving-it

I am moving this post to our JSP forum for you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic