package image;
import java.sql.*;
import java.io.*;
import java.util.*;
import oracle.jdbc.*;
import oracle.sql.*;
public class images
{
/*-------------------------
* Get the Blob image
*------------------------*/
public static byte[] getPhoto (OracleConnection conn, int iNumPhoto)
throws Exception, SQLException
{
String req = "" ;
Blob img ;
byte[] imgData = null ;
Statement stmt = conn.createStatement ();
// Query
req = "Select image From IMAGES Where ImageID = " + iNumPhoto ;
ResultSet rset = stmt.executeQuery ( req );
while (rset.next ())
{
img = rset.getBlob(1);
imgData = img.getBytes(1,(int)img.length());
}
rset.close();
stmt.close();
return imgData ;
}
}
The JavaServer Page includes the bean so its methods can be accessed in the JSP page using scriplets and âphotoâ as a named bean reference
<%@ page import = "image.*" %>
<%@ page import = "java.io.*" %>
<%@ page import = "oracle.jdbc.OracleConnection" %>
<jsp:useBean id="photo" class="image.images" scope="session" />
<%
int iNumPhoto ;
oracle.jdbc.driver.OracleConnection conn = null;
if ( request.getParameter("imgID") != null )
{
iNumPhoto = Integer.parseInt(request.getParameter("imgID")) ;
try
{
conn = â¦â¦â¦â¦;
conn.setAutoCommit (false);
// get the image from the database
byte[] imgData = photo.getPhoto( conn, iNumPhoto ) ;
// display the image
response.setContentType("image/gif");
OutputStream o = response.getOutputStream();
o.write(imgData);
o.flush();
o.close();
}
catch (Exception e)
{
e.printStackTrace();
throw e;
}
finally
{
⦠Close the connexion ⦠;
}
}
%>
http://studyjava.org/forums/