Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2297855
  • 博文数量: 252
  • 博客积分: 5472
  • 博客等级: 大校
  • 技术积分: 3107
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-17 18:39
文章分类

全部博文(252)

文章存档

2012年(96)

2011年(156)

分类: Java

2011-11-16 10:53:57

  1. public int doGetNewsNum() throws Exception{
  2.             int rscountInt=0;
  3.             ResultSet rscount=null;
  4.             String sqlcount="select count(*) from news ";
  5.             try{
  6.             PreparedStatement psmtcount=conn.prepareStatement(sqlcount);
  7.             rscount = psmtcount.executeQuery();
  8.             }catch(SQLException e){
  9.                 String error="SQLException";
  10.                 throw new Exception(error);
  11.             }catch(Exception e){
  12.                 String error="Exception";
  13.                 throw new Exception(error);
  14.             }    
  15.             rscountInt=rscount.getInt(1);
  16.             return rscountInt;
  17.         }
 
报错
  1. 严重: Servlet.service() for servlet jsp threw exception
  2. java.sql.SQLException: Before start of result set
  3. at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
  4. at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
  5. at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
  6. at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
  7. at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:841)
  8. at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2672)
  9. at data.News.doGetNewsNum(News.java:40)
  10. at org.apache.jsp.testjdbc_jsp._jspService(testjdbc_jsp.java:94)
  11. at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
  12. at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
  13. at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
  14. at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
  15. at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
  16. at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
  17. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
  18. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
  19. at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
  20. at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
  21. at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
  22. at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
  23. at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
  24. at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
  25. at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859)
  26. at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
  27. at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)
  28. at java.lang.Thread.run(Thread.java:619)
原因:
RESULTSET在没进行操作是在第一条数据之前的,所以才有before start,你应该调用next()方法,移动到第一条数据。
 
修改方法:
 
  1. public int doGetNewsNum() throws Exception{
  2.             int rscountInt=0;
  3.             ResultSet rscount=null;
  4.             String sqlcount="select count(*) from news ";
  5.             try{
  6.             PreparedStatement psmtcount=conn.prepareStatement(sqlcount);
  7.             rscount = psmtcount.executeQuery();
  8.             }catch(SQLException e){
  9.                 String error="SQLException";
  10.                 throw new Exception(error);
  11.             }catch(Exception e){
  12.                 String error="Exception";
  13.                 throw new Exception(error);
  14.             }    
  15.             rscount.next();
  16.             rscountInt=rscount.getInt(1);
  17.             return rscountInt;
  18.         }
增加 rscount.next()
阅读(4629) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~