orcale测试实例
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;
}
}
Tags:
作者:佚名评论内容只代表网友观点,与本站立场无关!
评论摘要(共 0 条,得分 0 分,平均 0 分)
查看完整评论