This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JSP and the fly likes retrieve images from a database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "retrieve images from a database" Watch "retrieve images from a database" New topic
Author

retrieve images from a database

kabita tripathy
Greenhorn

Joined: Sep 20, 2011
Posts: 3
Hello frds..

I created a database whichi contains image name and images in jpeg format. For a single imagename there is 3 images.

I want when I will give the image name it will so me the related images to that name

Now I want to retrive this using jsp. so please suggest me the code.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60773
    
  65

Create a servlet that streams the image as the response, then use the URL of this servlet in the JSP in an <img> tag.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
kabita tripathy
Greenhorn

Joined: Sep 20, 2011
Posts: 3
thank you sir for replying..

but i am storing images in a database.

i code this.. by this i only retrive one image what i gave inside the code.

but my question is when user put an id in front page. it will so three photoes related to that id.

will you please help me on this
<%
Connection connection = null;
String connectionURL = "jdbc:mysql://localhost:3306/mysql";
ResultSet rs = null;
PreparedStatement psmnt = null;
InputStream sImage;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "root");
psmnt = connection.prepareStatement("SELECT picture FROM ricepicinfo WHERE ano = ?");
psmnt.setString(1, "n3");
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);
}
}
}
catch(Exception ex){
out.println("error :"+ex);
}
finally {
// close all the connections.
rs.close();
psmnt.close();
connection.close();
}
%>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60773
    
  65

My reply still stands. There should never be any Java code in a JSP. Remove it.

Use a servlet to read the image from the DB and stream it as the response. In the JSP use an image tag as I a.ready advised.
kabita tripathy
Greenhorn

Joined: Sep 20, 2011
Posts: 3
will you write the code please.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60773
    
  65

Nope, that's your job.

Or you could search for previous discussions with examples.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
>will you write the code please.

remove the <% and %> markers.
Copy and paste this code into a servlet doGet/doPost method.
job done.

Or you can go check out this blog

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: retrieve images from a database
 
Similar Threads
Displaying uploaded images in jsp
How can I merge 18 images to 1 images?
Comparing Images??
Uploading jpeg images to webserver
Image jsp servlet controller bean and database