程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> orcale測試實例

orcale測試實例

編輯:關於JAVA

/*

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: [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 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("");

out.println("

Begin Employee

List



");

out.println("

");

/*

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("
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("



End Employee

List

");

out.close();

log(thisServletName + "doGet (End)");

}

public void destroy()

{

log(thisServletName + ":Terminated");

super.destroy();

}

public boolean isInitialized()

{

return servletInitialized;

}

public String getServletInfo()

{

return thisServletNameDesc;

}

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved