this
servlet has been tested for retreiving Blob datatype for image file form DB2 Database.Kindly convert it for oracle database wherevere needed thanx.
package
test;
/**
* Insert the type's description here.
* Creation date: (1/23/99 11:36:37 PM)
* @author: Administrator
*/
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class BlobServlet extends javax.servlet.http.HttpServlet
{
Connection con= null;
Statement stmt= null;
ResultSetrs= null;
/**
* Process incoming HTTP GET requests
*
* @param request Object that encapsulates the request to the servlet
* @param response Object that encapsulates the response from the servlet
*/
public void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException
{
performTask(request, response);
}
/**
* Process incoming HTTP POST requests
*
* @param request Object that encapsulates the request to the servlet
* @param response Object that encapsulates the response from the servlet
*/
public void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
performTask(request, response);
}
/**
* Returns the servlet info
string.
*/
public String getServletInfo() {
return super.getServletInfo();
}
/**
* Initializes the servlet.
*/
public void init() {
// insert code to initialize the servlet here
}
/**
* Process incoming requests for information
*
* @param request Object that encapsulates the request to the servlet
* @param response Object that encapsulates the response from the servlet
*/
public void performTask(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws IOException
{
try
{
// I have tested this servlet for my application change to make it general
//change here to remove hard coding of the sql query
String rSQL="select * from BlobTest where row_id='12'";
//change database driver and url etc.
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();
con=DriverManager.getConnection("jdbc:db2:SAMPLE","db2admin","udbadmin");
stmt=con.createStatement();
rs=stmt.executeQuery(rSQL);
//if no O/p found then just display message on the browser
if(! rs.next())
{
response.setContentType("text/html");
PrintWriter pout= new PrintWriter(response.getOutputStream());
pout.println("No Matching records found ");
pout.flush();
pout.close();
}//if(! rs.next())
// for reading the message set this .
response.setContentType("image/gif");
ServletOutputStream out= response.getOutputStream();
InputStream in=rs.getBinaryStream(2);
ByteArrayOutputStream baos= new ByteArrayOutputStream();
byte b[]= new byte[1024];
while(true)
{
int bytes=in.read(b);
if(bytes == -1)
{
break;
}//if
baos.write(b,0,bytes);
}//while
b=baos.toByteArray();
response.setContentLength(b.length);
out.write(b,0,b.length);
out.flush();
out.close();
System.out.println("successful");
}//try
catch(Throwable theException)
{
response.setContentType("text/html");
PrintWriter pout= new PrintWriter(response.getOutputStream());
pout.println("Exception while retreiving");
pout.flush();
pout.close();
}
finally
{
try
{
if(stmt != null) stmt.close();
if(rs != null) rs.close();
if(con != null) con.close();
}//try
catch(Exception ex)
{
}//catch
}//finally
}
}