我在jsp頁面的正文部分插入了以下java代碼,以從Oracle 10g數據庫中檢索表FLIGHTDATA的記錄。但是在執行Class.forName(...)行後,程序直接進入最後阻塞並關閉連接而不返回任何數據。任何建議,我做錯了什麼?謝謝 - 索姆納特沒有使用JSP從Oracle 10g數據庫返回的數據記錄
<%@ 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>");
%>
下/ WEB-INF/lib目錄添加在catch塊的ServletException由BalusC評論,並添加odbc6.jar文件後,我得到它,我想是因爲以下錯誤消息找不到jdbc驅動程序。
我也嘗試在Apache Tomcat安裝dir/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)
嗨,我已經在我的項目構建路徑的Libraries選項卡下添加了ojdbc6.jar。這不夠嗎? – somnathchakrabarti 2012-04-24 20:14:25
不需要。您需要在項目屬性的* Deployment Assembly *部分配置它,以便讓JAR文件最終在構建的WAR的'/ WEB-INF/lib'文件夾中結束。但這很笨拙。只需在項目的'/ WEB-INF/lib'文件夾中手動刪除物理JAR文件即可。不要忘記在構建路徑中撤消您之前所做的更改。這些變化是不必要的,可能會衝突。當您將JAR文件放在項目的'/ WEB-INF/lib'文件夾中時,IDE將自動處理正確的構建路徑設置。 – BalusC 2012-04-24 20:16:26
是的,我從項目構建路徑 - >庫中刪除了這些文件,並將它們添加到/WebContent/WEB-INF/lib以及/web-apps/ROOT/WEB-INF/lib可用於所有網絡應用程序。但是現在由於在catch塊中添加了ServletException而引發了詳細的錯誤消息,問題依然存在。 –
somnathchakrabarti
2012-04-24 20:35:33