执行SQL:SELECT count(*) FROM tb_person 时发生异常:Before start of result set 。 怎么办?

<%@pagelanguage="java"pageEncoding="UTF-8"contentType="text/html;charset=UTF-8"%><%@p... <%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8"%>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.ResultSet" %>
<jsp:directive.page import="java.sql.Date"/>
<jsp:directive.page import="java.sql.Timestamp"/>
<jsp:directive.page import="java.sql.SQLException"/>
<jsp:directive.page import="com.helloweenvsfei.util.DbManager"/>
<jsp:directive.page import="java.sql.PreparedStatement"/>
<jsp:directive.page import="com.helloweenvsfei.util.Pagination"/>

<%
final int pageSize = 10; //一页显示10条记录
int pageNum = 1; //当前的页数
int pageCount = 1; //总页数
int recordCount = 0; //总记录数

try {
pageNum = Integer.parseInt(request.getParameter("pageNum"));
} catch(Exception e) {}

String sql = null;

Connection conn = null;
PreparedStatement preStmt = null;
ResultSet rs = null;

try {
sql = "SELECT count(*) FROM tb_person ";
recordCount = DbManager.getCount(sql);
pageCount = (recordCount + pageSize - 1) / pageSize;

int startRecord = (pageNum - 1) * pageSize;

sql = "SELECT * FROM tb_person LIMIT ?, ? ";

conn = DbManager.getConnection();
preStmt = conn.prepareStatement(sql);
DbManager.setParams(preStmt,startRecord,pageSize);

rs = preStmt.executeQuery();

%>

package com.helloweenvsfei.util;
import java.sql.*;
public class DbManager {
public static Connection getConnection() throws SQLException {
return getConnection("databaseWeb","root","admin");
}

public static Connection getConnection(String dbName,String userName,String password) //getConnection()函数重载
throws SQLException {
String url = "jdbc:mysql://localhost:3306/" + dbName + "?useUnicode=true&characterEncoding=utf-8";

DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //注册驱动

return DriverManager.getConnection(url, userName, password); //建立数据库连接
}

public static int getCount(String sql) throws SQLException {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
return rs.getInt(1);
} finally {
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(conn != null) conn.close();
}
}
}
异常信息:
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:2674)
at com.helloweenvsfei.util.DbManager.getCount(DbManager.java:68)
at org.apache.jsp.listPagedPerson_jsp._jspService(listPagedPerson_jsp.java:90)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at java.lang.Thread.run(Thread.java:619)
展开
 我来答
khg305387543
推荐于2017-09-23 · TA获得超过183个赞
知道小有建树答主
回答量:197
采纳率:0%
帮助的人:139万
展开全部
try {
conn = getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
return rs.getInt(1);
} finally {
里的return rs.getInt(1);没用
一般都是先在外面定义一个变量 int a;
然后if(rs.next()){a = rs.getInt(1);}
然后在finally里 renturn a;
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友1f8560255
2011-08-23 · TA获得超过108个赞
知道答主
回答量:109
采纳率:0%
帮助的人:55万
展开全部
最好能把异常信息也贴出来
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式