File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

retrieve images from a database

 
kabita tripathy
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64188
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a servlet that streams the image as the response, then use the URL of this servlet in the JSP in an <img> tag.
 
kabita tripathy
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64188
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
will you write the code please.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64188
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope, that's your job.

Or you could search for previous discussions with examples.
 
Stefan Evans
Bartender
Posts: 1640
9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic