A common JSP paging program
Last Update:2017-02-28
Source: Internet
Author: User
js| Program | Paging//This program uses a class that reflects the values of the query to automatically populate the Value object.
This class can refer to IBM's article: using class reflection to simplify the development of 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";//Current page
private int i_page=1;//Current page
private int i_page_count=10;//show number of bars per page
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 (*) statement
Private ResultSet Rst=null;
Private PreparedStatement Stm=null;
Private Collection Result_c=null;
Private String s_null= "None";
Input value///////////////////////////////
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;
}
return value///////////////////////////////////////////
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;
}
Operation///////////////////////////////////////////////
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;
}
}
}