1、創(chuàng)建一個 Java Web 項目砂代,添加 mysql 連接數(shù)據(jù)庫的 jar 包蹋订,連接數(shù)據(jù)庫,查詢數(shù)據(jù)刻伊。
2露戒、創(chuàng)建一個 Dao 類,用戶操作數(shù)據(jù)庫數(shù)據(jù)捶箱。實現(xiàn)分頁的 sql 語句為:
select * from user order by id desc limit (curPage-1)*pageSize智什,pageSize
pageSize:每頁顯示的數(shù)據(jù)條數(shù)
curPage:當前顯示的頁數(shù)
limit 后面跟隨兩個參數(shù),中間用逗號隔開丁屎,前一個參數(shù)是偏移量荠锭,即每頁第一條記錄數(shù),第二個參數(shù)是每頁顯示的記錄條數(shù)晨川。
Dao 類內(nèi)容如下:
public class UserDao {
private Connection conn = null;
private PreparedStatement ps = null;
private Statement stmt = null;
private ResultSet rs = null;
public List getUsers(int pageSize,int curPage) {
List user=new ArrayList();
String sql = "select * from user order by id desc limit ?,?";
try {
ps = new GetConn().getConn().prepareStatement(sql);
ps.setInt(1, (curPage-1)*pageSize);
ps.setInt(2, pageSize);
rs = ps.executeQuery();
while(rs.next()){
//需要在 User 類中证九,設置一個帶有參數(shù)的構造函數(shù)
user.add(new User(rs.getInt(1),rs.getString(2),rs.getInt(3)));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
System.out.println(sql);
}
return user;
}
//查詢表中總的記錄條數(shù)
public int count(){
String sql = "select count(*) from user where 1=1";
try {
//獲取數(shù)據(jù)庫連接删豺,執(zhí)行查詢語句
ps = new GetConn().getConn().prepareStatement(sql);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
int num = 0;
try {
rs = ps.executeQuery();
if(rs.next()){
num = rs.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return num;
}
}
3、創(chuàng)建一個 servlet 愧怜,用于處理分頁顯示:
public showUser() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int pageSize = 3; ? ? ? ? ? //每頁顯示記錄條數(shù)
String pageIndex = request.getParameter("curPage"); ? ? ? ? ? ? //獲取當前頁數(shù)
int curPage = pageIndex==null?1:Integer.valueOf(pageIndex); ??
//如果當前頁數(shù)為空呀页,則賦值為1,不為空叫搁,則轉化為整數(shù)
int totalUser = userDao.count(); ? ? ? ? ?//總的記錄條數(shù)
System.out.println(totalUser);
int totalPage = totalUser%pageSize==0?totalUser/pageSize:totalUser/pageSize+1;
//計算總的頁數(shù)
List user = userDao.getUsers(pageSize,curPage);
//進行分頁查詢
request.setAttribute("curPage", curPage);
request.setAttribute("totalPage", totalPage);
request.setAttribute("user", user);
request.getRequestDispatcher("MyJsp.jsp").forward(request, response);
}
private UserDao userDao = null;
public void init() throws ServletException {
// Put your code here
userDao = new UserDao();
}
4赔桌、在 index.jsp 頁面上設置一個連接,指向 servlet 渴逻。由 servlet 處理后疾党,轉向 MyJsp.jsp 頁面,進行分頁顯示惨奕。
5雪位、在 MyJsp.jsp 頁面的頭元素中插入 Java 代碼,獲取 servlet 傳入的參數(shù)梨撞,當前頁數(shù)和總的頁數(shù)以及所有的記錄信息雹洗。
<%
int curPage = 0;
int totalPage = 0;
List users = (List)request.getAttribute("user");
if(request.getAttribute("curPage") != null){
curPage = (Integer)request.getAttribute("curPage");
totalPage = (Integer)request.getAttribute("totalPage");
}
%>
然后在頁面的 body 元素中分頁顯示:
設置表頭信息,同時對集合元素進行遍歷卧波,獲得對應數(shù)據(jù)时肿。
設置頁碼信息,跳轉到相應界面港粱。
6螃成、運行程序,查看效果查坪,如圖: