java的分页显示数据?数据库用的mysql.
3个回答
2013-08-23
展开全部
实现分页功能跟使用哪种数据库是没有关系的,要实现分页你需要设置几个值来限定你要显示的记录,比如“当前页,上一页,下一页,每页记录的条数”,所以每次发送请求到服务器的时候要根据这些条件对查询出来的所有数据进行截取,如果你知道怎么使用hibernate的话,那就更简单了,hibernate有对分页进行处理的方法。如果你有兴趣的话,我可以给你点具体的代码看看,发到你邮箱里。
推荐于2017-09-05
展开全部
举例 : 表名t_name 主键id
JSP文件部分 核心代码
<%
int pageSize = 3;
String strPageNo = request.getParameter("pageNo");
int pageNo;
if (strPageNo == null || strPageNo.equals("")) {
pageNo = 1;
} else {
try {
pageNo = Integer.parseInt(strPageNo.trim());
} catch (NumberFormatException e) {
pageNo = 1;
}
if (pageNo <= 0)
pageNo = 1;
}
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/bbs?user=root&password=root";
Connection conn = DriverManager.getConnection(url);
Statement stmtCount = conn.createStatement();
ResultSet rsCount = stmtCount
.executeQuery("select count(*) from t_name where id = 0");
rsCount.next();
int totalRecords = rsCount.getInt(1);
int totalPages = totalRecords % pageSize == 0 ? totalRecords
/ pageSize : totalRecords / pageSize + 1;
if (pageNo > totalPages)
pageNo = totalPages;
int startPos = (pageNo - 1) * pageSize;
Statement stmt = conn.createStatement();
ResultSet rs = stmt
.executeQuery("select * from t_name where id = 0 order by pdate desc limit "
+ startPos + "," + pageSize);
%>
共<%=totalPages%>页 第<%=pageNo%>页
<a href="ShowArticleFlat.jsp?pageNo=<%=pageNo-1%>"> < </a>
<a href="ShowArticleFlat.jsp?pageNo=<%=pageNo+1%>"> > </a>
<form name="form1" action="ShowArticleFlat.jsp">
<select name="pageNo" onchange="document.form1.submit()">
<%
for(int i=1; i<=totalPages; i++) {
%>
<option value=<%=i%> <%=(pageNo == i) ? "selected" : ""%>> 第<%=i%>页
<%
}
%>
</select>
</form>
JSP文件部分 核心代码
<%
int pageSize = 3;
String strPageNo = request.getParameter("pageNo");
int pageNo;
if (strPageNo == null || strPageNo.equals("")) {
pageNo = 1;
} else {
try {
pageNo = Integer.parseInt(strPageNo.trim());
} catch (NumberFormatException e) {
pageNo = 1;
}
if (pageNo <= 0)
pageNo = 1;
}
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/bbs?user=root&password=root";
Connection conn = DriverManager.getConnection(url);
Statement stmtCount = conn.createStatement();
ResultSet rsCount = stmtCount
.executeQuery("select count(*) from t_name where id = 0");
rsCount.next();
int totalRecords = rsCount.getInt(1);
int totalPages = totalRecords % pageSize == 0 ? totalRecords
/ pageSize : totalRecords / pageSize + 1;
if (pageNo > totalPages)
pageNo = totalPages;
int startPos = (pageNo - 1) * pageSize;
Statement stmt = conn.createStatement();
ResultSet rs = stmt
.executeQuery("select * from t_name where id = 0 order by pdate desc limit "
+ startPos + "," + pageSize);
%>
共<%=totalPages%>页 第<%=pageNo%>页
<a href="ShowArticleFlat.jsp?pageNo=<%=pageNo-1%>"> < </a>
<a href="ShowArticleFlat.jsp?pageNo=<%=pageNo+1%>"> > </a>
<form name="form1" action="ShowArticleFlat.jsp">
<select name="pageNo" onchange="document.form1.submit()">
<%
for(int i=1; i<=totalPages; i++) {
%>
<option value=<%=i%> <%=(pageNo == i) ? "selected" : ""%>> 第<%=i%>页
<%
}
%>
</select>
</form>
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-08-23
展开全部
在项目中写个公共类Page,其中有allpage 共几页,allcount 共几条数据,percount 每页几条数据,nowpage 当前页 ,inpage 前一页,nextpage 后一页。这几个属性
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |