📄 fenye.java
字号:
/*
* Pagination.java
*
* Created on 2007年11月1日, 下午1:13
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package dianping;
import java.sql.SQLException;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.ResultSet;
import dianping.DB;
/**
*冷漠大神 qq 361619004
* @author Angel
*/
public class fenye extends HttpServlet{
/** Creates a new instance of Pagination */
public fenye() {
}
private int pageSize=0;
private String table="";
private void setTable(String s)
{
this.table=s;
}
private String getTable()
{
return table;
}
/**
*初始化表名与每页显示的条数 扩展用
*/
public void info(String tableName,int pageSize)
{
this.setTable(tableName);
this.setPageSize(pageSize);
}
/**
*首页
*参数说明 request 传入jsp内置的request对象 current 当前页 例:比如你现在在index.jsp页 那你就 XX.top(request,"index.jsp"),当然你也可以懒的写法XX.top(request,"")这么写也可以实现相同的功能
*/
public String top(HttpServletRequest request,String current)
{
request.setAttribute("info","0");
return "<a href="+current+"?page="+(String)request.getAttribute("info")+">首页</a> ";
}
/**
*上一页
* 参数说明 request 传入jsp内置的request对象 current 当前页 例:比如你现在在index.jsp页 那你就 XX.shang(request,"index.jsp"),当然你也可以懒的写法XX.shang(request,"")这么写也可以实现相同的功能
*yema 这个参数就是你当前的页码 你可以在你的my.jsp 这么调用 String pages=request.getParameter("page") 再把pages传到yema 里就一切 OK 了
*/
public String shang(HttpServletRequest request,String current,String yema)
{
int i=Integer.parseInt(yema);
if(i>0)
i--;
String s=""+i;
request.setAttribute("info",s);
return "<a href="+current+"?page="+(String)request.getAttribute("info")+">上一页</a> ";
}
/**
*下一页
* 参数说明 request 传入jsp内置的request对象 current 当前页 例:比如你现在在index.jsp页 那你就 XX.xia(request,"index.jsp"),当然你也可以懒的写法XX.xia(request,"")这么写也可以实现相同的功能
*yema 这个参数就是你当前的页码 你可以在你的my.jsp 这么调用 String pages=request.getParameter("page") 再把pages传到yema 里就一切 OK 了
*/
public String xia(HttpServletRequest request,String current,String yema )
{
String wei=null;
DB ms=new DB();
ResultSet rs=ms.MSquery("select count(*) as a from "+this.getTable());
try {
if(rs.next())
{
wei=rs.getString("a");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
int ii=0;
if(wei!=null)
{
ii=Integer.parseInt(wei);
ii=ii/this.getPageSize();
}
int i=Integer.parseInt(yema);
if(i<ii)
i++;
String s=""+i;
request.setAttribute("info",s);
ms.close();
return "<a href="+current+"?page="+(String)request.getAttribute("info")+">下一页</a> ";
}
/**
*尾页
*参数说明 request 传入jsp内置的request对象 current 当前页 例:比如你现在在index.jsp页 那你就 XX.bottom(request,"index.jsp"),当然你也可以懒的写法XX.bottom(request,"")这么写也可以实现相同的功能
*/
public String bottom(HttpServletRequest request,String current)
{
String wei=null;
DB ms=new DB();
ResultSet rs=ms.MSquery("select count(*) as a from "+this.getTable());
try {
if(rs.next())
{
wei=rs.getString("a");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
int ii=0;
if(wei!=null)
{
ii=Integer.parseInt(wei);
ii=ii/this.getPageSize();
}
wei=""+ii;
request.setAttribute("info",wei);
ms.close();
return "<a href="+current+"?page="+(String)request.getAttribute("info")+">尾页</a> ";
}
/**
*这是一个实现了统计和跳转的功能
*传进去当前页字符串的信息会返回
*<script>function list(){var p = page.value;window.location.href='index.jsp?page='+p;}</script>
* <form action="index.jsp" onsubmit=return list() >
* <input type=text name=page size=3 />
* <input type=submit value=跳转>
* </form>
*共有XX条数据,分XX页显示,当前第XX页!
*的字符串
*/
private String DBcount="";
private String dangqian=null;
public String tiaozhuan(String current,String yema2)
{
String wei=null;
DB ms=new DB();
ResultSet rs=ms.MSquery("select count(*) as a from "+this.getTable());
try {
if(rs.next())
{
wei=rs.getString("a");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
DBcount=wei.toString();
int ii=0;
if(wei!=null)
{
ii=Integer.parseInt(wei);
ii=ii/pageSize;
}
ii++;
wei=""+ii;
ms.close();
int ye=Integer.parseInt(yema2);
ye++;
return "<script>function list(){var p = page.value;window.location.href='"+current+"?page='+p;}</script><form action='"+current+"' onsubmit=return list() ><input type=text name=page size=5 /><input type=submit value=跳转 > 共有:<font color=red >"+DBcount+"</font> 条数据,分<font color=red >"+ii+"</font>页显示,当前第:<font color=red >"+ye+"</font>页!</form>(索引从0开始)";
}
private void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
private int getPageSize()
{
return this.pageSize;
}
/**
*返回sql语句
*参数说明 pageSize 这个参数是指你每页想显示几条数据 比如说 把10 传进去就会返回10条记录的结果集 tableName 这个没有说明好说的啦 就是把你想要操作的表 的名字传进来就行了
*yema 这个参数就是你当前的页码 你可以在你的my.jsp 这么调用 String pages=request.getParameter("page") 再把pages传到yema 里就一切 OK 了
*/
public String getSqlASC(int pageSize,String tableName,String yema) {
this.setTable(tableName);
this.setPageSize(pageSize);
int i=0;
int temp=0;
if(yema!=null&&!yema.equals(""))
{
i=Integer.parseInt(yema);
temp=pageSize*i;
}
return "select top "+pageSize+" * from "+tableName+" where (id not in (select top "+temp+" id from "+tableName+" order by id)) order by id ";
}
/**
*返回sql语句 按照你的主键id值倒序 输出
*参数说明 pageSize 这个参数是指你每页想显示几条数据 比如说 把10 传进去就会返回10条记录的结果集 tableName 这个没有说明好说的啦 就是把你想要操作的表 的名字传进来就行了
*yema 这个参数就是你当前的页码 你可以在你的my.jsp 这么调用 String pages=request.getParameter("page") 再把pages传到yema 里就一切 OK 了
*/
public String getSqlDESC(int pageSize,String tableName,String yema) {
this.setPageSize(pageSize);
this.setTable(tableName);
int i=0;
int temp=0;
if(yema!=null&&!yema.equals(""))
{
i=Integer.parseInt(yema);
temp=pageSize*i;
}
return "select top "+pageSize+" * from "+tableName+" where (id not in (select top "+temp+" id from "+tableName+" order by id desc)) order by id desc";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -