一个通用的JSP分页程序

//本程序使用了一个类反射的类,可以自动将查询出来的值填充到值对象里。
//这个类可以参见ibm的文章:使用类反射机制简化struts的开发.

package com.lyjWeb.wyhn.common;


import java.sql.*;
import com.lyjWeb.common.*;
import java.util.*;

public class Fenye {
 
 private Connection con=null;
 private String sql="";
 private String page="1";// 当前页 
 private int i_page=1;//当前页
 private int i_page_count=10;//每页显示条数
 private String nextpage,prepage,sumpage,sumcount;
 
 private String sql_select,sql_from,sql_where,sql_order;
 private String sql_pre="";
 private String sql_count=""; //count(*)语句
 private ResultSet rst=null;
 private PreparedStatement stm=null;
 
 private Collection result_c=null;
 
 private String s_null="无";
 
///输入值///
 public void setConnection(Connection con)
 {
  this.con=con;
 }

 public void setSql(String sql_select,String sql_from,String sql_where,String sql_order)
 {
  //this.sql=sql;
  this.sql_select=sql_select;
  this.sql_from=sql_from;
  this.sql_where=sql_where;
  this.sql_order=sql_order;
  this.sql_count="select count(*) " + sql_from + " " + sql_where;
  this.sql=sql_select + " " + sql_from + " " + sql_where + " " + sql_order;
 }
 
 public void setStm(PreparedStatement stm)
 {
  this.stm=stm;
 }
 
 public void setPage(String p)
 {
  if(p==null)
  {
   this.page="1";
    this.i_page=1;
  }
  else
  {
   p=p.trim();
   if(p.equals("")) p="1";
   this.page=p;
   this.i_page=Integer.parseInt(this.page);  
  }
  
 }
 
 public void setPageCount(int pcount)
 {
  this.i_page_count=pcount;
 }

 public void setSqlPre(String sqlpre)
 {
  this.sql_pre=sqlpre; 
 }

 public void setS_null(String s_null)
 {
  this.s_null=s_null;
 }
//返回值/// 
 public ResultSet getRst()
 {
  return rst;
 }
 
 public String getPage()
 {
  return page;
 }

 public String getNextPage()
 {
  return nextpage;
 }
 
 public String getPrePage()
 {
  return prepage;
 }
 
 public String getSumPage()
 {
  return sumpage;
 }
 
 public String getCount()
 {
  return sumcount;
 }
 
 public Collection getCollection()
 {
  return result_c;
 }
 
操  作///
 private String b_string,f_string;
 private String comsql;
 
 private void CountPage() throws Exception
 {
  //try{
   PreparedStatement stm1=con.prepareStatement(sql_count);
   ResultSet rst_count=stm1.executeQuery();
   rst_count.next();
   int i_sum=rst_count.getInt(1);
   
   rst_count.close();
   rst_count=null;
   stm1.close();
   stm1=null;

   
   this.sumcount=String.valueOf(i_sum);
   
   int page_all=0;
   if (i_sum % i_page_count==0)
   {
    page_all=i_sum / i_page_count;
   }
   else{
    page_all=i_sum / i_page_count+1;
   }
   
   sumpage=String.valueOf(page_all);
   
   if(i_page>page_all)
   {
    i_page=page_all;
    page=String.valueOf(i_page);
   }else if(i_page<1)
   {
    i_page=1;
    page=String.valueOf(i_page);
   }
   
   if(i_page==page_all)
    nextpage=String.valueOf(i_page) ;
   else
    nextpage=String.valueOf(i_page+1) ;
         
   if(i_page==1)
    prepage="1";
   else
    prepage=String.valueOf(i_page-1);
   
   
   int f_number=i_page * i_page_count;
   int b_number=f_number-i_page_count+1;
   b_string=String.valueOf(b_number);
   f_string=String.valueOf(f_number);      
   
  //}catch(Exception e)
  //{
  // System.out.println("Error in fenye:" + e.toString());
  //}  
 }
 
 private void ComSql()
 {
  
  comsql= " from (select rownum row_id ,ttttt2.* from( " + sql + " ) ttttt2 ) where row_id between " + b_string + " and " + f_string;
  
  if(sql_pre.equals(""))
   comsql=sql_select + comsql;
  else
   comsql=sql_pre+comsql;
  //return comsql;  
 }
 private void SearchData() throws Exception
 {
  String s=comsql;
  stm=con.prepareStatement(s);
  rst=stm.executeQuery(); 
 }
 
 public int DoFenye()
 {
  try{
   this.CountPage();
   this.ComSql();
   this.SearchData();
   return 1;
  }catch(Exception e)
  {
   System.out.println("Error in fenye:" + e.toString()); 
   return 0;   
  }
 }
 
 public int DoFenyeCollection(String thedao)
 {
  try{
   System.out.println(sql_count);
   this.CountPage();
   
   this.ComSql();
   System.out.println(comsql);
   result_c=Tool.Select(comsql,thedao,con,s_null);
   return 1;
  }catch(Exception e)
  {
   System.out.println("Error in fenye:" + e.toString()); 
   return 0;   
  }
 }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值