jsp实现简单分页功能(在线等!),问题描述如下。急啊!穷人,只有20分,都给了,请各位大侠帮忙。
我用jsp做了一个简单的表格(eclipse),用jdbc连接的mysql数据库,只实select功能即可,请各位大神帮忙做一个分页功能,不需要12345的分页,只要上页...
我用jsp做了一个简单的表格(eclipse),用jdbc连接的mysql数据库,只实select功能即可,请各位大神帮忙做一个分页功能,不需要12345的分页,只要上页、下页就行,代码和页面截图我都会贴上来,急啊!
我的所有代码:(其中上下页按钮里写了href,可以忽略)
<%@page contentType="text/html;charset=GBK" import="java.sql.*"%>
<html>
<head>
<title>用户注册:使用JDBC处理用户提交数据</title>
<%
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager
.getConnection("jdbc:mysql://localhost:3306/mydb?user=root&password=&useUnicode=true&characterEncoding=GB2312");
Statement stmt = con.createStatement();
String query = "SELECT * FROM userinfo";
// ResultSet rs = stmt.executeQuery(query);
ResultSet rs = stmt.executeQuery(query);
%>
<style type="text/css">
<!--
body,table{
font-size:12px;
}
table{
table-layout:fixed;
empty-cells:show;
border-collapse: collapse;
margin:0 auto;
}
td{
height:25px;
Text-align:center;
}
h1,h2,h3{
font-size:20px;
margin:0;
padding:0;
}
table.t1{
font-family: sans-serif;
border:1px solid #cad9ea;
color:#696969;
}
table.t1 th {
background-image: url(bg_list_title.gif);
background-repeat::repeat-x;
height:24px;
}
table.t1 td,table.t1 th{
border:1px solid #cad9ea;
padding:0 1em 0;
}
table.t1 tr.a1{
background-color:#DCDCDC;
}
#nav {
list-style: none;
display: inline;
}
#nav li {
display: inline;
margin-right: 100px;
float: center;
height: 50px;
width: 30px;
H4 { padding-top: 20px; padding-bottom: 5px; padding-left: 90px; padding-right: 0px }
}
#nav a {
color: #666666;
text-decoration: none;
display: block;
font-size: 15px;
font-family: Geneva, Arial, Helvetica, sans-serif;
border: 1 solid #999;
width: 35px;
height: 10px;
line-height: 20px;
text-align: center;
background-image: url(bg_list_title.gif);
background-repeat::repeat-x;
height:24px;
}
</style>
</head>
<body>
<table width="60%" heigh="10%" id="mytab" border="0" class="t1">
<tr>
<th width="10%">序号</th>
<th>部门名称</th>
<th>数据类型</th>
<th>数据量</th>
</tr>
<%
while (rs.next()) {
%>
<tr>
<td><%=rs.getString("number")%></td>
<td><%=rs.getString("name")%></td>
<td><%=rs.getString("type")%></td>
<td><%=rs.getString("data")%></td>
</tr>
<%
}
%>
</table>
<br>
<ul id="nav">
<li><a href="#">上页</a></li>
<li><a href="#">下页</a></li>
</ul>
<%
rs.close();
stmt.close();
con.close();
} catch (SQLException sqle) {
out.println("sqle=" + sqle);
} finally {
}
%>
</body>
</html>
路径截图
已做成的页面截图 展开
我的所有代码:(其中上下页按钮里写了href,可以忽略)
<%@page contentType="text/html;charset=GBK" import="java.sql.*"%>
<html>
<head>
<title>用户注册:使用JDBC处理用户提交数据</title>
<%
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager
.getConnection("jdbc:mysql://localhost:3306/mydb?user=root&password=&useUnicode=true&characterEncoding=GB2312");
Statement stmt = con.createStatement();
String query = "SELECT * FROM userinfo";
// ResultSet rs = stmt.executeQuery(query);
ResultSet rs = stmt.executeQuery(query);
%>
<style type="text/css">
<!--
body,table{
font-size:12px;
}
table{
table-layout:fixed;
empty-cells:show;
border-collapse: collapse;
margin:0 auto;
}
td{
height:25px;
Text-align:center;
}
h1,h2,h3{
font-size:20px;
margin:0;
padding:0;
}
table.t1{
font-family: sans-serif;
border:1px solid #cad9ea;
color:#696969;
}
table.t1 th {
background-image: url(bg_list_title.gif);
background-repeat::repeat-x;
height:24px;
}
table.t1 td,table.t1 th{
border:1px solid #cad9ea;
padding:0 1em 0;
}
table.t1 tr.a1{
background-color:#DCDCDC;
}
#nav {
list-style: none;
display: inline;
}
#nav li {
display: inline;
margin-right: 100px;
float: center;
height: 50px;
width: 30px;
H4 { padding-top: 20px; padding-bottom: 5px; padding-left: 90px; padding-right: 0px }
}
#nav a {
color: #666666;
text-decoration: none;
display: block;
font-size: 15px;
font-family: Geneva, Arial, Helvetica, sans-serif;
border: 1 solid #999;
width: 35px;
height: 10px;
line-height: 20px;
text-align: center;
background-image: url(bg_list_title.gif);
background-repeat::repeat-x;
height:24px;
}
</style>
</head>
<body>
<table width="60%" heigh="10%" id="mytab" border="0" class="t1">
<tr>
<th width="10%">序号</th>
<th>部门名称</th>
<th>数据类型</th>
<th>数据量</th>
</tr>
<%
while (rs.next()) {
%>
<tr>
<td><%=rs.getString("number")%></td>
<td><%=rs.getString("name")%></td>
<td><%=rs.getString("type")%></td>
<td><%=rs.getString("data")%></td>
</tr>
<%
}
%>
</table>
<br>
<ul id="nav">
<li><a href="#">上页</a></li>
<li><a href="#">下页</a></li>
</ul>
<%
rs.close();
stmt.close();
con.close();
} catch (SQLException sqle) {
out.println("sqle=" + sqle);
} finally {
}
%>
</body>
</html>
路径截图
已做成的页面截图 展开
3个回答
展开全部
分页的方式有很多,简单跟你说下方法.进入这个显示的页面的时候显示的是第一页,你要想好一页显示多少条数据,比如这个是9条.那么查询数据库的时候根据不同的数据库试用不同的查询语句,MySQL应该是limit 1,9.第一页显示查询到的1-9条数据.当你按下下页的时候就查询10-18条数据然后显示就可以了.注意当最后一页 再次点击下页时,仍然显示最后一页,同理第一页点击上页时还显示第一页.指示到这应该能自己做出来了
更多追问追答
追问
恩恩 大概的思想我懂了 这门语言我是初学者 很多东西没有系统的学过 因为这个页面要的比较急 所以上来求助 您能不能帮我具体化一下 比如具体的语句 具体的位置
追答
之前的查询是查询全部的数据,现在是用Limit 得到部分数据.
INT M=1,N=9;
String query = "SELECT * FROM userinfo LIMIT "+M+","+N;
点击下页时,M,N+9,上页时就-9;小于0时M=1.还得查询最大数据多少行,大于最大行就等于最大行
展开全部
SELECT
*
FROM
Account
WHERE
(usertype='base' or usertype='home' or usertype='salse')
and logindate is not null order by logindate desc
LIMIT 起始行, 每页多少行
LIMIT 接受一个或两个数字参数。
参数必须是一个整数常量。
如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,
第二个参数指定返回记录行的最大数目。
初始记录行的偏移量是 0(而不是 1)
*
FROM
Account
WHERE
(usertype='base' or usertype='home' or usertype='salse')
and logindate is not null order by logindate desc
LIMIT 起始行, 每页多少行
LIMIT 接受一个或两个数字参数。
参数必须是一个整数常量。
如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,
第二个参数指定返回记录行的最大数目。
初始记录行的偏移量是 0(而不是 1)
追问
这段是加在jdbc里的代码吧? 能写的再具体点儿么 还有上下页按钮的功能代码应该怎么写? 谢谢了?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
给你个直接可以用的吧,需要自己配置几个文件,然后直接套用代码就行了。
一.webcontend层
1.webcontent/common/meta.jsp
代码:
<base href="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}/">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
2.webcontent/page/page.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c"%>
<c:set var="page" value="${sessionScope.page}" />
<c:set var="path" value="${pageContext.request.contextPath}" />
<c:set var="url" value="${param.url}" />
<c:set var="urlParams" value="${param.urlParams}" />
<c:set var="pathurl" value="${path}/${url}" />
<tr align="center">
共${page.totalCount}条记录 共${page.totalPage}页 每页显示${page.everyPage}条
当前第${page.currentPage}页
<c:choose>
<c:when test="${page.hasPrePage eq false}">
<<首页 <上页
</c:when>
<c:otherwise>
<a href="${pathurl}?&pageAction=first${urlParams}"><<首页 </a>
<a href="${pathurl}?pageAction=previous${urlParams}" /><上一页</a>
</c:otherwise>
</c:choose>
||
<c:choose>
<c:when test="${page.hasNextPage eq false}">
下页> 尾页>>
</c:when>
<c:otherwise>
<a href="${pathurl}?&pageAction=next${urlParams}">下一页> </a>
<a href="${pathurl}?pageAction=last${urlParams}" />末页>></a>
</c:otherwise>
</c:choose>
<SELECT name="indexChange" id="indexChange"
onchange="getCurrentPage(this.value);">
<c:forEach var="index" begin="1" end="${page.totalPage}" step="1">
<option value="${index}" ${page.currentPage eq index ? "selected" : ""}>
第${index}页
</option>
</c:forEach>
</SELECT>
每页显示:<select name="everyPage" id="everyPage" onchange="setEveryPage(this.value);">
<c:forEach var="pageCount" begin="5" end="${page.totalCount}" step="5">
<option value="${pageCount}" ${page.everyPage eq pageCount ? "selected" : ""}>
${pageCount}条
</option>
</c:forEach>
</select>
</tr>
<div style='display: none'>
<a class=listlink id="indexPageHref" href='#'></a>
</div>
<script>
function getCurrentPage(index){
var a = document.getElementById("indexPageHref");
a.href = '${pathurl}?pageAction=gopage&pageKey='+index+'${urlParams}';
a.setAttribute("onclick",'');
a.click("return false");
}
function setEveryPage(everyPage){
var a = document.getElementById("indexPageHref");
var currentPage = document.getElementById('indexChange').value;
a.href = '${pathurl}?pageAction=setpage&pageKey='+everyPage+'${urlParams}';
a.setAttribute("onclick",'');
a.click("return false");
}
function sortPage(sortName){
var a = document.getElementById("indexPageHref");
a.href = '${pathurl}?pageAction=sort&pageKey='+sortName+'${urlParams}';
a.setAttribute("onclick",'');
a.click("return false");
}
</script>
二.java源码曾
3.controller 里
代码:
@RequestMapping(value="xxxxx.htm",method=RequestMethod.GET)
public ModelAndView list(HttpServletRequest request) {
Long totalCount = new Long(registerService.pageCounts());
//System.out.println(totalCount);
String sqlStr = "select * from 表名 where sex='男'";
Page page = executePage(request,sqlStr,totalCount,"id desc");
List<UserAll(pojo)> users = registerService.pageList(page.getQuerySql());
return new ModelAndView("register/register_list","users",users);
}
2.service层
代码:
public int pageCounts() {
// TODO Auto-generated method stub
return registerDao.pageCounts();
}
public List<UserAll> pageList(String querySql) {
return registerDao.pageList(querySql);
}
3.dao层
public int pageCounts() {
String sqlStr = "select count(*) from register";
return jdbcTemplate.queryForInt(sqlStr, new Object[] {});
}
public List<UserAll> pageList(String querySql) {
// TODO Auto-generated method stub
return simpleJdbcTemplate.query(querySql, new BeanPropertyRowMapper<UserAll>(UserAll.class));
}
三.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<head>
<%@ include file="/common/meta.jsp"%>
</head>
<html>
<body>
<jsp:include page="/page/page.jsp">
<jsp:param name="url" value="registerlist.htm" />
</jsp:include>
</body>
</html>
一.webcontend层
1.webcontent/common/meta.jsp
代码:
<base href="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}/">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
2.webcontent/page/page.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c"%>
<c:set var="page" value="${sessionScope.page}" />
<c:set var="path" value="${pageContext.request.contextPath}" />
<c:set var="url" value="${param.url}" />
<c:set var="urlParams" value="${param.urlParams}" />
<c:set var="pathurl" value="${path}/${url}" />
<tr align="center">
共${page.totalCount}条记录 共${page.totalPage}页 每页显示${page.everyPage}条
当前第${page.currentPage}页
<c:choose>
<c:when test="${page.hasPrePage eq false}">
<<首页 <上页
</c:when>
<c:otherwise>
<a href="${pathurl}?&pageAction=first${urlParams}"><<首页 </a>
<a href="${pathurl}?pageAction=previous${urlParams}" /><上一页</a>
</c:otherwise>
</c:choose>
||
<c:choose>
<c:when test="${page.hasNextPage eq false}">
下页> 尾页>>
</c:when>
<c:otherwise>
<a href="${pathurl}?&pageAction=next${urlParams}">下一页> </a>
<a href="${pathurl}?pageAction=last${urlParams}" />末页>></a>
</c:otherwise>
</c:choose>
<SELECT name="indexChange" id="indexChange"
onchange="getCurrentPage(this.value);">
<c:forEach var="index" begin="1" end="${page.totalPage}" step="1">
<option value="${index}" ${page.currentPage eq index ? "selected" : ""}>
第${index}页
</option>
</c:forEach>
</SELECT>
每页显示:<select name="everyPage" id="everyPage" onchange="setEveryPage(this.value);">
<c:forEach var="pageCount" begin="5" end="${page.totalCount}" step="5">
<option value="${pageCount}" ${page.everyPage eq pageCount ? "selected" : ""}>
${pageCount}条
</option>
</c:forEach>
</select>
</tr>
<div style='display: none'>
<a class=listlink id="indexPageHref" href='#'></a>
</div>
<script>
function getCurrentPage(index){
var a = document.getElementById("indexPageHref");
a.href = '${pathurl}?pageAction=gopage&pageKey='+index+'${urlParams}';
a.setAttribute("onclick",'');
a.click("return false");
}
function setEveryPage(everyPage){
var a = document.getElementById("indexPageHref");
var currentPage = document.getElementById('indexChange').value;
a.href = '${pathurl}?pageAction=setpage&pageKey='+everyPage+'${urlParams}';
a.setAttribute("onclick",'');
a.click("return false");
}
function sortPage(sortName){
var a = document.getElementById("indexPageHref");
a.href = '${pathurl}?pageAction=sort&pageKey='+sortName+'${urlParams}';
a.setAttribute("onclick",'');
a.click("return false");
}
</script>
二.java源码曾
3.controller 里
代码:
@RequestMapping(value="xxxxx.htm",method=RequestMethod.GET)
public ModelAndView list(HttpServletRequest request) {
Long totalCount = new Long(registerService.pageCounts());
//System.out.println(totalCount);
String sqlStr = "select * from 表名 where sex='男'";
Page page = executePage(request,sqlStr,totalCount,"id desc");
List<UserAll(pojo)> users = registerService.pageList(page.getQuerySql());
return new ModelAndView("register/register_list","users",users);
}
2.service层
代码:
public int pageCounts() {
// TODO Auto-generated method stub
return registerDao.pageCounts();
}
public List<UserAll> pageList(String querySql) {
return registerDao.pageList(querySql);
}
3.dao层
public int pageCounts() {
String sqlStr = "select count(*) from register";
return jdbcTemplate.queryForInt(sqlStr, new Object[] {});
}
public List<UserAll> pageList(String querySql) {
// TODO Auto-generated method stub
return simpleJdbcTemplate.query(querySql, new BeanPropertyRowMapper<UserAll>(UserAll.class));
}
三.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<head>
<%@ include file="/common/meta.jsp"%>
</head>
<html>
<body>
<jsp:include page="/page/page.jsp">
<jsp:param name="url" value="registerlist.htm" />
</jsp:include>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询