爱心技术专栏专题

e测试实例

摘录:java 来源:java 加入时间:2006年08月12日
摘要:
e测试实例

/*
   OracleTestServlet -  This is a sample servlet which
connects to an Oracle instance.  It includes some code to
log various points of invocation and …

转载:转载请保留本信息,本文来自
http://www.51dibs.com
/html/2006/article/info14/a_73676b6219f98f9d.htm

e测试实例

站点:爱心种子小博士 关键字:e测试实例

   
e测试实例
/*
   OracleTestServlet -  This is a sample servlet which
connects to an Oracle instance.  It includes some code to
log various points of invocation and execution into
the WebSphere Application Servers log.  It also sets a log
file (in the current directory where the servlet is
invoked) for the JDBC driver.  This JDBC driver log can be
examined along with the servlets log output in order to
determine configuration problems.  Create the
corresponding class file from this source and move it to the
appropriate servlets directory.

Marc Connolly
Oracle Corporation
Email: [email protected]

*/

import   java.io.File;
import   java.io.IOException;
import   java.lang.IllegalStateException;
import   java.io.PrintWriter;
import   java.io.PrintStream;
import   java.io.FileWriter;
import   java.io.FileOutputStream;

import   java.sql.DriverManager;
import   java.sql.Connection;
import   java.sql.SQLException;
import   java.sql.Statement;
import   java.sql.ResultSet;

import   java.util.Date;

import   javax.servlet.Servlet;
import   javax.servlet.ServletConfig;
import   javax.servlet.ServletException;
import   javax.servlet.http.HttpServlet;
import   javax.servlet.http.HttpServletRequest;
import   javax.servlet.http.HttpServletResponse;
import   javax.servlet.http.HttpUtils;

public class OracleServlet extends HttpServlet
{


   private String                thisServletName =
"OracleTestServlet";
   private String                thisServletNameDesc = "
Oracle JDBC Test Servlet";

   private boolean               servletInitialized = false;

   private long                  servletStartTimeStamp;

   public void init(ServletConfig config) throws
ServletException
   {
      super.init(config);
      /*
      Record the servlets initialization time
      */
      servletStartTimeStamp = System.currentTimeMillis();

      log(thisServletName + ":Init Completed");

      /*
      Mark servlet init complete
      */
      servletInitialized = true;
   }


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

      log("Start Menu Visual List()");
      log(thisServletName + "doGet (Start)");

      long startTimeStamp = System.currentTimeMillis();

      response.setContentType("text/html");
      PrintWriter out = response.getWriter();


      out.println("<HTML><TITLE>OracleTestServlet: Using
Oracle JDBC Driver</TITLE><BODY>");
      out.println("<BR><BR<BR<HR><CENTER>Begin Employee
List</CENTER><BR><BR>");
      out.println("<PRE>");

      /*
      Enable JDBC Driver logging
      */
      String jdbcDriverlogFileName =
"./OracleServletJDBC.log";

      log(thisServletName + "JDBC Log: " +
jdbcDriverlogFileName);

      /*
      Make the log file
      */
      File file = new File(jdbcDriverlogFileName);
      if (file.exists())
      {
         if (!file.canWrite())
         {
            log(thisServletName + "JDBC Driver Log file (" +
file.getAbsolutePath() + ") is not writable");
         }
      }
      else
      {
         try
         {
            File parent = new File(file.getParent());
            if (!parent.canWrite())
            {
               log(thisServletName + "JDBC Driver Log file
directory (" + parent.getAbsolutePath() + ") is not
writable");
            }
         }
         catch (NullPointerException e)
         {
              // no parent.
         }
      }
      try
      {
            DriverManager.setLogStream(new PrintStream(new
FileOutputStream(file.getAbsolutePath(), true)));
      }
      catch (IOException ioE)
      {
         log(thisServletName + "JDBC Driver Log - exception
thrown during setLogStream:" + ioE);
      }

      try
      {
         log(thisServletName + "Registering
Driver(before)");

         DriverManager.registerDriver(new
oracle.jdbc.driver.OracleDriver());

         log(thisServletName + "Registering Driver(after)");

         /*
         Note regarding connection url: the ip address is
the address of the host upon which the Oracle instance is
running followed by
         the port which the Oracle instances listener (the
TNS address space) is using as specified in the
.PARMLIB(MPMTNS) member
         of the Oracles MPM started task.
         */

         Connection conn =  DriverManager.getConnection
("jdbc:oracle:thin:@9.12.2.10:1621:MPM5","system",
"manager");


         log(thisServletName + "Get Connection(After)");

         Statement stmt = conn.createStatement ();
         log(thisServletName + "Create Statement(After)");

         ResultSet rset = stmt.executeQuery ("select EMPNO,
ENAME from scott.EMP");
         log(thisServletName + "Execute Query(After)");

         while (rset.next ())
            out.println("<BR>EMPNO:" + rset.getString (1) +
",NAME:" + rset.getString (2));
        log(thisServletName + "Close Connection(Before)");
         conn.close();
         log(thisServletName + "Close Connection(Before)");

      }
      catch (Exception e)
      {
         log(thisServletName + "Exception:" + e);
      }


      out.println("</PRE><BR><HR><CENTER>End Employee
List</CENTER></   FORM></BODY></HTML>");
      out.close();
      log(thisServletName + "doGet (End)");
   }



   public void destroy()
   {
      log(thisServletName + ":Terminated");
      super.destroy();
   }

   public boolean isInitialized()
   {
      return servletInitialized;
   }

   public String getServletInfo()
   {
      return thisServletNameDesc;
   }

}





客户服务中心信箱:[email protected] [email protected] 网站地图

声明

合作伙伴: