I have inserted the following java code in the body part of my jsp page to retrieve the records of the table FLIGHTDATA from a Oracle 10g database. But after the execution of the Class.forName(…) line the program directly goes to finally block and closing the connection without returning any data. Any suggestions on what I am doing wrong ? Thanks – Somnath
<%@ 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>");
%>
After adding the ServletException in the catch block as commented by BalusC, and adding the odbc6.jar files under /WEB-INF/lib, I am getting the following error message which I suppose is due to the jdbc driver not found.
I also tried adding the jar files under Apache Tomcat installation dir /ROOT/web-apps/WEB_INF/lib so that they are available for all web-apps but the problem persists.
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)
root cause
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)
root cause
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)
That can only happen if a
ClassNotFoundExceptionwas been thrown. This means that concrete class as specified by the given class name is not in the webapp’s runtime classpath at all. That class is part of the Oracle JDBC driver. Putting the Oracle JDBC driver JAR file in/WEB-INF/libfolder of the webapp should fix that problem.Further I strongly recommend you to do something in that empty
catch (Exception e) {}block to indicate that an exception has occurred. Right now you’re stabbing clueless around in the dark as to what really happened. If you have rethrown it as aServletException, then you’d have gotten a much more self-explaining error page instead of a halfbaked JSP result.Last but not least, writing Java code in a JSP file is a bad practice. How to salvage this properly, check this answer: Show JDBC ResultSet in HTML in JSP page using MVC and DAO pattern.
Update as per your update:
Then it’s the wrong JAR file (i.e. it’s not the one containing the
oracle.jdbc.driver.OracleDriverclass), or you didn’t rebuild/redeploy/restart the webapp properly. Make sure that you’ve downloaded the right JDBC driver for your Oracle database version as listed here. Make sure that you’ve properly rebuilt/redeployed/restarted the webapp.This is nonsense. Undo this change. To make a JAR available to all deployed webapps, put it in Tomcat’s own
/libfolder (the one straight in Tomcat installation folder). But that won’t solve your problem if it’s the wrong JAR file.