java仿谷歌分页

昨天写了几个分页后,今天仿写谷歌的分页!其中遇到了算法和对象封装的问题,其中重要的是算法的选择。

下面分享一下我的主要代码:

javaBean中的Student和Pagelei。

student类:

Code:
import java.io.Serializable;

public class Student implements Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;

//成员属性
private int id; //递增的id序列
private String name;//名称
private int age;//年龄
private String email;//邮箱


public Student(){

}

public Student(String name,int age,String email){
this.name=name;
this.age=age;
this.email=email;
}

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 int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}



}

Page类:

Code:
import java.util.List;

public class Page {

private int nowpage;//当前页
private int countrecord;//总记录数
private int countpage;//总页数


public static final int PAGESIZE=5;//每页显示的记录数


private int sumindex =6;//索引的sum值
private int startindex = 1;//开始的索引值
private int endindex = 3;//结束的索引值


private List allentities;

public Page(){

}

public void index(int nowpage){ //可变
/*计算出索引的位置
*
* */
this.endindex = nowpage+2;
if(this.endindex-5>0){
if(this.endindex <= this.countpage){
this.startindex = this.endindex-5;
}else{
this.endindex = this.countpage;
this.startindex = this.endindex-5;
}
}else{
this.startindex = 1;
}
}

public int getNowpage() {
return nowpage;
}
public void setNowpage(int nowpage) {
this.nowpage = nowpage;
}
public int getCountrecord() {
return countrecord;
}
public void setCountrecord(int countrecord) {
this.countrecord = countrecord;
}
public int getCountpage() {
return countpage;
}
public void setCountpage(int countpage) {
this.countpage = countpage;
}
public int getStartindex() {
return startindex;
}
public void setStartindex(int startindex) {
this.startindex = startindex;
}
public int getEndindex() {
return endindex;
}
public void setEndindex(int endindex) {
this.endindex = endindex;
}
public List getAllentities() {
return allentities;
}
public void setAllentities(List allentities) {
this.allentities = allentities;
}

public int getSumindex() {
return sumindex;
}

public void setSumindex(int sumindex) {
this.sumindex = sumindex;
}

}

逻辑处理类:

Code:
public class ListStusServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doPost(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//1.设置编码
req.setCharacterEncoding("utf8");



//2.获取当前页
Page stupage = new Page();
int nowpage=1;
String npage = req.getParameter("nowpage");

if(npage!=null){
nowpage = Integer.valueOf(npage);
}



//3、创建sevice服务操作对象
StudentServiceImpl ssi = new StudentServiceImpl();

int lastpage = ssi.getCountPage();



List<Student> allentities = ssi.getNowPageInfo(nowpage);

stupage.setCountpage(lastpage);
stupage.setNowpage(nowpage); //存入到page中
stupage.setAllentities(allentities);//将结果集放进page中
stupage.index(nowpage);
System.out.println("页码是:"+nowpage+"-start:"+stupage.getStartindex()+" / end:"+stupage.getEndindex());
req.setAttribute("stupage", stupage);


req.getRequestDispatcher("liststs.jsp").forward(req, resp);
}

}

操作类:

Code:
public class StudentDaoImpl implements StudentDao {

// 数据库操作的对象
private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
// 每页显示的记录数
private static final int PAGESIZE = 5;

// 获取总页数
public int getCountPage() {
return getCountRecord() % PAGESIZE == 0 ? getCountRecord() / PAGESIZE
: getCountRecord() / PAGESIZE + 1;
}

// 获取总记录
public int getCountRecord() {
// 1、定义返回结果
int countrecord = 0;
// 2、获取数据库连接对象
conn = DBConn.getConn();
// 3、创建预处理的sql语句
String sql = "select count(*) from student";

try {
// 4、根据预处理的sql语句创建预处理的操作对象
pstmt = conn.prepareStatement(sql);
// 5、查询的时候 直接执行
rs = pstmt.executeQuery();
// 6、判断
if (rs.next()) {
countrecord = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConn.close(rs, pstmt);
}
return countrecord;
}

// 获取当前页信息
public List<Student> getNowPageInfo(int nowpage) {
//1、定义返回结果变量
List<Student> allentities = new ArrayList<Student>();
//2、获取连接对象
conn = DBConn.getConn();
try {
//3、根据预处理的sql语句创建预处理的操作对象
pstmt = conn.prepareStatement("select id,name,age,email from student limit ?,?");
//4、定义下标变量 并赋值
int index = 1;
pstmt.setInt(index++, (nowpage-1)*PAGESIZE);
pstmt.setInt(index++, PAGESIZE);
//5、查询的时候 直接执行
rs = pstmt.executeQuery();
//判断
while(rs.next()){
//创建实体bean对象
Student entity = new Student();
entity.setId(rs.getInt("id"));
entity.setName(rs.getString("name"));
entity.setAge(rs.getInt("age"));
entity.setEmail(rs.getString("email"));
//添加到集合中
allentities.add(entity);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBConn.close(rs, pstmt);
}
return allentities;
}

public static void main(String[] args) {
StudentDaoImpl sdi = new StudentDaoImpl();

/* int countrecord = sdi.getCountRecord();

System.out.println(countrecord);

System.out.println(sdi.getCountPage());*/

List<Student> allentities = sdi.getNowPageInfo(7);

for(Student entity : allentities){
System.out.println(entity.getName());
}


}

}

总结:今天的感觉是:学习很重要,要多动脑,多练习!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值