I’m new to learning JSPs and Servlets. I have a Java class which calls a stored procedure and return a list of states from a database (this is my test case while I learn, other more complex actions will come later). The list of states should be returned to a JSP for display/use in a form, etc… For now, I’m happy to just get the data printing via the JSP page to know it is there.
However, when I call the Java code from the JSP, it appears that the result object is returned as null and thus no data is accessible. I’ve tried to return the data as a ResultSet object as well, but the results are the same. I do know the stored procedure works when called from SQL. Additionally I have the Java code as a servlet that responds to a doPost request and that works fine…
What might I be missing / failing to understand? Is this possible?
package edu.XXXX.ais.userapp;
import java.sql.*;
import java.util.*;
public class DBQueries {
public static Vector<String> getStatesList() {
Vector<String> results = new Vector<String>();
Connection con = null;
try {
// Example of executing a stored procedure
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection ("jdbc:mysql://hostname:3306/dbName","username","password");
CallableStatement cs = con.prepareCall("{call getStatesList()}");
ResultSet rs = cs.getResultSet();
while (rs.next()) {
results.add(rs.getString(1));
}
} catch (SQLException e) {
System.err.println("Servlet could not display records." + e);
} catch (ClassNotFoundException e) {
System.err.println("JDBC driver not found." + e);
} finally {
try {
if (con != null) {
con.close();
con = null;
}
} catch (SQLException e) {
System.err.println(e);
}
}
return results;
}
}
JSP page that calls the above Java code (expects to be able to receive results…)
<html>
<head>
<title>Foo</title>
</head>
<body>
<h3>List of states via a servlet call to the database</h3>
<%@ page import="java.util.*" %>
<%@ page import="edu.XXXX.ais.userapp.DBQueries" %>
<%
Vector rs = DBQueries.getStatesList();
if (rs != null)
for (int index = 0; index < rs.size(); index++) {
out.println("Name : " + (String) rs.get(index));
}
%>
</body>
</html>
The resulting Tomcat errors are as follows:
org.apache.jasper.JasperException: An exception occurred processing JSP page /states.jsp at line 12
9: <%@ page import="edu.XXXX.ais.userapp.DBQueries" %>
10:
11: <%
12: Vector rs = DBQueries.getStatesList();
13: if (rs != null)
14: for (int index = 0; index < rs.size(); index++) {
15: out.println("Name : " + (String) rs.get(index));
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause
java.lang.NullPointerException
edu.XXXX.ais.userapp.DBQueries.getStatesList(Unknown Source)
org.apache.jsp.states_jsp._jspService(states_jsp.java:75)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
In your sample code, the
CallableStatementis not executed beforecs.getResultSet(), sorswill be null, and thenNullPointerExceptionwill occur when executingrs.next().Make sure the statement is executed, and make sure the execute result is
ResultSetnot an update count.