1

Image(.jpg) is stored in database in blob type. I have written code for retrieving blob

<%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>
<% Blob image = null;
java.sql.Connection con = null;
byte[ ] imgData = null ;
java.sql.Statement stmt = null;
java.sql.ResultSet rs = null;
 try {
System.out.println("DisplayBlob.jsp request Parameter "+request.getParameter("imgName"));
con = java.sql.DriverManager.getConnection("jdbc:oracle:thin:@ip:1521:sid","userName", "password");
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT blobObj FROM ImageBlobTable");
if (rs.next()) {
image = rs.getBlob(1);
imgData = image.getBytes(1,(int)image.length());
} else {
out.println("Display Blob Example");
out.println("image not found for given id>");
return;
}
response.setContentType("image/gif");
OutputStream o = response.getOutputStream();
o.write(imgData);
o.flush();
o.close();
} catch (Exception e) {
out.println("Unable To Display image");
out.println("Image Display Error=" + e.getMessage());
return;
} finally {
try {
rs.close();
stmt.close();
con.close();
 } catch (SQLException e) {
 e.printStackTrace();
 }
 }
 %>  

There is index.jsp. In this Jsp there is text,When user takes cursor to the the text then i have to show image. When user remove cursor on that text then image should be disappeared. How can i do this?

Techn: JSP,jquery

user752590
  • 111
  • 5
  • 12
  • 29
  • It is no good practice to put code like this in a JSP. Please consider at least using servlets, or a simple [MVC](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller) pattern. With a separate servlet for retrieving your JPG from database, you will find your problem easy to solve. – f_puras Aug 29 '12 at 13:54
  • I transferred the above code(bus. logic) to struts2 and got the url.....but not able to solve the problem – user752590 Aug 29 '12 at 14:14

1 Answers1

1

Split your code in a servlet for image data retrieval + download like this, and a JSP performing image display/hiding via JavaScript like that.

Community
  • 1
  • 1
f_puras
  • 2,521
  • 4
  • 33
  • 38