This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to retrieve image from ms access database through jsp

 
j u ray
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
I have stored images in ms access. The field name is "image" and the data type is OLE Object. I have stored an image with ".bmp" extension. In the database in the record it is showing "Bitmap Image". Now I am using JSP to retrieve this image and display it in the web page.

The coding is::
<%
InputStream sImage;
try {
String dataSourceName = "pictures";
String dbURL = "jdbcdbc:" + dataSourceName;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
String sTable = "imagetable";
Connection cn = null;
Statement st = null;
ResultSet rs = null;
String sSql = "SELECT machine_image,machine_name,machine_details FROM " + sTable;
cn = DriverManager.getConnection(dbURL,"","");
st = cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = st.executeQuery( sSql );
ResultSetMetaData rsmd = rs.getMetaData();
int n = rsmd.getColumnCount();
out.println( "<div align=\"center\">" + "<table border=2 cellspacing=2 bgcolor=LightGreen width=400><tr>" );
while(rs.next()) {
out.println( "</tr><tr>" );
byte[] bytearray = new byte[1048576];
int size=0;
sImage = rs.getBinaryStream(1);
response.reset();
response.setContentType("image/bmp");
while((size=sImage.read(bytearray))!= -1 ){
response.getOutputStream().write(bytearray,0,size);
}
} //while
out.println( "</tr></table></div>" )

%>
<%

}//try

catch (Exception err) {
out.println("ERROR" + err);
}
%>


the output which i am getting is:
in the text box where the image is supposed to be displayed , string value is being displayed.
i am not able to get the image.

i have gone through net but not able to get any proper solution.

I will be grateful if anyone can help me. please help me as soon as possible.
thanks in advance.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recommendation.

1. Don't use code in JSP to retrieve image or any other extensive computation.
2. Never ever try to serve binary data from JSP, use a servlet, JSP is for writing HTML with IMG tags

This question comes up all the time, search the forum.
Bill
 
Mehul Kumbhani
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If possible for you to get image without using OLE object in access use the image path to store image.

if you are using netbeans the you only need to give path of your current directory,
I am currently using this,so if possible do that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic