Display Clob Servlet : Database « Servlets « Java






Display Clob Servlet

import java.io.IOException;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DisplayClobServlet extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,
      ServletException {

    Clob fileAsCLOB = null;
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;

    String id = "001";
    String query = "select fileBody from DataFiles where id = " + id;
    ServletOutputStream out = response.getOutputStream();

    // all responses will be in text/html format
    response.setContentType("text/html");

    try {
      conn = getHSQLConnection();
    } catch (Exception e) {
      out.println("<html><head><title>CLOB Example</title></head>");
      out.println("<body><h4>Database Connection Problem.</h4>");
      out.println("<h5>" + e.getMessage() + "</h5></body></html>");
      return;
    }

    try {
      stmt = conn.createStatement();
      rs = stmt.executeQuery(query);
      if (rs.next()) {
        fileAsCLOB = rs.getClob(1);
      } else {
        out.println("<html><head><title>CLOB Example</title></head>");
        out.println("<body><h4>No file found for id=" + id + "</h4></body></html>");
        return;
      }

      // Materialize the CLOB as a String object (get the whole clob).
      long length = fileAsCLOB.length();
      // note that the first character is at position 1
      String fileAsString = fileAsCLOB.getSubString(1, (int) length);

      // write it for display
      out.println(fileAsString);
      System.out.println("CLOB writing done.");
    } catch (SQLException e) {
      out.println("<html><head><title>Error: CLOB Example</title></head>");
      out.println("<body><h4>Error=" + e.getMessage() + "</h4></body></html>");
      return;
    } finally {
      try {
        rs.close();
        stmt.close();
        conn.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
  }

  private static Connection getHSQLConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    System.out.println("Driver Loaded.");
    String url = "jdbc:hsqldb:data/tutorial";
    return DriverManager.getConnection(url, "sa", "");
  }

  public static Connection getMySqlConnection() throws Exception {
    String driver = "org.gjt.mm.mysql.Driver";
    String url = "jdbc:mysql://localhost/demo2s";
    String username = "oost";
    String password = "oost";

    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }

  public static Connection getOracleConnection() throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:databaseName";
    String username = "username";
    String password = "password";

    Class.forName(driver); // load Oracle driver
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }

}

           
       








Related examples in the same category

1.Servlets Database Query
2.Using JDBC in Servlets
3.Cached Connection Servlet
4.Transaction Connection Servlet
5.Session Login JDBC
6.JDBC and Servlet
7.Database and Servlet: Database MetaData
8.Database and Servlet: Store procedure
9.Database transaction
10.Typical database commands
11.Process a raw SQL query; use ResultSetMetaData to format it
12.See Account
13.Guest Book Servlet
14.Dedicated Connection Servlet
15.Login Servlets
16.OCCI Connection Servlet
17.Get Column Names From ResultSet
18.Delete Blob From Servlet
19.Delete Clob From Servlet
20.Display Blob Servlet
21.Delete Clob From Oracle in a Servlet
22.Insert Clob to MySql Servlet
23.Update Clob data stored in MySql from a Servlet