Question

I am refactoring two .jsp pages to place some common code to a Java class. The purpose of the common code is to connect to a database.

Both pages were working well before the change. But I now get an "ResultSet cannot be resolved to a type" error on one page, whereas the other page runs without error. I do not understand why, as I think I made similar changes to both pages.

Could anyone help me explain why the first page does not work, while the first one does?

Here is the page that no longer works after the change:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-GB" xml:lang="en-GB">
<%@page pageEncoding="utf-8" %>

<head>
    <meta charset="UTF-8" /> 
    <title>Forum</title>
    <link rel="stylesheet" type="text/css" href="stocktails.css" />
</head>
<body>

    <%@ include file="navigation.html" %>

    <div id="maindiv">
        <header>
            <h1> Discussion forum </h1>
            <p>Bounce your investing ideas off the community</p>
        </header>

        <div class="left-col">

            <div id='post'>
                <form action='post.jsp' method="get">
                    User name: <input type="text" name="userName"><br>
                    Password: <input type="password" name="pwd">
                    <textarea id="postText" name="textarea" rows="10" cols="50">Type here</textarea>
                    <input type="submit" value="Submit">
                </form>
            </div>

            <%@ page import="com.stocktails.*" %>
            <%
                dbConnect dbConnection = new dbConnect();
                String sqlStr = "SELECT * FROM Posts ORDER BY Id ASC";
                ResultSet rset = dbConnection.executeQuery(sqlStr);
            %>

            <div id="tablePlaceholder">
                <form method="get" action="forum.jsp">
                    <table class='forumTable'>
          <%
              while (rset.next()) {
          %>
                        <tr>
                            <td class="postDate">Posted on: <%= rset.getDate("Date") %></td>
                            <td class="postId">Post #<%= rset.getInt("Id") %></td>
                        </tr>
                        <tr>
                            <td class="postMember"><%= rset.getString("Member") %></td>
                            <td class="postText"><%= rset.getString("Text") %></td>
                        </tr>
                        <tr>
                            <td class="postEmpty"></td>
                        </tr>
          <%
              }
          %>
                </table>
                <br>
              </form>
            </div>

         <% dbConnection.closeAll(); %>

        </div>

        <div class="right-col">
            <aside class="aside-forum">
                <header>
                    <h1> Forum etiquette </h1>
                    <p> Thou shall not troll </p>
                </header>
                <section>
                    <h1> No promotion </h1>
                    <p> Do not use this forum to promote your own business. </p>
                </section>
                <section>
                    <h1> Keep calm and carry on </h1>
                    <p> Be courteous. Personal disagreements should be handled through email and not through public posts. </p>
                </section>
                <section>
                    <h1> No hijacking </h1>
                    <p> Do not hijack someone else's thread and interrupt a topic of discussion. </p>
                </section>
                <section>
                    <h1> You say /təˈmeɪtoʊ/, we say /təˈmɑːtəʊ/ </h1>
                    <p> Please refrain from using American spelling. </p>
                </section>
            </aside>
        </div>

        <div class="clear"></div>
    </div>

    <%@ include file="footer.html" %>

</body>
</html>

Here is the error message if get:

HTTP Status 500 - Unable to compile class for JSP:

type Exception report

message Unable to compile class for JSP:

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 37 in the jsp file: /forum.jsp
ResultSet cannot be resolved to a type
34:             <%
35:                 dbConnect dbConnection = new dbConnect();
36:                 String sqlStr = "SELECT * FROM Posts ORDER BY Id ASC";
37:                 ResultSet rset = dbConnection.executeQuery(sqlStr);
38:             %>
39: 
40:             <div id="tablePlaceholder">


Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:468)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:657)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
    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:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.52 logs.

Here is the other page that still works even after I moved some code to a separate class:

<html>
<head>
  <title>Quote Query</title>
</head>
<body>
  <h1>retrieve stock quotes</h1>
  <h3>Choose Ticker(s):</h3>
  <form method="get">
    <input type="checkbox" name="ticker" value="AAPL">AAPL
    <input type="checkbox" name="ticker" value="MSFT">MSFT
    <input type="submit" value="Query">
  </form>

  <%
    String[] tickers = request.getParameterValues("ticker");
    if (tickers != null) {
  %>


        <%@ page import="com.stocktails.*" %>
        <%
            dbConnect dbConnection = new dbConnect();

            String sqlStr = "SELECT * FROM Quotes WHERE Ticker IN (";
            sqlStr += "'" + tickers[0] + "'";  // First ticker
            for (int i = 1; i < tickers.length; ++i) {
                sqlStr += ", '" + tickers[i] + "'";  // Subsequent tickers need a leading commas
            }
            sqlStr += ") ORDER BY Date ASC";

            // for debugging
            System.out.println("Query statement is " + sqlStr);

            ResultSet rset = dbConnection.executeQuery(sqlStr);
        %>

      <hr>
      <form method="get" action="quotes.jsp">
        <table border=1 cellpadding=5>
          <tr>
            <th>Ticker</th>
            <th>Date</th>
            <th>PriceOpen</th>
            <th>PriceHigh</th>
            <th>PriceLow</th>
            <th>PriceClose</th>
            <th>Volume</th>
            <th>PriceAdjClose</th>
          </tr>
  <%
      while (rset.next()) {
  %>
          <tr>
            <td><%= rset.getString("Ticker") %></td>
            <td><%= rset.getString("Date") %></td>
            <td>$<%= rset.getInt("PriceOpen") %></td>
            <td><%= rset.getInt("PriceHigh") %></td>
            <td><%= rset.getString("PriceLow") %></td>
            <td><%= rset.getString("PriceClose") %></td>
            <td>$<%= rset.getInt("Volume") %></td>
            <td><%= rset.getInt("PriceAdjClose") %></td>
          </tr>
  <%
      }
  %>
        </table>
        <br>
        <input type="submit" value="Order">
        <input type="reset" value="Clear">
      </form>
      <a href="<%= request.getRequestURI() %>"><h3>Back</h3></a>
  <%
        dbConnection.closeAll();
    }
  %>

    <%@ include file="footer.html" %>
</body>
</html>

Here is the Java class:

import javax.naming.*;
import java.sql.*;
import javax.sql.*;

public class dbConnect
{
    private Context init;
    private Context env;
    private DataSource data;
    private Connection conn;
    private Statement stmt;
    private ResultSet rset;

    public dbConnect()
    {
        try {
            init = new InitialContext();
        }
        catch (NamingException e)
        {
            System.out.println("Cannot create initial environment");
        };

        try {
            env = (Context) init.lookup("java:/comp/env/");
        }
        catch (NamingException e)
        {
            System.out.println("Cannot create context");
        };

        try {
            data = (DataSource) env.lookup("jdbc/mydb");
        }
        catch (NamingException e)
        {
            System.out.println("Cannot create data source");
        };

        try {
            conn = data.getConnection();            
        }
        catch (SQLException e)
        {
            System.out.println("Cannot get connection");
        }

        try {
            stmt = conn.createStatement();          
        }
        catch (SQLException e)
        {
            System.out.println("Cannot create statement");
        }

    }

    public ResultSet executeQuery(String sqlStr)
    {
        try {
            rset = stmt.executeQuery(sqlStr);
        }
        catch (SQLException e)
        {
            System.out.println("Cannot execute query");
        }
        return rset;
    }

    public void closeAll()
    {
        try {
            rset.close();
            stmt.close();
            conn.close();                       
        }
        catch (SQLException e)
        {
            System.out.println("Cannot close connection");
        }
    }
}
Was it helpful?

Solution

You should also import java.sql.ResultSet class in your JSP :

<%@ page import="java.sql.ResultSet" %>

make sure that you included the jdbc JAR.

PS: You should learn how to organize your code , never access database through JSP,Read more about RequestDispatcher,request.setAttribute..., Google about MVC.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top