public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletExcetption,IOException{ doGet(request,response); } }package com.page; import java.util.Vector; public class PageBean{ public int curPage; public int maxPage; public int maxRowCount; public int rowsPerPage=5; public Vector data; public PageBean(){
} public void countMaxPage(){ if(this.maxRowCount%this.rowsPerPage==0){ this.maxPage=this.maxRowCount/this.rowsPerPage; }else{ this.maxPage=this.maxRowCount/this.rowsPerPage+1; } } public Vector getResult(){ return this.data; } public PageBean(ContactBean contact)throws Exception{ this.maxRowCount=contact.getAvailableCount();//总行数 this.data=contact.getResult(); //显示本页的内容 this.countMaxPage(); } }
package com.page;
import java.util.*;
import com.page;
import java.sql.*;
public class ContactBean{
private Connection con;
Vector v;
public ContactBean()throws Exception{
conn=DatabaseConn.getConnection();
v=new Vector();
}
public int getAvailableCount()throws Exception{
int ret=0;
Statement stmt = con.createStatement();
String strSql = "select count(*) from contact";
ResultSet rs = stmt.execute(strSql);
while(rs.next()){
ret=rs.getInt(1);
}
return ret;
}
public PageBean listData(String page)throws Exception{
try{
PageBean pageBean = new PageBean(this);
int pageNum = Integer.parseInt(page);
Statement stmt = con.createStatement();
String strSql = "select top"+pageNum*pageBean.rowsPerPage+" * from contact order by userName";
ResultSet rs=stmt.executeQuery(strSql);
int i=0;
while(rs.next()){
if(i>(pageNum-1)*pageBean.rowsPerPage-1){
Object[] obj = new Object[6];
obj[0] = rs.getString("userName");
obj[1] = new Integer(rs.getInt("mobile"));
obj[2] = rs.getString("phone");
obj[3] = rs.getString("mail");
obj[4] = rs.getDate("lastcontact");
obj[5] = rs.getString("mem");
v.add(obj);
}
i++;
}
rs.close();
stmt.close();
pageBean.curPage=pageNum;
pageBean.data=v;
return pageBean;
}catch(Exception e){
e.printStackTrace();
}
}
public Vector getResult()throws Exception{
return v;
}
}
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class ContactServlet extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletExcetption,IOException{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
try{
ContactBean contact = new ContactBean();
PageBean pageCtl = new contact.listData((String)request.getParameter("jumpPage"));
request.setAttribute("pageCtl",pageCtl);
}catch(Exception se){
se.printStackTrace();
}
RequestDispatcher dis = request.getRequestDispatcher("/viewcontact");
dis.forward(request,response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletExcetption,IOException{
doGet(request,response);
}
}package com.page;
import java.util.Vector;
public class PageBean{
public int curPage;
public int maxPage;
public int maxRowCount;
public int rowsPerPage=5;
public Vector data;
public PageBean(){
}
public void countMaxPage(){
if(this.maxRowCount%this.rowsPerPage==0){
this.maxPage=this.maxRowCount/this.rowsPerPage;
}else{
this.maxPage=this.maxRowCount/this.rowsPerPage+1;
}
}
public Vector getResult(){
return this.data;
}
public PageBean(ContactBean contact)throws Exception{
this.maxRowCount=contact.getAvailableCount();//总行数
this.data=contact.getResult(); //显示本页的内容
this.countMaxPage();
}
}