📄 pagerecord.java
字号:
package pool;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class PageRecord extends HttpServlet
{
private int pagenum=0;// the record numer of each page including
private int currentPage=0;
ConnectionPool connMgr = ConnectionPool.getInstance();
Connection conn = connMgr.getConnection();
public PageRecord(){
//注册驱动程序
}
public void setPagenum(int s)
{
this.pagenum=s;
}
public void setCurrentPage(int s)
{
this.currentPage=s;
}
public int getPagenum()
{
return this.pagenum;
}
public int getCurrentPage()
{
return this.currentPage;
}
public ResultSet getNowRs(String sql)
{
ResultSet trs=null;
try{
Statement st=conn.createStatement();
trs=st.executeQuery(sql);
int count=0;
while (count<(this.getCurrentPage()*this.getPagenum()))
{
//这里把游标向后移动到可以开始直接读取的位置。因为是静态的,只能向前读
//如果读不下去,就抛出异常,返回为null
trs.next();
count++;
}
}catch(SQLException e)
{
trs=null;
}
return trs;
}
//这个方法写出脚注,也就是分页的信息,需要传如url信息,以及调用servlet里的request和response对象
public void setFoot(String url,String sql,HttpServletResponse res)throws ServletException ,java.io.IOException,SQLException
{
PrintWriter out=res.getWriter();
PoolBean use=new PoolBean();
//count the page number
int AllCount=0;
try{
//Statement st=conn.createStatement();
ResultSet rs=use.Query(sql);
while (rs.next())
{
AllCount++;
}
rs.close();
use.freeCon();
}catch(SQLException e)
{
AllCount=0;
use.freeCon();
}
int pagecount=AllCount/pagenum;
pagecount=pagecount+1;
String foot="";
//根据首末状态确定foot的写法
if (this.getCurrentPage()==0)
{
if (pagecount==1)
{
foot="[共 "+pagecount+" 页] [目前 "+(this.getCurrentPage()+1)+" 页] [首页] [上一页] [下一页]</a> [末页]";
}else{
foot="[共 "+pagecount+" 页] [目前 "+(this.getCurrentPage()+1)+" 页] [首页] [上一页] <a href="+url+"?CurrentPage="+(this.getCurrentPage()+1)+">[下一页]</a> <a href="+url+"?CurrentPage="+(pagecount-1)+">[末页]</a>";
}
}else{
if (this.getCurrentPage()==(pagecount-1))
{
foot="[共 "+pagecount+" 页] [目前 "+(this.getCurrentPage()+1)+" 页] <a href="+url+"?CurrentPage=0>[首页]</a> <a href="+url+"?CurrentPage="+(this.getCurrentPage()-1)+">[上一页] </a> [下一页] [末页]</a>";
}else
{
foot="[共 "+pagecount+" 页] [目前 "+(this.getCurrentPage()+1)+" 页] <a href="+url+"?CurrentPage=0>[首页]</a> <a href="+url+"?CurrentPage="+(this.getCurrentPage()-1)+">[上一页] </a> <a href="+url+"?CurrentPage="+(this.getCurrentPage()+1)+">[下一页]</a> <a href="+url+"?CurrentPage="+(pagecount-1)+">[末页]</a>";
}
}
out.println(foot);
}
//这个方法写出脚注,也就是分页的信息,需要传如url信息,以及调用servlet里的request和response对象
//这个是setSql的方式使用该Bean的时候,用来显示Foot的
public void setFootControl(String url,String sql,String[] controlName,String[] controlValue,int conLen,HttpServletResponse res)throws ServletException ,java.io.IOException,SQLException
{
PrintWriter out=res.getWriter();
PoolBean use=new PoolBean();
//count the page number
int AllCount=0;
try{
//Statement st=conn.createStatement();
ResultSet rs=use.Query(sql);
while (rs.next())
{
AllCount++;
}
rs.close();
use.freeCon();
}catch(SQLException e)
{
AllCount=0;
use.freeCon();
}
int pagecount=AllCount/pagenum;
pagecount=pagecount+1;
//以下是确定参数以及其表达式
String control="";
for (int z=0;z<conLen;z++ )
{
control=control+"&"+controlName[z]+"="+controlValue[z];
}
System.out.println("目前的传的值为:"+control);
String foot="";
//根据首末状态确定foot的写法
if (this.getCurrentPage()==0)
{
if (pagecount==1)
{
foot="[共 "+pagecount+" 页] [目前 "+(this.getCurrentPage()+1)+" 页] [首页] [上一页] [下一页]</a> [末页]";
}else{
foot="[共 "+pagecount+" 页] [目前 "+(this.getCurrentPage()+1)+" 页] [首页] [上一页] <a href="+url+"?CurrentPage="+(this.getCurrentPage()+1)+control+">[下一页]</a> <a href="+url+"?CurrentPage="+(pagecount-1)+control+">[末页]</a>";
}
}else{
if (this.getCurrentPage()==(pagecount-1))
{
foot="[共 "+pagecount+" 页] [目前 "+(this.getCurrentPage()+1)+" 页] <a href="+url+"?CurrentPage=0"+control+">[首页]</a> <a href="+url+"?CurrentPage="+(this.getCurrentPage()-1)+control+">[上一页] </a> [下一页] [末页]";
}else
{
foot="[共 "+pagecount+" 页] [目前 "+(this.getCurrentPage()+1)+" 页] <a href="+url+"?CurrentPage=0"+control+">[首页]</a> <a href="+url+"?CurrentPage="+(this.getCurrentPage()-1)+control+">[上一页] </a> <a href="+url+"?CurrentPage="+(this.getCurrentPage()+1)+control+">[下一页]</a> <a href="+url+"?CurrentPage="+(pagecount-1)+control+">[末页]</a>";
}
}
System.out.println("当前有调用setFootSql方法,foot为"+foot);
out.println(foot);
}
public void freeCon()throws java.sql.SQLException
{
connMgr.freeConnection(conn);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -