import java.sql.PreparedStatement
import java.sql.ResultSet
import java.util.Enumeration
import javax.servlet.http.HttpServletRequest
import com.lqh.dao.db.DBCon
public class PageDAO {
public static final String Text = "text"
public static final String Image = "image"
public static final String BbsText = "bbstext"
public static final String BbsImage = "bbsimage"
private HttpServletRequest request
private int currentpage = 1// 当前是第几页
private int pagecount = 0// 一共有多少页
private int rscount = 0// 一共有多少行
private int pagesize = 10// 每页有多少行[默认为20行]
public PageDAO(HttpServletRequest request) {
this.request = request
}
public int getCurrentpage() {
return currentpage
}
public void setCurrentpage(int currentpage) {
this.currentpage = currentpage
}
public int getPagecount() {
return pagecount
}
public void setPagecount(int pagecount) {
this.pagecount = pagecount
}
public int getPagesize() {
return pagesize
}
public void setPagesize(int pagesize) {
this.pagesize = pagesize
}
public int getRscount() {
return rscount
}
public void setRscount(int rscount) {
this.rscount = rscount
}
/**
* 传入SQL语句获取总记录数
*/
public int getRsCountForRs(String sql) {
Connection conn = null
PreparedStatement ps = null
ResultSet rs = null
DBCon dbcon=new DBCon()
try {
conn = dbcon.getConn()
ps = conn.prepareStatement(sql)
rs = ps.executeQuery()
if (rs.next()) {
rs.last()
this.rscount = rs.getRow()
} else {
this.rscount = 0
}
} catch (Exception ex) {
ex.printStackTrace()
this.rscount = 0
} finally {
dbcon.tryClose(rs, ps, conn)
}
return this.rscount
}
public int getRsCountForSQL(String sql) {
Connection conn = null
PreparedStatement ps = null
ResultSet rs = null
DBCon dbcon=new DBCon()
try {
conn = dbcon.getConn()
ps = conn.prepareStatement(sql)
rs = ps.executeQuery()
if (rs.next()) {
this.rscount = rs.getInt("rscount")
} else {
this.rscount = 0
}
} catch (Exception ex) {
ex.printStackTrace()
this.rscount = 0
} finally {
dbcon.tryClose(rs, ps, conn)
}
return this.rscount
}
/**
* 获取总页数
*
* @return int
*/
public int getPageCount() {
try {
this.pagecount = ((this.rscount - 1) / this.pagesize) + 1
} catch (Exception ex) {
this.pagecount = 0
}
return this.pagecount
}
/**
* 获取当前页码的设置
*
* @return int
*/
public int getCurrentPage() {
try {
if (this.request.getParameter("currentpage") != null
&&Integer.parseInt(this.request
.getParameter("currentpage")) >1) {
this.currentpage = Integer.parseInt(this.request
.getParameter("currentpage"))
} else {
this.currentpage = 1
}
} catch (Exception ex) {
this.currentpage = 1
}
return this.currentpage
}
/**
* 分页工具条
*
* @param fileName
*String
* @return String
*/
public String pagetool(String flag) {
StringBuffer str = new StringBuffer()
String url = this.getParamUrl()
int ProPage = this.currentpage - 1
int Nextpage = this.currentpage + 1
// 文字的分页
if (flag.equals(PageDAO.Text)) {
str.append("<form method='post' name='pageform' action=''>")
str
.append("<table style='color: windowframe' width='100%' border='0' cellspacing='0' cellpadding='0'>")
str.append("<tr>")
str.append("<td width='20%'></td>")
str.append("<td height='26'>")
str.append("共有记录" + this.rscount + "条 ")
str.append("共" + this.pagecount + "页 ")
str.append("每页" + this.pagesize + "记录 ")
str.append("现在" + this.currentpage + "/" + this.pagecount + "页")
str.append("</td><td>")
if (this.currentpage >1) {
str.append("<a href='" + url + "&currentpage=1'>首页</a>")
str.append(" ")
str.append("<a href='" + url + "&currentpage=" + ProPage
+ "'>上一页</a>")
str.append(" ")
} else {
str.append("首页")
str.append(" ")
str.append("上一页")
str.append(" ")
}
if (this.currentpage <this.pagecount) {
str.append("<a href='" + url + "&currentpage=" + Nextpage
+ "'>下一页</a>")
str.append(" ")
} else {
str.append("下一页")
str.append(" ")
}
if (this.pagecount >1 &&this.currentpage != this.pagecount) {
str.append("<a href='" + url + "&currentpage=" + pagecount
+ "'>尾页</a>")
str.append(" ")
} else {
str.append("尾页")
str.append(" ")
}
str.append("转到")
str
.append("<select name='currentpage' onchange='javascript:ChangePage(this.value)'>")
for (int j = 1j <= pagecountj++) {
str.append("<option value='" + j + "'")
if (currentpage == j) {
str.append("selected")
}
str.append(">")
str.append("" + j + "")
str.append("</option>")
}
str.append("</select>页")
str.append("</td><td width='3%'> </td></tr></table>")
str.append("<script language='javascript'>")
str.append("function ChangePage(testpage){")
str.append("document.pageform.action='" + url
+ "&currentpage='+testpage+''")
str.append("document.pageform.submit()")
str.append("}")
str.append("</script>")
str.append("</form>")
} else if (flag.equals(PageDAO.Image)) {
/**
* 图片的分页
*/
} else if (flag.equals(PageDAO.BbsText)) {
/**
* 论坛形式的分页[直接以数字方式体现]
*/
str
.append("<table width='100%' border='0' cellspacing='0' cellpadding='0'>")
str.append("<tr>")
str.append("<td width='3%'> </td>")
str.append("<td height='26'>")
str.append("记录" + this.rscount + "条 ")
str.append("共" + this.pagecount + "页 ")
str.append("每页" + this.pagesize + "记录 ")
str.append("现在" + this.currentpage + "/" + this.pagecount + "页")
str.append("</td><td>")
// 设定是否有首页的链接
if (this.currentpage >1) {
str.append("<a href='" + url + "&currentpage=1'>首页</a>")
str.append(" ")
}
// 设定是否有上一页的链接
if (this.currentpage >1) {
str.append("<a href='" + url + "&currentpage=" + ProPage
+ "'>上一页</a>")
str.append(" ")
}
// 如果总页数只有10的话
if (this.pagecount <= 10) {
for (int i = 1i <= this.pagecounti++) {
if (this.currentpage == i) {
str.append("<font color=red>[" + i
+ "]</font> ")
} else {
str.append("<a href='" + url + "&currentpage=" + i
+ "'>" + i + "</a> ")
}
}
} else {
// 说明总数有超过10页
// 制定特环的开始页和结束页
int endPage = this.currentpage + 4
if (endPage >this.pagecount) {
endPage = this.pagecount
}
int startPage = 0
if (this.pagecount >= 8 &&this.currentpage >= 8) {
startPage = this.currentpage - 5
} else {
// 表示从第一页开始算
startPage = 1
}
System.out.println(startPage)
System.out.println(endPage)
for (int i = startPagei <= endPagei++) {
if (this.currentpage == i) {
str.append("<font color=red>[" + i
+ "]</font> ")
} else {
str.append("<a href='" + url + "&currentpage=" + i
+ "'>" + i + "</a> ")
}
}
}
// 设定是否有下一页的链接
if (this.currentpage <this.pagecount) {
str.append("<a href='" + url + "&currentpage=" + Nextpage
+ "'>下一页</a>")
str.append(" ")
}
// 设定是否有尾页的链接
if (this.pagecount >1 &&this.currentpage != this.pagecount) {
str.append("<a href='" + url + "&currentpage=" + pagecount
+ "'>尾页</a>")
str.append(" ")
}
str.append("</td><td width='3%'> </td></tr></table>")
} else if (flag.equals(PageDAO.BbsImage)) {
/**
* 论坛形式的分页[以图片的方式体现]
*/
// 设定分页显示的CSS
str.append("<style>")
str
.append("BODY {FONT-SIZE: 12pxFONT-FAMILY:宋体WIDTH: 60%PADDING-LEFT: 25px}")
str
.append("DIV.meneame {PADDING-RIGHT: 3pxPADDING-LEFT: 3pxFONT-SIZE: 80%PADDING-BOTTOM: 3pxMARGIN: 3pxCOLOR: #ff6500PADDING-TOP: 3pxTEXT-ALIGN: center}")
str
.append("DIV.meneame A {BORDER-RIGHT: #ff9600 1px solidPADDING-RIGHT: 7pxBACKGROUND-POSITION: 50% bottomBORDER-TOP: #ff9600 1px solidPADDING-LEFT: 7pxBACKGROUND-IMAGE: url('"
+ this.request.getContextPath()
+ "/meneame.jpg')PADDING-BOTTOM: 5pxBORDER-LEFT: #ff9600 1px solidCOLOR: #ff6500MARGIN-RIGHT: 3pxPADDING-TOP: 5pxBORDER-BOTTOM: #ff9600 1px solidTEXT-DECORATION: none}")
str
.append("DIV.meneame A:hover {BORDER-RIGHT: #ff9600 1px solidBORDER-TOP: #ff9600 1px solidBACKGROUND-IMAGE: noneBORDER-LEFT: #ff9600 1px solidCOLOR: #ff6500BORDER-BOTTOM: #ff9600 1px solidBACKGROUND-COLOR: #ffc794}")
str
.append("DIV.meneame SPAN.current {BORDER-RIGHT: #ff6500 1px solidPADDING-RIGHT: 7pxBORDER-TOP: #ff6500 1px solidPADDING-LEFT: 7pxFONT-WEIGHT: boldPADDING-BOTTOM: 5pxBORDER-LEFT: #ff6500 1px solidCOLOR: #ff6500MARGIN-RIGHT: 3pxPADDING-TOP: 5pxBORDER-BOTTOM: #ff6500 1px solidBACKGROUND-COLOR: #ffbe94}")
str
.append("DIV.meneame SPAN.disabled {BORDER-RIGHT: #ffe3c6 1px solidPADDING-RIGHT: 7pxBORDER-TOP: #ffe3c6 1px solidPADDING-LEFT: 7pxPADDING-BOTTOM: 5pxBORDER-LEFT: #ffe3c6 1px solidCOLOR: #ffe3c6MARGIN-RIGHT: 3pxPADDING-TOP: 5pxBORDER-BOTTOM: #ffe3c6 1px solid}")
str.append("</style>")
str.append("<div class=\"meneame\">")
// 判定是否有上一页
if (this.currentpage >1) {
str.append("<a href='" + url
+ "&currentpage=1' hidefocus=\"true\">首页</a>")
str.append(" ")
str.append("<a href='" + url + "&currentpage=" + ProPage
+ "' hidefocus=\"true\">上一页</a>")
str.append(" ")
} else {
str.append("<span class=\"disabled\">首页</span>")
str.append(" ")
str.append("<span class=\"disabled\">上一页</span>")
str.append(" ")
}
// 显示中间的图片
if (this.pagecount <= 10) {
for (int i = 1i <= this.pagecounti++) {
if (this.currentpage == i) {
str.append("<span class=\"current\">" + i + "</span>")
} else {
str.append("<a href='" + url + "&currentpage=" + i
+ "' hidefocus=\"true\">" + i
+ "</a> ")
}
}
} else {
// 说明总数有超过10页
// 制定特环的开始页和结束页
int endPage = this.currentpage + 4
if (endPage >this.pagecount) {
endPage = this.pagecount
}
int startPage = 0
if (this.pagecount >= 8 &&this.currentpage >= 8) {
startPage = this.currentpage - 5
} else {
// 表示从第一页开始算
startPage = 1
}
System.out.println(startPage)
System.out.println(endPage)
for (int i = startPagei <= endPagei++) {
if (this.currentpage == i) {
str.append("<span class=\"current\">" + i + "</span>")
} else {
str.append("<a href='" + url + "&currentpage=" + i
+ "' hidefocus=\"true\">" + i
+ "</a> ")
}
}
}
// 判断下一页和尾页
if (this.currentpage <this.pagecount) {
if (this.currentpage <this.pagecount - 10) {
str.append("...")
str.append("<a href='" + url + "&currentpage="
+ (this.pagecount - 1) + "' hidefocus=\"true\">"
+ (this.pagecount - 1) + "</a> ")
str.append("<a href='" + url + "&currentpage="
+ this.pagecount + "' hidefocus=\"true\">"
+ this.pagecount + "</a> ")
}
str.append("<a href='" + url + "&currentpage=" + Nextpage
+ "' hidefocus=\"true\">下一页</a>")
str.append(" ")
} else {
str.append("<span class=\"disabled\">下一页</span>")
str.append(" ")
}
if (this.pagecount >1 &&this.currentpage != this.pagecount) {
str.append("<a href='" + url + "&currentpage=" + pagecount
+ "' hidefocus=\"true\">尾页</a>")
str.append(" ")
} else {
str.append("<span class=\"disabled\">尾页</span>")
str.append(" ")
}
str.append("</div>")
}
return str.toString()
}
public String getParamUrl() {
String url = ""
url = this.request.getRequestURI().toString()
if (url.indexOf("?") == -1) {
url = url + "?"
}
String totalParams = ""
Enumeration params = this.request.getParameterNames()// 得到所有参数名
while (params.hasMoreElements()) {
String tempName = params.nextElement().toString()
String tempValue = this.request.getParameter(tempName)
if (tempValue != null &&!tempValue.equals("")
&&!tempName.equals("currentpage")) {
if (totalParams.equals("")) {
totalParams = totalParams + tempName + "=" + tempValue
} else {
totalParams = totalParams + "&" + tempName + "="
+ tempValue
}
}
}
String totalUrl = url + totalParams
return totalUrl
}
}
intPageCount = (intRowCount+intPageSize-1) / intPageSize等价于intPageCount=(intRowCount-1/intPageSiez)+1
如果introwCount(总记录)=8,intPageSize(每页记录)=8则共为1页,如果rowCount=14,inPageSize=8则为2页,就是说总页数至少是要比8的倍数多1才会多翻一页.你想想如果一页显示八个记录,而总数刚好最后页有8个记录,你会翻页吗,不会的吧!!就这个道理
其实可以用为intPageCount=intRowCount%intPageSize==0?(intRowCount)/intPageSize:(intRowCount/intPageSize+1)
假设rowCount(总记录数)=8 intPageSize(每页记录数)=8,那么总页数就是因为8%8==0则8/8=1
就是一页
假设rowCount=14 intPageSize=8则总页数就是因为14%8!=0则14/8+1=2
就是两页这样分析楼主不知道能否明白