-1

I am attempting to return the count of mysql rows. I get the following error when I run the .jsp file:

org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 54

Which is this line of code:

// count posts in category 1
catCount = conn.countCategoryPosts(1);

I know the database connection is setup fine. This is the countCategoryPosts method.

public int countCategoryPosts(int ncatID) throws Exception{
    int catID = ncatID;
    try{
        sql = "SELECT COUNT(*) FROM crm_posts WHERE cat_id = ?";
        prep = conn.prepareStatement(sql);
        prep.setInt(1, catID);
        rs = prep.executeQuery();
    }catch(Exception e){
        e.printStackTrace();
    }
    return rs.getInt(1);
}

What is causing the error?

EDIT: As requested the full stacktrace:

type Exception report

message An exception occurred processing JSP page /index.jsp at line 54

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

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 54

51:                         id = Integer.parseInt(catAttributes[1]);
52:                         
53:                         // count posts in this category
54:                         catCount = conn.countCategoryPosts(1);
55:                         
56:                         // get posts in this category
57:                         postList = conn.getCategoryPosts(id);


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
    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)
    org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:176)
    org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
    org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
    org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:394)
root cause

javax.servlet.ServletException: java.sql.SQLException: Before start of result set
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:916)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:845)
    org.apache.jsp.index_jsp._jspService(index_jsp.java:208)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    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:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:176)
    org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
    org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
    org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:394)
root cause

java.sql.SQLException: Before start of result set
    com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1084)
    com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
    com.mysql.jdbc.SQLError.createSQLException(SQLError.java:973)
    com.mysql.jdbc.SQLError.createSQLException(SQLError.java:918)
    com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:850)
    com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2705)
    com.servlet.explore.dbCon.countCategoryPosts(dbCon.java:103)
    org.apache.jsp.index_jsp._jspService(index_jsp.java:145)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    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:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:176)
    org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
    org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
    org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:394)
crmepham
  • 4,676
  • 19
  • 80
  • 155

1 Answers1

1

You can not call

rs.getInt(1)

if you did not move your cursor with

rs.next() // for example

This should work:

public int countCategoryPosts(int ncatID) throws Exception{
    int catID = ncatID;
    int result = 0;
    try{
        sql = "SELECT COUNT(*) FROM crm_posts WHERE cat_id = ?";
        prep = conn.prepareStatement(sql);
        prep.setInt(1, catID);
        rs = prep.executeQuery();
        if (rs.next()) {
           result = rs.getInt(1);
        }
    }catch(Exception e){
        e.printStackTrace();
    }
    return result;
}
lpratlong
  • 1,421
  • 9
  • 17