• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

How to fetch a picture from DB?

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my work,I store pictures as Image type in SQL server.
Now i wanna fetch the pic and show it in JSP,somebody told me that I should
make the pic to a byte array first,so what is next ?
How can I do to make the byte array show as a gif in Jsp?

thx a lot !!!
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmmmmm
you can use a helper class which may has a method which converts a byte[] into a file and place it into a particular directory, somewhere under /WEB_INF/, then just specify the name, including path, in your jsp.

offcourse you will have the name and extension of that particular image with you in the database, right.
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Daniel,

Here is a simple example that I recently posted in this thread.



Do it in a Servlet, not in a JSP.

Hope that helps.

Jules
 
Daniel Washington
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry,it seems your code doesn't work,your codes fetch byte array from a real my-image.jpg and show it using servlet,but I mean how to show the picture which is stored in Blob type.
thx anyway

Originally posted by Julian Kennedy:
Hi Daniel,

Here is a simple example that I recently posted in this thread.



Do it in a Servlet, not in a JSP.

Hope that helps.

Jules

 
JulianInactive KennedyInactive
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, is your question how to read a Blob type from a database? If you look at the thread I linked to, that may help you. If you give it a bit of thought you can easily adapt my example to work for you.
 
Daniel Washington
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
very thx to Jules,I have already figured out this problem with your help.
now I post my complete JSP code ,hope can help others.
<%@ page contentType="text/html; charset=ISO-8859-1" %>
<%@ page import="java.sql.*,java.io.*"%>

<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc dbc:testforwebsphere","sa","sa");
String sql = "select * from testforphoto where test='111'";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs =pstmt.executeQuery();
rs.next();
InputStream in = rs.getBinaryStream("photo");
OutputStream sos = response.getOutputStream();

int len = 0;
byte[] b = new byte[1024];
response.reset();
response.setContentType("image/gif");
while((len = in.read(b)) > 0)
sos.write(b);
in.close();
sos.close();
%>
<html>
<body>
show the picture successfully!
</body>
</html>

Originally posted by Julian Kennedy:
So, is your question how to read a Blob type from a database? If you look at the thread I linked to, that may help you. If you give it a bit of thought you can easily adapt my example to work for you.

 
Those who dance are thought mad by those who hear not the music. This tiny ad plays the bagpipes:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic