package examples.jdbc.pool;

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import weblogic.db.jdbc.*;
import weblogic.html.*;

/**
 * This example shows how to use a WebLogic pool driver to make a
 * JDBC connection for a servlet. 
 *
 *
 * @author        Copyright (c) 1997-2000 by BEA Systems, Inc.  All Rights Reserved.
 */
public class simpleselect extends javax.servlet.http.HttpServlet {

  /**
   * This implements the service method in HttpServlet. The method uses
   * a dbKona DataSet along with the pool driver for database access,
   * and htmlKona to generate an HTML page for display.
   *<h3>To set up this example:</h3>
   *<ol>
   *<li>Set up your development shell as described in <a
   *href=../../../techstart/environment.html>Setting up your environment</a>.
   * 
   *<p>
   *
   *<li>Compile this example by executing the following command in your development shell:
   *
   *<p>
   *
   *<font face="Courier New"><b>$ javac -d %SERVLET_CLASSES% simpleselect.java</b></font>
   *<p>	
   *
   *<li>Register the servlet in your <tt>weblogic.properties</tt> file
   *by adding the following  property:
   *
   *<pre>  weblogic.httpd.register.pooltest=examples.jdbc.pool.simpleselect</pre>
   *
   *<p>
   *
   *<li>Register the connection pool "<font face="Courier New" size=-1>oraclePool</font>" in your WebLogic
   *Server's <tt>weblogic.properties</tt> file by uncommenting the lines
   *that refer to the <font face="Courier New"
   *size=-1>connectionPool.oraclePool</font>. Make sure to uncomment the
   *ACLs for this connection pool as well. If you are not using an Oracle
   *database, adjust the connection properties in these lines so that
   *they are appropriate for your database.
   *
   *<p>
   *<li>Start your WebLogic Server.
   *
   *<p>
   *<li>Call the servlet from a browser with a URL like
   *
   *<p>
   *<font face="Courier New">http://<i>WebLogicHost:port</i>/pooltest</font> 
   *
   *<p>Where <i>WebLogicHost:port</i> is the hostname and port number of
   *your WebLogic Server.
   *
   *<p>
   *
   *</ol>
   */
  public synchronized void service(HttpServletRequest req, HttpServletResponse res)
       throws IOException
  {
    try {
      
      Class.forName("weblogic.jdbc.pool.Driver").newInstance();
      Connection conn =
        DriverManager.getConnection("jdbc:weblogic:pool:oraclePool", null);
      
      res.setStatus(HttpServletResponse.SC_OK);
      res.setContentType("text/html");
      
      TableDataSet ds = new TableDataSet(conn, "emp");
      ds.where("sal < 2000")
        .order("empno")
        .fetchRecords();
      
      TableElement tbl = new TableElement(ds);
      
      ServletPage hp = new ServletPage("Simple select");
      hp.getBody()
        .addElement(MarkupElement.HorizontalLine)
        .addElement(tbl);
      
      ds.close();
      conn.close();
      hp.output(res.getOutputStream());
    }
    catch (Exception e) {
      ServletPage hp             = new ServletPage("An Exception occurred");
      ByteArrayOutputStream ostr = new ByteArrayOutputStream();
      e.printStackTrace(new PrintStream(ostr));
      hp.getBody()
          .addElement(new HeadingElement("An Exception occurred:", 2))
        .addElement(new LiteralElement(ostr.toString()));
      hp.output(res.getOutputStream());
    }
  }
}

