• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

orcale测试实例

发布: 2007-7-01 18:47 | 作者: admin | 来源: | 查看: 21次 | 进入软件测试论坛讨论

领测软件测试网 /*
   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 Server@#s 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 servlet@#s 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: mdconnol@us.oracle.com

*/

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 servlet@#s 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 instance@#s listener (the
TNS address space) is using as specified in the
.PARMLIB(MPMTNS) member
         of the Oracle@#s 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;
   }

}




文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网