Pagecontrol.java~3~
资源名称:bangong.rar [点击查看]
上传用户:dlqqsh
上传日期:2021-11-13
资源大小:7840k
文件大小:7k
源码类别:
OA系统
开发平台:
Java
- package control.Textcontrol;
- import java.io.*;
- import java.lang.*;
- import java.sql.*;
- import java.util.*;
- import control.*;
- public class Pagecontrol {
- private int ipage;//分頁單位
- private int allpage;//總頁數
- private int pages=1;//接受的頁碼變數
- private int cpage=1;//當前頁
- private int allcol=0;
- private String[] context;
- private String pagecontrol;
- private String select;
- public Pagecontrol() {
- }
- public void setpages(String i){
- int change = java.lang.Integer.valueOf(i).intValue();
- this.pages=change;
- }
- //参数一中存一个已经完成连接的Connection对象,参数二中存每页显示的行数,第三个参数存要操作的表名,第四个参数存这个表的主键的键名,第五个参数存排序方式 当为""时为正序,当为"desc"时为逆序,后一个参数存要查找的字段,最后一个参数为查询条件
- public String[] getPageinf(int page,String text,String key,String order,String showcolm[],String forbiden) throws Exception{
- sqlcontrol consql = new sqlcontrol();
- consql.getMysqlconn("newsinfo");
- this.ipage = page;
- String pagesql="";
- if(forbiden.equals("")){
- pagesql = "select count(*) from " + text;
- }else{
- pagesql = "select count(*) from " + text + " where "+forbiden;
- }
- ResultSet pagers = consql.getStatement(pagesql);//取總文章數
- while(pagers.next()){
- this.allcol = pagers.getInt(1);
- }
- pagers.close();
- //獲得總頁面數
- this.allpage = (int)Math.ceil((allcol + ipage-1)/ipage);
- if(pages>allpage || pages == 0){
- cpage =1;
- }else{
- cpage = pages;
- }
- String sql="";
- int a1=cpage*ipage-allcol;//判断到达尾页时应该显示的纪录数
- //獲得当前页
- if(((cpage-1)*ipage)==0){
- if(forbiden.equals("")){
- sql = "select * from "+text+" order by "+key+" "+order+" limit "+ipage+"";
- }else{
- sql = "select * from "+text+" where "+forbiden+" order by "+key+" "+order+" limit "+ipage+"";
- }
- }else if(a1<0){
- if(forbiden.equals("")){
- sql = "select * from "+text+" order by "+key+" "+order+" limit "+(cpage-1)*ipage+","+ipage+";";
- }else{
- sql = "select * from "+text+" where "+forbiden+" order by "+key+" "+order+" limit "+(cpage-1)*ipage+","+ipage+";";
- }
- }else if(a1>=0){
- if(forbiden.equals("")){
- sql = "select * from "+text+" order by "+key+" "+order+" limit "+(cpage-1)*ipage+","+a1+";";
- }else{
- sql = "select * from "+text+" where "+forbiden+" order by "+key+" "+order+" limit "+(cpage-1)*ipage+","+a1+";";
- }
- }
- ResultSet rs = consql.getStatement(sql);
- int wei=0;
- if(a1<0){
- wei = (ipage) * (showcolm.length);
- }else if(a1>=0){
- wei = (a1) * (showcolm.length);
- }
- String getcontext[] = new String[wei];
- int help=0;
- while(rs.next()){
- for(int i=0;i<showcolm.length;i++){
- getcontext[help] = rs.getString(showcolm[i]);
- help++;
- }
- }
- this.context = getcontext;
- rs.close();
- return context;
- }
- //参数一中存一个已经完成连接的Connection对象,参数二中存每页显示的行数,第三个参数存要操作的表名,第四个参数存这个表的主键的键名,第五个参数存排序方式 当为""时为正序,当为"desc"时为逆序,后一个参数存要查找的字段,最后一个参数为查询条件
- public String getPageinfsql(int page,String text,String key,String order,String forbiden) throws Exception{
- sqlcontrol consql = new sqlcontrol();
- consql.getSQL2005conn("dbconfig.xml");
- this.ipage = page;
- System.out.println("page "+page );
- String pagesql="";
- if(forbiden.equals("")){
- pagesql = "select count(*) from " + text;
- }else{
- pagesql = "select count(*) from " + text + " where "+forbiden;
- }
- ResultSet pagers = consql.getStatement(pagesql);//取總文章數
- while(pagers.next()){
- this.allcol = pagers.getInt(1);
- System.out.println("allcol "+allcol);
- }
- pagers.close();
- //獲得總頁面數
- this.allpage = (int)Math.ceil((allcol + ipage-1)/ipage);
- if(pages>allpage || pages == 0){
- cpage =1;
- }else{
- cpage = pages;
- }
- String sql="";
- int a1=cpage*ipage-allcol;//判断还剩余的纪录数
- //獲得当前页
- if(((cpage-1)*ipage)==0){
- if(forbiden.equals("")){
- sql = "select Top "+ipage+" * from "+text+" order by "+key+" "+order;
- }else{
- sql = "select Top "+ipage+" * from "+text+" where "+forbiden+" order by "+key+" "+order;
- }
- }else if(a1<0){
- if(forbiden.equals("")){
- sql = "select Top "+ipage+" * from "+text+" where "+key+" not in (select Top "+(cpage-1)*ipage+" "+key+" from "+text+" order by "+key+" "+order+")order by "+key+" "+order;
- }else{
- sql = "select Top "+ipage+" * from "+text+" where "+key+" not in (select Top "+(cpage-1)*ipage+" "+key+" from "+text+" order by "+key+" "+order+") and "+forbiden+" order by "+key+" "+order;
- }
- }else if(a1>=0){
- if(forbiden.equals("")){
- sql = "select Top "+a1+" * from "+text+" where "+key+" not in (select Top "+(cpage-1)*ipage+" "+key+" from "+text+" order by "+key+" "+order+")order by "+key+" "+order;
- }else{
- sql = "select Top "+a1+" * from "+text+" where "+key+" not in (select Top "+(cpage-1)*ipage+" "+key+" from "+text+" order by "+key+" "+order+") and "+forbiden+" order by "+key+" "+order;
- }
- }
- this.select=sql;
- return select;
- }
- //分页控制标记方法,第一个参数存需要分页的文件名,第二个参数存页码参数的名字,第三个参数存页码显示的长度,为了格式美观最好设置5以上的数
- public String controlPage(String Filename,String param,int length){
- StringBuffer in = new StringBuffer();
- in.append("共"+allcol+"条记录 分"+allpage+"页 ");
- if(cpage>1){
- in.append("<a href='"+Filename+"?"+param+"="+(cpage-1)+"'>上一页</a>");
- }
- if(cpage<allpage){
- in.append("<a href='"+Filename+"?"+param+"="+(cpage+1)+"'>下一页</a>");
- }
- in.append(" ");
- int n = length;
- int m=0;
- if((cpage+allpage)>n){
- m=n;
- for(int i=0;i<m;i++){
- in.append("<a href='"+Filename+"?"+param+"="+(i+1)+"'>["+(i+1)+"]</a> ");
- }
- in.append("...");
- }else{
- m=allpage-cpage;
- for(int i=(cpage-1);i<m;i++){
- in.append("<a href='"+Filename+"?"+param+"="+(i+1)+"'>["+(i+1)+"]</a> ");
- }
- }
- in.append(" 当前为第"+cpage+"页");
- this.pagecontrol=in.toString();
- return pagecontrol;
- }
- }