分页怎么实现:
看到效果
1 当前页-1 当前页+1 最后一页 总页数
首页 上一页 下一页 末页
算法分析:------------------------------------------------
当前页:
int nowpage;
首页:
nowpage=1;
末页:
int countpage; 总页
nowpage=countpage;
上一页:
nowpage = nowpage-1;
if(nowpage<1){
nowpage=1;
}
下一页:
nowpage = nowpage+1;
if(nowpage>countpage){
nowpage=countpage;
}
-----------------------------------------------------
理解概念:
当前页 nowpage
总页数 countpage =======7页
每页显示的记录数 10条
当前页开始的记录数:(nowpage-1)*10+1;
1 1-10
2 11-20
3 21 30
4 31 40
5 41 50
总记录数:
countrecord =64记录
总页数==总记录数%每页显示的记录数==0?总记录数/每页显示的记录数:总记录数/每页显示的记录数+1;
------------------------------------------
表的操作:
总记录数:select count(*) from 表名;
每页显示3条记录: 声明 int pagesize=3;
总页数:总页数==总记录数%每页显示的记录数==0?总记录数/每页显示的记录数:总记录数/每页显示的记录数+1;
当前页的记录信息:
select * from 表名 limit (nowpage-1)*pagesize,pagesize;
package qi.dao;
import java.util.List;
import qi.domain.Admin;
public interface AdminDao {
List<Admin>findNowPageInfo(Integer nowpage);
Integer findCountRecord();
Integer findCountPage();
}
package qi.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import qi.domain.Admin;
public class AdminDaoImpl implements AdminDao {
private static Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
private static final String URL = "jdbc:mysql://localhost:3306/3g?user=root&password=bwosi1nea&useUnicode=true&characterEncoding=UTF8";
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(URL);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static final Integer PAGESIZE=3;
private Integer countRecord;
private Integer countPage;
public List<Admin> findNowPageInfo(Integer nowpage) {
List<Admin> allentites = new ArrayList<Admin>();
String sql = "select id,name,pass,sex,age from admin limit ?,?";
try {
pstmt = conn.prepareStatement(sql);
int index=1;
pstmt.setInt(index++,(nowpage-1)*PAGESIZE);
pstmt.setInt(index++,PAGESIZE);
rs = pstmt.executeQuery();
while (rs.next()) {
Admin entity = new Admin();
entity.setId(rs.getInt("id"));
entity.setName(rs.getString("name"));
entity.setPass(rs.getString("pass"));
entity.setSex(rs.getString("sex"));
entity.setAge(rs.getInt("age"));
allentites.add(entity);
}
release(rs, pstmt);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return allentites;
}
public Integer findCountRecord() {
String sql="select count(*) from admin";
try {
pstmt = conn.prepareStatement(sql);
rs=pstmt.executeQuery();
if(rs.next()){
this.countRecord = rs.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return this.countRecord;
}
private void release(ResultSet rs, PreparedStatement pstmt) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public Integer findCountPage() {
findCountRecord();
this.countPage=this.countRecord%this.PAGESIZE==0?this.countRecord/this.PAGESIZE:this.countRecord/this.PAGESIZE+1;
return this.countPage;
}
}
package qi.domain;
public class Admin {
private Integer id;
private String name;
private String pass;
private String sex;
private Integer age;
public Admin() {
super();
// TODO Auto-generated constructor stub
}
public Admin(Integer id, String name, String pass, String sex, Integer age) {
super();
this.id = id;
this.name = name;
this.pass = pass;
this.sex = sex;
this.age = age;
}
@Override
public String toString() {
return "Admin [id=" + id + ", name=" + name + ", pass=" + pass
+ ", sex=" + sex + ", age=" + age + "]";
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
package qi.junit;
import java.util.List;
import org.junit.Test;
import qi.domain.Admin;
import qi.service.AdminServiceImpl;
public class AdminTest {
private AdminServiceImpl aService = new AdminServiceImpl();
@Test
public void getCountRecrd(){
System.out.println(aService.findCountRecord());
}
@Test
public void getCountPage(){
System.out.println(aService.findCountPage());
}
@Test
public void NowPageInfo(){
List<Admin> admins = aService.findNowPageInfo(4);
for(Admin admin:admins){
System.out.println(admin.toString());
}
}
}
package qi.service;
import qi.dao.AdminDao;
public interface AdminService extends AdminDao{
}
package qi.service;
import java.util.List;
import qi.dao.AdminDao;
import qi.dao.AdminDaoImpl;
import qi.domain.Admin;
public class AdminServiceImpl implements AdminService{
private AdminDao aDao = new AdminDaoImpl();
public List<Admin> findNowPageInfo(Integer nowpage) {
return aDao.findNowPageInfo(nowpage);
}
public Integer findCountRecord() {
return aDao.findCountRecord();
}
public Integer findCountPage() {
return aDao.findCountPage();
}
}