ajax分页的java类

/**
* @author zhangshuling E-mail:[email protected]
* @version createtime:Apr 2, 2009 9:20:07 AM 此类用于整合 ajax 进行分页 返回一个
*          java.util.Hashtable 里面分别有两个对象 一个为分页的字符串 ( < << 1 | 2 | 3 | 4 |5 |
*          ..... >> >) 一个为分页的数据(java.util.List) 暂时没做Hibernate的相关的查询,有需要就自己添吧
*          ^_^
*/
public class AjaxPage {

private int pageSize = 10; // 分页刻度 (多少条记录为一页)

private int pageTotal = 0; // 分页总数 (一共有多少页)

// 分页所需图片
private String firstPageImage = "http://www.kaogoo.com/english/images/back2.gif";

private String previousPageImage = "http://www.kaogoo.com/english/images/backing.gif";

private String nextPageImage = "http://www.kaogoo.com/english/images/next.gif";

private String lastPageImage = "http://www.kaogoo.com/english/images/next2.gif";

public static final String KEY_PAGELIST = "PAGELIST";

public static final String KEY_PAGELINK = "PAGELINK";

public AjaxPage() {

}

public AjaxPage(int pageSize, int pageTotal, String firstPageImage,
String previousPageImage, String nextPageImage, String lastPageImage) {
setPageSize(pageSize);
setPageTotal(pageTotal);
setFirstPageImage(firstPageImage);
setPreviousPageImage(previousPageImage);
setNextPageImage(nextPageImage);
setLastPageImage(lastPageImage);
}

public String getFirstPageImage() {
return firstPageImage;
}

public void setFirstPageImage(String firstPageImage) {
this.firstPageImage = firstPageImage;
}

public String getLastPageImage() {
return lastPageImage;
}

public void setLastPageImage(String lastPageImage) {
this.lastPageImage = lastPageImage;
}

public String getNextPageImage() {
return nextPageImage;
}

public void setNextPageImage(String nextPageImage) {
this.nextPageImage = nextPageImage;
}

public int getPageSize() {
return pageSize;
}

public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}

public int getPageTotal() {
return pageTotal;
}

public void setPageTotal(int pageTotal) {
this.pageTotal = pageTotal;
}

public String getPreviousPageImage() {
return previousPageImage;
}

public void setPreviousPageImage(String previousPageImage) {
this.previousPageImage = previousPageImage;
}

/**
*
* @param sqlTotal
*            //查询所有记录条数
* @param sql
*            //查询所需记录
* @param currentPage
*            //当前页码
* @param parameter
*            //参数() parameter所带的& 必须用&amp;转义 不然在会在传递的时候出错 &parameter=1&...
*            应该写成 &amp;parameter=1&amp;...
* @param ajaxMethod
*            //ajaxMethod 指的是ajax调用的javascript方法 格式为
*            ajaxMethod(parameter,page);
* @param connection
* @param preparedStatement
* @param resultSet
* @return
*/
public java.util.Hashtable ajaxPage(String sqlTotal, String sql,
int currentPage, String parameter, String ajaxMethod,
java.sql.Connection connection,
java.sql.PreparedStatement preparedStatement,
java.sql.ResultSet resultSet) {
java.util.Hashtable pageContent = new java.util.Hashtable();
java.util.List pageList = new java.util.ArrayList();
StringBuffer pageLink = new StringBuffer(
"&lt;table width=&quot;100%&quot; align=&quot;center&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot;&gt;");
try {
preparedStatement = connection.prepareStatement(sqlTotal);
resultSet = preparedStatement.executeQuery();
resultSet.next();
setPageTotal(resultSet.getInt(1));
} catch (java.sql.SQLException e) {
setPageTotal(0);
} catch (Exception e) {
setPageTotal(0);
}
if (getPageTotal() > 0) {
int pageCount = (int) Math.ceil((double) getPageTotal()
/ getPageSize()); // 总共该分多少页
int start = currentPage - 4;
int end = currentPage + 4;
if (start <= 1) {
start = 1;
end = start + 9;
}
if (end >= pageCount) {
end = pageCount;
start = end - 9;
if (start <= 0) {
start = 1;
}
}
int previous = (currentPage - 1 > 0 ? (currentPage - 1) : 1);
int next = ((currentPage + 1) < pageCount ? (currentPage + 1)
: pageCount);
pageLink
.append("&lt;tr&gt;&lt;td align=&quot;center&quot; height=&quot;30&quot; width=&quot;100%&quot;&gt;");
pageLink.append("&lt;a href=&quot;javascript:").append(ajaxMethod)
.append("('").append(parameter).append("',1)").append(
"&quot; title=&quot;转到第1页&quot; &gt;");
pageLink.append("&lt;img boder=&quot;0&quot; src=&quot;");
pageLink.append(getFirstPageImage())
.append("&quot;/&gt;&lt;/a&gt;");
pageLink.append("&amp;nbsp;&amp;nbsp;");

pageLink.append("&lt;a href=&quot;javascript:").append(ajaxMethod)
.append("('").append(parameter).append("',").append(
previous).append(")&quot; title=&quot;转到第").append(
previous).append("页&quot; &gt;");
pageLink.append("&lt;img boder=&quot;0&quot; src=&quot;");
pageLink.append(getPreviousPageImage()).append(
"&quot;/&gt;&lt;/a&gt;");
pageLink.append("&amp;nbsp;&amp;nbsp;");
for (int i = start; i <= end; i++) {
if (i != currentPage) {
pageLink.append("&lt;a href=&quot;javascript:").append(
ajaxMethod).append("('").append(parameter).append(
"',").append(i).append(")&quot; title=&quot;转到第")
.append(i).append("页&quot;&gt;").append(i).append(
"&lt;/a&gt;");
} else {
pageLink
.append("&lt;span title=&quot;当前第")
.append(i)
.append(
"页&quot;&gt;&lt;font color=&quot;red&quot;&gt;")
.append(i).append("&lt;/font&gt;&lt;/span&gt;");
}
if (i < end) {
pageLink.append("&amp;nbsp;|&amp;nbsp;");
}
}
pageLink.append("&amp;nbsp;&amp;nbsp;");
pageLink.append("&lt;a href=&quot;javascript:").append(ajaxMethod)
.append("('").append(parameter).append("',").append(next)
.append(")&quot; title=&quot;转到第").append(next).append(
"页&quot; &gt;");
pageLink.append("&lt;img boder=&quot;0&quot; src=&quot;");
pageLink.append(getNextPageImage()).append("&quot;/&gt;&lt;/a&gt;");
pageLink.append("&amp;nbsp;&amp;nbsp;");

pageLink.append("&lt;a href=&quot;javascript:").append(ajaxMethod)
.append("('").append(parameter).append("',").append(
pageCount).append(")&quot; title=&quot;转到第")
.append(pageCount).append("页&quot; &gt;");
pageLink.append("&lt;img boder=&quot;0&quot; src=&quot;");
pageLink.append(getLastPageImage()).append("&quot;/&gt;&lt;/a&gt;");
pageLink.append("&amp;nbsp;&amp;nbsp;");

pageLink.append("&lt;/td&gt;&lt;/tr&gt;");

if (currentPage - 1 < previous) {
currentPage = previous;
}
if (currentPage + 1 > next) {
currentPage = next;
}
int focus = ((currentPage - 1) * getPageSize()) + 1;
try {
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
pageList = parseResultSet(pageList, resultSet, this
.getPageSize(), focus);
} catch (java.sql.SQLException e) {
setPageTotal(0);
} catch (Exception e) {
setPageTotal(0);
}
} else { // 总记录为0 无记录
pageLink
.append("&lt;tr&gt;&lt;td align=&quot;center&quot; width=&quot;100%&quot;&gt;");
pageLink.append("没有符合条件的数据!");
pageLink.append("&lt;/td&gt;&lt;/tr&gt;");
}
pageLink.append("&lt;/table&gt;");
pageContent.put(AjaxPage.KEY_PAGELIST, pageList);
pageContent.put(AjaxPage.KEY_PAGELINK, pageLink.toString());
return pageContent;
}

/**
* 将一个记录集ResultSet转化成一个存放Object[]的List
*
* @param pageList
* @param resultSet
* @param size
*            //取多少条记录
* @param focus
*            //从第几条开始去
* @return
*/
public java.util.List parseResultSet(java.util.List pageList,
java.sql.ResultSet resultSet, int size, int focus) {

try {
resultSet.absolute(focus); // 定位到第多少格
resultSet.previous(); // 前移一格
int col = resultSet.getMetaData().getColumnCount();
Object[] obj;
int index = 1;
while (resultSet.next()) {
if (index > size)
break;
obj = new Object[col];
for (int i = 1; i <= col; i++) {
obj[(i - 1)] = resultSet.getString(i);
}
pageList.add(obj);
index++;
}

} catch (java.sql.SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return pageList;
}
}

你可能感兴趣的:(JavaScript,java,sql,Ajax,Hibernate)