一、分页接口:
/*
* Page.java
* 创建日期 2005-4-13
* @ Author wuxp
*/
package org.page.pageutil;
/**
* @ Author wuxp
*/
public interface Page {
public void setCurrentPage(String c);
public int getCurrentPage();
public String getToolBar(String path);
public int count(String sql);
public int getMaxPage();
}
二、查询类
/*
* Query.java
* 创建日期 2005-4-13
* @ Author wuxp
*/
package org.page.pageutil;
/**
* @ Author wuxp
*/
public class Query {
String sql="";
int max=10;
public Query(String sql1)
{
sql="select * from (select table1.*,rownum as row1 from ("+sql1+") table1) ";
}
public String getQuerySql(){
return sql;
}
public void setFirstPage(int first)
{
sql+="where row1>"+(first*max);
if (first==0)
sql+=" and row1<="+max;
else
sql+=" and row1<="+(first*max+max);
//System.out.println(sql);
}
public void setMaxResults(int max)
{
this.max=max;
}
}
三、分页接口实现:
/*
* SimplePage.java
* 创建日期 2005-4-13
* @ Author wuxp
*/
package org.genius.util.page.pageutil;
import java.sql.ResultSet;
import org.genius.systemmanager.database.JndiConn;
/**
* @ Author wuxp
*/
public class SimplePage implements Page {
public int currentPage=0;
private int all;
private int maxPage;
private int maxResults;
public void setCurrentPage(String c)
{
if (c!=null)
currentPage=Integer.parseInt(c);
}
public void setMaxPage(int maxPage) {
this.maxPage = maxPage;
}
public int getMaxPage() {
return maxPage;
}
public int getCurrentPage()
{
return currentPage;
}
public String getToolBar(String path)
{
String toolbar="<table border=0 width=95% height=25><form action='"+path+"' method='post' onsubmit='checkJump(this)'><tr><td align=right>";
toolbar+="共<font color='#21A2E7'>"+maxResults+"</font>条 ";
toolbar+="<a href='"+path+"¤tPage=0'>首页</a> | ";
if (currentPage==0)
toolbar+="上页 | ";
else
toolbar+="<a href='"+path+"¤tPage="+(currentPage-1)+"'>上页</a> | ";
if (currentPage==all)
toolbar+="下页 | ";
else
toolbar+="<a href='"+path+"¤tPage="+(currentPage+1)+"'>下页</a> | ";
toolbar+="<a href='"+path+"¤tPage="+all+"'>尾页</a> ";
toolbar+=" 页次:<font color='#21A2E7'>"+(currentPage+1)+"/"+(all+1)+"</font>页 ";
toolbar+="<font color='#21A2E7'>"+maxPage+"</font>条/页 ";
toolbar+=" 转到第<input type=\"text\" name=\"currentPage\" style=\"width:15;height:18\">页 ";
toolbar+="<input type='submit' value='GO' class='toolbar_go'>";
toolbar+="<script>function checkJump(form1){" +
"if (form1.currentPage.value==\"\"||isNaN(form1.currentPage.value)||form1.currentPage.value>"+(all+1)+"||form1.currentPage.value<0){" +
"form1.currentPage.value='0';}" +
"else{" +
"form1.currentPage.value=parseInt(parseInt(form1.currentPage.value)-1);" +
"}";
toolbar+="}</script>";
toolbar+="</td></form></tr></table>";
return toolbar;
}
public int count(String sql){
//此类是自己封装的连结池,可改变为自己的数据库连接
JndiConn conn=new JndiConn();
String sql_count="select count(*) as count from ("+sql+")";
//System.out.println(sql);
try{
ResultSet rs=conn.executeQuery(sql_count);
while (rs.next())
all=rs.getInt("count");
rs.close();
}
catch(Exception e){
e.printStackTrace();
}finally{
conn.close();
}
maxResults=all;
if (all%maxPage==0){
if (all<=maxPage)
all=0;
else
all=all/maxPage-1;
}
else
all=all/maxPage;
//all=all/maxPage;
return all;
}
}