我试图返回mysql行的数量。运行.jsp文件时出现以下错误:
org.apache.jasper.JasperException:处理发生异常 第54行的JSP页面/index.jsp
这行代码是什么:
// count posts in category 1
catCount = conn.countCategoryPosts(1);
我知道数据库连接设置正常。
这是countCategoryPosts
方法。
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);
}
导致错误的原因是什么?
编辑:根据要求提供完整的堆栈跟踪:
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)
答案 0 :(得分:1)
你不能打电话
rs.getInt(1)
如果你没有用
移动光标rs.next() // for example
这应该有效:
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;
}