JSP仿百度分頁,谷歌分頁頁碼處理

像百度一樣的jsp分頁效果,像goolge一樣的分頁效果!
根據設定參數一次取一頁記錄內容顯示

jsp頁面代碼如下:


Java代碼
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="dao.*"%>
<%@ page import="bean.*"%>
<html>
<head>
</head>
<body>
<%
int pagesize = 10;//每頁顯示記錄數
int liststep = 10;//最多顯示分頁頁數
int pages = 1;//默認顯示第一頁
if (request.getParameter("pages") != null) {
pages = Integer.parseInt(request.getParameter("pages"));//分頁頁碼變量
}
int count = PageDao.getmaxpagecount();//假設取出記錄總數
int pagescount = (int) Math.ceil((double) count / pagesize);//求總頁數,ceil(num)取整不小於num
if (pagescount < pages) {
pages = pagescount;//如果分頁變量大總頁數,則將分頁變量設計爲總頁數
}
if (pages < 1) {
pages = 1;//如果分頁變量小於1,則將分頁變量設爲1
}
int listbegin = (pages - (int) Math.ceil((double) liststep / 2));//從第幾頁開始顯示分頁信息
if (listbegin < 1) { //當前頁-(總顯示的頁列表數/2)
listbegin = 1;
}
int listend = pages + liststep / 2;//分頁信息顯示到第幾頁//當前頁+(總顯示的頁列表數/2)
if (listend > pagescount) {
listend = pagescount + 1;
}
%>
<table align="center">
<tr>
<th>
圖書編號
</th>
<th>
圖書名稱
</th>
<th>
出版社
</th>
<th>
作者
</th>
<th>
價格
</th>
</tr>
<%
List<Book> list = PageDao.getAllPageInfo(pages);
Iterator<Book> it = list.iterator();
while (it.hasNext()) {
Book b = it.next();
if (b.getId() % 2 == 0) {
out.println("<tr bgcolor='blue'>");
} else {
out.println("<tr bgcolor='red'>");
}
%>
<td><%=b.getId()%></td>
<td><%=b.getBookname()%></td>
<td><%=b.getBookpublish()%></td>
<td><%=b.getBookauthor()%></td>
<td><%=b.getBookprice()%></td>
<%
out.println("<tr bgcolor='red'>");
}
%>
</table>
<table align="center">
<tr>
<%
//<顯示分頁信息
//<顯示上一頁
if (pages > 1) {
out.println("<td><a href=?pages=" + (pages - 1)
+ ">上一頁</a></td>");
}//>顯示上一頁
//<顯示分頁碼
for (int i = listbegin; i < listend; i++) {
if (i != pages) {//如果i不等於當前頁
out.println("<td><a href=?pages=" + i + ">[" + i
+ "]</a></td>");
} else {
out.println("<td>[" + i + "]</td>");
}
}//顯示分頁碼>
//<顯示下一頁
if (pages != pagescount) {
out.println("<td><a href=?pages=" + (pages + 1)
+ ">下一頁</a></td>");
}//>顯示下一頁
//>顯示分頁信息
%>
</tr>
</table>
</body>
</html>



Dao層代碼:


Java代碼
package dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import bean.Book;

public class PageDao {
public static int getmaxpagecount() {
int num = 0;
Connection conn = null;
Statement stm = null;
ResultSet rs = null;
try {
conn = ConnectionManager.getInstances();
stm = conn.createStatement();
rs = stm.executeQuery("select count(*) from book");
if (rs.next()) {
num = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return num;
}

public static List<Book> getAllPageInfo(int curpage) {
List<Book> list = new ArrayList<Book>();
Connection conn = null;
Statement stm = null;
ResultSet rs = null;

try {
conn = ConnectionManager.getInstances();
conn.setAutoCommit(false);
stm = conn.createStatement();
rs = stm
.executeQuery(("select top 10 * from book where id not in (select top "
+ ((curpage - 1) * 10) + " id from book order by id)order by id"));
while (rs.next()) {
Book b = new Book();
b.setId(rs.getInt("id"));
b.setBookname(rs.getString("bookname"));
b.setBookpublish(rs.getString("bookpublish"));
b.setBookauthor(rs.getString("bookauthor"));
b.setBookprice(rs.getDouble("bookprice"));
list.add(b);
}
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
stm.close();
conn.close();
} catch (SQLException e) {
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}
}
return list;
}
}



連接代碼:




Java代碼
package dao;
import java.sql.*;
public class ConnectionManager {

public static final String DRIVER="com.microsoft.jdbc.sqlserver.SQLServerDriver";
public static final String URL="jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=pagination";
public static final String UID="sa";
public static final String PWD="112233";

public static Connection getInstances(){
Connection conn=null;

try {
Class.forName(DRIVER);
conn=DriverManager.getConnection(URL,UID,PWD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {

e.printStackTrace();
}
return conn;
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章