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