Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The exception tells you where to look:</p> <pre><code>edu.XXXX.ais.userapp.DBQueries.getStatesList(Unknown Source) </code></pre> <p>There's a null reference in that method. I'd recommend testing that without the JSP and use a debugger. </p> <p>You don't close your <code>Statement</code> or <code>ResultSet</code>. That will lead to grief. Close them in reverse order of creation, just like you did the <code>Connection</code>.</p> <p>For starters, please stop putting scriptlet code in your JSP. That's 1999 style; don't do it. </p> <p>The right thing to do is to learn JSTL and only have your JSP do display tasks.</p> <p>Let the JSP talk to a servlet that intercedes on its behalf with the database.</p> <p>Use the app server to set up the data source. You shouldn't have connection information inside your code.</p> <p>Forget about servlets and JSPs for now. Start with the data layer. </p> <p>Write that using POJOs, test it, get it working, and put it aside. </p> <p>Then write a servlet that takes in an HTTP request, validates and binds the parameters, uses your data access layer to talk to the database, and then put the response in request or session scope for the page to display. </p> <p>It's called Model-2 MVC. That's the way to write Java web apps.</p> <p>I'd write that DBUtils class something more like this: </p> <pre><code>package edu.XXXX.ais.userapp; import java.sql.*; import java.util.*; public class DBQueries { public static List&lt;String&gt; getStates(Connection connection) throws SQLException { List&lt;String&gt; states = new ArrayList&lt;String&gt;(); CallableStatement cs = null; ResultSet rs = null; try { cs = connection.prepareCall("{call getStatesList()}"); boolean hasResultSet = cs.execute(); rs = cs.getResultSet(); while (rs.next()) { states.add(rs.getString(1)); } } finally { close(rs); close(cs); } return states; } public static void close(ResultSet rs) { try { if (rs != null) { rs.close(); } } catch (SQLException e) { e.printStackTrace(); } } public static void close(Statement st) { try { if (st != null) { st.close(); } } catch (SQLException e) { e.printStackTrace(); } } public static void close(Connection conn) { try { if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } </code></pre> <p>Write a JUnit test to prove that your DBQueries works: </p> <pre><code>package edu.XXX.ais.userapp; public class DBQueriesTest { // You have to initialize this; I'm deliberately leaving out some details. private Connection connection; @Test public void testGetStates() { List&lt;String&gt; actual = DbQueries.getStates(this.connection); List&lt;String&gt; expected = { "CT", "NY" }; // whatever Assert.assertEquals(expected, actual); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload