jsp实现数据库数据分页显示,大家帮我看一下我的代码为什么,下一页跳到3就点不动呢?上一页都没有问题 50
<body><tableborder="3"><tr><tdwidth=120>用户名</td><tdwidth=120>密码</td><tdwidth=150>邮箱</...
<body>
<table border="3">
<tr>
<td width=120>用户名</td>
<td width=120>密码</td>
<td width=150>邮箱</td>
<td width=100>姓名</td>
<td width=120>电话</td>
</tr>
<%!
public static final String DRIVER = "com.mysql.jdbc.Driver";
public static final String USER = "root";
public static final String PASS = "";
public static final String URL = "jdbc:mysql://localhost:3306/my?characterEncoding=utf8";
//每页显示的记录数
public static final int PAGESIZE = 3;
//显示的总页数
int pageCount;
//当前显示的页数(默认第一页)
int curPage = 1;
%>
<%
try {
//连接数据库
Class.forName(DRIVER);
Connection con = DriverManager.getConnection(URL, USER, PASS);
//查询所有记录
String sql = "SELECT * from userinfo";
PreparedStatement stat = con
.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stat.executeQuery();
//获取总记录数
rs.last();
int size = rs.getRow();
//计算总页数
if (size % PAGESIZE == 0) {
pageCount = size / PAGESIZE;
} else {
pageCount = size / PAGESIZE + 1;
}
//获取当前显示的页数
String cur = request.getParameter("curPage");
if (cur == null) {
cur = "1";
}
curPage = Integer.parseInt(cur);
if (curPage >= pageCount) {
curPage = pageCount;
} else if (curPage <= 0) {
curPage = 1;
} else {
;
}
//将光标移动到结果集指定行编号
boolean flag = rs.absolute((curPage - 1) * PAGESIZE + 1);
//获取结果集指定行记录
int count = PAGESIZE;
do {
if (count == 0) {
break;
}
%>
<tr>
<td><%=rs.getString(1)%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
<td><%=rs.getString(4)%></td>
<td><%=rs.getString(5)%></td>
</tr>
<%
count--;
} while (rs.next());
rs.close();
stat.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
</table>
<a href="JspPaging.jsp?curPage=1">首页</a>
<%
if (curPage == 1) {
%>
<a href="JspPaging.jsp?curPage=<%=curPage%>">上一页</a>
<%
} else {
%>
<a href="JspPaging.jsp?curPage=<%=curPage - 1%>">上一页</a>
<%
}
%>
<%
if (curPage == PAGESIZE) {
%>
<a href="JspPaging.jsp?curPage=<%=curPage%>">下一页</a>
<%
} else {
%>
<a href="JspPaging.jsp?curPage=<%=curPage + 1%>">下一页</a>
<%
}
%>
<a href="JspPaging.jsp?curPage=<%=pageCount%>">尾页</a> 第<%=curPage%>页/共<%=pageCount%>页
</body> 展开
<table border="3">
<tr>
<td width=120>用户名</td>
<td width=120>密码</td>
<td width=150>邮箱</td>
<td width=100>姓名</td>
<td width=120>电话</td>
</tr>
<%!
public static final String DRIVER = "com.mysql.jdbc.Driver";
public static final String USER = "root";
public static final String PASS = "";
public static final String URL = "jdbc:mysql://localhost:3306/my?characterEncoding=utf8";
//每页显示的记录数
public static final int PAGESIZE = 3;
//显示的总页数
int pageCount;
//当前显示的页数(默认第一页)
int curPage = 1;
%>
<%
try {
//连接数据库
Class.forName(DRIVER);
Connection con = DriverManager.getConnection(URL, USER, PASS);
//查询所有记录
String sql = "SELECT * from userinfo";
PreparedStatement stat = con
.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stat.executeQuery();
//获取总记录数
rs.last();
int size = rs.getRow();
//计算总页数
if (size % PAGESIZE == 0) {
pageCount = size / PAGESIZE;
} else {
pageCount = size / PAGESIZE + 1;
}
//获取当前显示的页数
String cur = request.getParameter("curPage");
if (cur == null) {
cur = "1";
}
curPage = Integer.parseInt(cur);
if (curPage >= pageCount) {
curPage = pageCount;
} else if (curPage <= 0) {
curPage = 1;
} else {
;
}
//将光标移动到结果集指定行编号
boolean flag = rs.absolute((curPage - 1) * PAGESIZE + 1);
//获取结果集指定行记录
int count = PAGESIZE;
do {
if (count == 0) {
break;
}
%>
<tr>
<td><%=rs.getString(1)%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
<td><%=rs.getString(4)%></td>
<td><%=rs.getString(5)%></td>
</tr>
<%
count--;
} while (rs.next());
rs.close();
stat.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
</table>
<a href="JspPaging.jsp?curPage=1">首页</a>
<%
if (curPage == 1) {
%>
<a href="JspPaging.jsp?curPage=<%=curPage%>">上一页</a>
<%
} else {
%>
<a href="JspPaging.jsp?curPage=<%=curPage - 1%>">上一页</a>
<%
}
%>
<%
if (curPage == PAGESIZE) {
%>
<a href="JspPaging.jsp?curPage=<%=curPage%>">下一页</a>
<%
} else {
%>
<a href="JspPaging.jsp?curPage=<%=curPage + 1%>">下一页</a>
<%
}
%>
<a href="JspPaging.jsp?curPage=<%=pageCount%>">尾页</a> 第<%=curPage%>页/共<%=pageCount%>页
</body> 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询