FileDocCategorySizeDatePackage
TestClobAsciiServlet.javaAPI DocExample2775Thu Dec 14 18:23:26 GMT 2000None

TestClobAsciiServlet.java

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class TestClobAsciiServlet extends HttpServlet {

 public void doGet(
  HttpServletRequest request, 
  HttpServletResponse response) 
  throws IOException, ServletException {

  ServletOutputStream out = response.getOutputStream();

  Clob       biography  = null;
  Connection connection = CacheConnection.checkOut();
  Statement  statement  = null;
  ResultSet  resultSet  = null;
  String     sql        =
   "select biography " +
   "from   person p, person_information i " +
   "where  p.person_id = i.person_id " +
   "and    last_name = " + formatWithTicks(request.getParameter("last_name")) + " " +
   "and    first_name = " + formatWithTicks(request.getParameter("first_name"));
  try { 
   statement = connection.createStatement();
   resultSet = statement.executeQuery(sql);
   if (resultSet.next()) {
    biography = resultSet.getClob(1);
   }
   else {
    response.setContentType("text/html");
    out.println("<html><head><title>Person Biography</title></head>");
    out.println("<body><h1>No data found</h1></body></html>");
    return;
   }
   response.setContentType("text/plain");
   InputStream in = biography.getAsciiStream();
   System.out.println("after getAsciiStream");
   int length = (int)biography.length();
   System.out.println("lenght of the blob is " + length);
   byte[] buffer = new byte[length];
   while ((length = in.read(buffer)) != -1) {
    System.out.println("writing " + length + " bytes");
    out.write(buffer, 0, length);
   }
   System.out.println("written");
   in.close();
   out.flush();
  }
  catch (SQLException e) {
   System.out.println("TestClobServlet.doGet() SQLException: " + 
    e.getMessage() + "executing ");
   System.out.println(sql);
  }
  finally {
   if (resultSet != null) 
    try { resultSet.close(); } catch (SQLException ignore) { }
   if (statement != null) 
    try { statement.close(); } catch (SQLException ignore) { }
  }

  // let's return the conection
  CacheConnection.checkIn(connection);

 }

 public void doPost(
  HttpServletRequest request, 
  HttpServletResponse response)
  throws IOException, ServletException {
  doGet(request, response);
 }

 private String formatWithTicks(String string) {
  if (string != null) {
   char[]       in  = string.toCharArray();
   StringBuffer out = new StringBuffer((int)(in.length * 1.1));
   if (in.length > 0)
    out.append("'");
   for (int i=0;i < in.length;i++) {
    out.append(in[i]);
    if (in[i] == '\'') 
     out.append(in[i]);
   }
   if (in.length > 0)
    out.append("'");
   return out.toString();
  }
  else {
   return "NULL";
  }
 }


}