我在我的 jsp 页面的正文部分插入了以下 java 代码,以从 Oracle 10g 数据库中检索表 FLIGHTDATA 的记录。但是在执行 Class.forName(...) 行之后,程序直接进入 finally 阻塞并关闭连接而不返回任何数据。关于我做错了什么的任何建议?谢谢 - 索姆纳特
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%
out.println("<table border='1'><tr>");
Connection connection = null;
Statement statement = null;
ResultSet rs_1hop = null;
ResultSetMetaData rsm_1hop = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String connectionURL = "jdbc:oracle:thin:@localhost:1521:xe";
connection = DriverManager.getConnection(connectionURL, "system", "system");
statement = connection.createStatement();
// sql query to retrieve values from the secified table.
String QueryString = "SELECT * from FLIGHTDATA";
rs_1hop = statement.executeQuery(QueryString);
rsm_1hop = rs_1hop.getMetaData();
int colCnt = rsm_1hop.getColumnCount();
for (int i=1; i<=colCnt; ++i) {
out.println("<th>" + rsm_1hop.getColumnName(i) + "</th>");
}
out.println("</tr>");
while (rs_1hop.next()) {
out.println("<tr>");
for (int i=1; i<=colCnt; ++i)
out.println("<td>" + rs_1hop.getString(i) + "</td>");
out.println("</tr>");
}
} catch (Exception e) {
} finally {
if (statement != null)
statement.close();
if (connection != null)
connection.close();
}
out.println("</table><br><br>");
%>
在按照 BalusC 的注释在 catch 块中添加 ServletException 并在 /WEB-INF/lib 下添加 odbc6.jar 文件后,我收到以下错误消息,我认为这是由于找不到 jdbc 驱动程序。
我还尝试在 Apache Tomcat 安装目录 /ROOT/web-apps/WEB_INF/lib 下添加 jar 文件,以便它们可用于所有 Web 应用程序,但问题仍然存在。
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: javax.servlet.ServletException: DB interaction failed! org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:500)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:410)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
根本原因
javax.servlet.ServletException: DB interaction failed!
org.apache.jsp.retrievePossibleRoutes_jsp._jspService(retrievePossibleRoutes_jsp.java:96)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
根本原因
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:128)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:66)
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Unknown Source)
org.apache.jsp.retrievePossibleRoutes_jsp._jspService(retrievePossibleRoutes_jsp.java:73)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)