想要做分页先熟悉数据库,咋们用mysql的sqlserver的以后弄。
看看效果
1.连接数据库大家都应该会吧:分页mysql相对sqlserver简单,
分页mysql例如:
"select * from name limit "+m+","+n+""
; 其中m是指记录开始的index,从m开始,n表示几条记录
2.创建分页的javabean
public class Page {
private int totalPagecount=1;//总页数
private int pagesize=10;//每一页显示的记录数;
private int totalcount=0;//记录总数;
private int currpageno=1;//当前页码;
List<Name> namesList;//实体集合
public int getTotalPagecount() {
return totalPagecount;
}
public void setTotalPagecount(int totalPagecount) {
this.totalPagecount = totalPagecount;
}
public int getPagesize() {
return pagesize;
}
public void setPagesize(int pagesize) {
if(this.pagesize>0)
this.pagesize = pagesize;
}
public int getTotalcount() {
return totalcount;
}
public void setTotalcount(int totalcount) {
if(totalcount>0)
this.totalcount = totalcount;
//计算总页数
totalPagecount=totalcount%pagesize==0?(totalcount/pagesize)
:totalcount/pagesize+1;
}
public int getCurrpageno() {
return currpageno;
}
public void setCurrpageno(int currpageno) {
this.currpageno = currpageno;
}
public List<Name> getNamesList() {
return namesList;
}
public void setNamesList(List<Name> namesList) {
this.namesList = namesList;
}
}
3.创建对象实体
//我的对象就3条记录大家根据自己数据库创建
public class Name {
private int id;
private String name;
private String img;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
}
4.下来应该从数据库查表多少条数据和显示的数据了
4.1查询对少条记录的mysql
select count(*) as tc from name
4.2连接数据库查找数据的dao如果不会连接数据库看这里 数据库连接
public class PageDao {
private static ComboPooledDataSource datasource;
private static Connection con;
static {
datasource=new ComboPooledDataSource();
try {
con=datasource.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
* 获取总条数
*/
public int getTotalCount() {
String sql="select count(*) as tc from name";
PreparedStatement ps;
int totalcount = 0;
try {
ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while(rs.next()) {
totalcount=rs.getInt("tc");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return totalcount;
}
/*
* pagesize//每一页显示的记录数
* pageno //页码
*/
public List<Name> getPageNameList(int pageno,int pageSize) throws SQLException{
String sql="select * from name limit "+pageSize*(pageno-1)+","+pageSize+"";
//从多少个显示,显示多少个
PreparedStatement ps=con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
List <Name> listname = new ArrayList<>();
while(rs.next()) {
Name name=new Name();
name.setId(rs.getInt("id"));
name.setName(rs.getString("name"));
name.setImg(rs.getString("image"));
listname.add(name);
}
return listname;
}
}
5,接下来去servlet里面去掉用对应的方法,这个方法已经把对象传给前面也面了。也面用el表达式获取值。
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import huomin.Dao.PageDao;
import huomin.entity.Name;
import huomin.entity.Page;
@WebServlet("/NewFile")
public class NewFileServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String currpageno = req.getParameter("page");
String pageSize = req.getParameter("pageSize");
Page p=new Page();
if(pageSize!=null|| pageSize!="") {
p.setPagesize(Integer.parseInt(pageSize));
}
System.out.println("3333333333333333333333333333=="+currpageno);
if(currpageno==null||currpageno=="") {
p.setCurrpageno(1);
}else {
p.setCurrpageno(Integer.parseInt(currpageno));
}
PageDao pd=new PageDao();
System.out.println(pd.getTotalCount());
p.setTotalcount((pd.getTotalCount()));
List<Name> pageList=new ArrayList<>();
try {
pageList = pd.getPageNameList(p.getCurrpageno(), p.getPagesize());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// TODO Auto-generated method stub
req.setAttribute( "Page",p);
req.setAttribute( "pageList",pageList);
req.getRequestDispatcher("/NewFile.jsp").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(req, resp);
}
}
6,下来就是在jsp也面获取,我也面做得非常简单,不喜欢做前端功能基本都实现了
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
<style type="text/css">
*{
margin: 0;padding: 0;}
.main{
height: 600px;
width: 1000px;
}
.sj{
height: 500px;
width: 999.9px;
}
.fy{
height: 99.9px;
width: 999.9px;
}
</style>
</head>
<body>
<script type="text/javascript">
function to_page(pag){
if(pag){
var zys=${Page.totalPagecount}
if(pag>0&&pag<=zys){
var input = document.getElementById("page");
input.value=pag;
}
}
var form = document.getElementById('form');
form.submit();
}
function pagee(data){
var thisoblect=data.value;
var input = document.getElementById("page");
if(thisoblect>0&&thisoblect<=${Page.totalPagecount}){
input.value=thisoblect;
}
to_page();
}
</script>
<form id="form" action="NewFile" method="post" >
<div class="main">
<div class="sj">
<table style="height: 100%;width: 100%">
<tr>
<td>id</td>
<td>name</td>
<td>image</td>
</tr>
<c:forEach items="${pageList}" var="pageList">
<tr>
<td>${pageList.id}</td>
<td>${pageList.name}</td>
<td>${pageList.img}</td>
</tr>
</c:forEach>
</table>
</div>
<div class="fy">
总共[<B>${Page.totalcount}</B>]记录,[<B>${Page.totalPagecount}</B>]页,
,每页显示
<select onchange="to_page()" name="pageSize" id="pageSize">
<option value="30" <c:if test="${Page.pagesize==30}">selected</c:if>>30</option>
<option value="10" <c:if test="${Page.pagesize==10}">selected</c:if>>10</option>
</select>
条
当前页码<input readonly="readonly" size="3" name="page" id="page" type="text" style="border: 0" value="${Page.currpageno}">
[<button style="border:0;background-color: ;" onclick="javascript:to_page(${Page.currpageno-1})">前一页</button>]
<c:choose>
<c:when test="${Page.totalPagecount<=5}">
<c:set var="begin" value="1"/>
<c:set var="end" value="${Page.totalPagecount}"/>
</c:when>
<c:when test="${Page.totalPagecount>5}">
<c:if test="${Page.currpageno<=3}">
<c:set var="begin" value="1"/>
<c:set var="end" value="5"/>
</c:if>
<c:if test="${Page.currpageno>3}">
<c:set var="begin" value="${Page.currpageno-2}"/>
<c:if test="${Page.totalPagecount>Page.currpageno+2}">
<c:set var="end" value="${Page.currpageno+2}"/>
</c:if>
<c:if test="${Page.totalPagecount<=Page.currpageno+2}">
<c:set var="end" value="${Page.totalPagecount}"/>
</c:if>
</c:if>
</c:when>
</c:choose>
<c:forEach begin="${begin}" end="${end}" var="v" >
<input onclick="pagee(this)" readonly="readonly" size="3" type="text" style="border: 0" value="${v}">
</c:forEach>
<c:if test="${end<Page.totalPagecount }">
<span>...</span>
</c:if>
[<button style="border:0;background-color: ;" onclick="javascript:to_page(${Page.currpageno+1})">后一页</button>]
</div>
</div>
</form>
</body>
</html>
;这就一个分页结束。