?? 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 + -