一个很好用的jsp分页Bean

import java.sql.*;
import beans.DBManager;

public class Page {
Connection conn = null;
Statement stmt = null;
ResultSet CountTopicrs = null; //初始化总记录数Rs变量
ResultSet Pagirs = null; //初始化分页时每页的记录集数Rs变量
public static void main(String[] args) ...{}
private int intCountTopic = 0; //主题总数,即select选出的、库中所有记录总数
public int intPageSize; //每页显示主题数,即每页显示的记录总数
public int intPageCount; //总页数
public int intPage = 0; //当前页数
private String Countsql = null,Pagisql = null,str = null,str_where = null;
private String str_parameter = "";
//public static int pages_n=1; //传分页参数值

private String nowPage; //初始化当前页intPage变量,以准确便获取当前页,即获取当前页的具体页号。
private String HttpFile; //当前的地址栏的文件,即具体jsp文件。

//连接数据库
public Page(){
try{
this.conn=DBManager.getConnection();
this.stmt=conn.createStatement();
}catch(Exception e){
e.printStackTrace();
}
}

//接收传分页参数
public void setPages(int n) ...{
intPageSize = n;
}

public String getPagisql() ...{
return Pagisql;
}

/**//*功能:接收参数组织SQL语句
*str_table :分页显示的表名
*str_where:分页的where条件
*httpfile :具体jsp文件
*pages :获取地址栏传过来的pages参数
*/
public ResultSet setQuerysql(String str_table,String str_where,String httpfile,String pages)
throws SQLException ...{
ResultSet r = null;
this.nowPage = pages;
this.HttpFile = httpfile; //分页文件名

Countsql = "select count(*) from " + str_table + " " + str_where;
Pagisql ="select * from "+ str_table+ " " + str_where;
try ...{
r = querySql(Countsql, Pagisql);
} catch (SQLException _ex) ...{
System.out.println(_ex);
}
return r;
}

/**//*功能:接收参数进行首尾页判断
*Countsql:总记录的Query字符串。[形式为select count(*) from tablename]
*Pagisql :要分页的Query字符串。[形式为select * from tablename where ...]
*request :参数传递过程中的变量。[用来控制翻页时的pages变量]
*/
public ResultSet querySql(String Countsql,String Pagisql) //,HttpServletRequest request
throws SQLException ...{
//获取当前文件名。
//HttpFile=request.getRequestURI();
//获取当前页,将数值赋予intPage变量。[分页栏中必须要有pages参数]
//nowPage=request.getParameter("pages");//由参数HttpServletRequest request传递而来

if (nowPage == null) ...{
intPage = 1;
} else ...{
intPage = Integer.parseInt(nowPage);
if (intPage < 1)
intPage = 1;
} //end else

//获取总记录数的结果集。

CountTopicrs = this.stmt.executeQuery(Countsql);
if (CountTopicrs.next()) ...{
intCountTopic = CountTopicrs.getInt(1); //获取第一个字段的整型
}
//获取总页数。
intPageCount = (intCountTopic + intPageSize - 1) / intPageSize;
//如果当前页大于总页数,则当前页等于总页数。//=最后一页
if (intPage > intPageCount) ...{
intPage = intPageCount;
}
//关闭总主题数的数据集。
CountTopicrs.close();

//获取执行分页的结果集。
Pagirs = this.stmt.executeQuery(Pagisql);
return Pagirs;
} //end querySql function.

//获取记录总数。
public int getCountTopic() ...{
return intCountTopic;
}

//获取总页数。
public int getPageCount() ...{
return intPageCount;
}

//获取当前页数。
public int getIntPage() ...{
return intPage;
}

//获取当前页的数据。boodata为True,表示要加入该数据到当前页。
//这里可能会在JSP调用时影响速度[因为调用时要多一层循环],因此放到JSP中嵌入,待改进。
//该代码暂时保留。
// public boolean getData(){
// boolean boodata=false;
// if (intPageCount>0)
// {
// try
// {
// while (Pagirs.next())
// {
// i++;
/**//// if (i>((intPage-1)*intPageSize) &&(i<=intPage*intPageSize))
// {
// boodata=true;
// }
// } //endwhile.
// }//end try.
// catch(Exception e){
// System.out.println(e.toString());
// }
// } //endif.
// return boodata;
// } //end getData();

//设置分页参数
public void setPfoot(String str) ...{
this.str_parameter += str;
}

//分页栏函数。
public String PageFooter() ...{
String str = "<form action=" + HttpFile + " name=form1 methord=post>";
int next, prev;
prev = intPage - 1;
next = intPage + 1;
str += "<font style='font-size: 9pt'>总计<font color='red'>"
+ getCountTopic()
+ "</font>条记录,"
+ "【共<font color='red'>"
+ getPageCount()
+ "</font>页】";
str += "【条"
+ intPageSize
+ "/页】 当前第<font color='red'>"
+ getIntPage()
+ "</font>页(列出第"
+ ((intPageSize * getIntPage() + 1) - intPageSize)
+ "到第"
+ (getIntPage() * intPageSize)
+ "条) &nbsp; &nbsp; ";
//getIntPage()*intPageSize
if (intPage > 1)
str += " <A href="
+ HttpFile
+ "?pages=1"
+ str_parameter
+ ">第一页</A> ";
else
str += " 第一页 ";

if (intPage > 1)
str += " <A href="
+ HttpFile
+ "?pages="
+ prev
+ str_parameter
+ ">上一页</A> ";
else
str += " 上一页 ";

if (intPage < intPageCount)
str += " <A href="
+ HttpFile
+ "?pages="
+ next
+ str_parameter
+ ">下一页</A> ";
else
str += " 下一页 ";

if (intPageCount > 1 && intPage != intPageCount)
str += " <A href="
+ HttpFile
+ "?pages="
+ intPageCount
+ str_parameter
+ ">最后页</A>";
else
str += " 最后页 </font>";
str
+= " 转到<INPUT TYPE='text'NAME='pages' size='2'>页 <input type='submit' name='Submit' value='go'></form>";
return str;
}
//关闭数据库连接
public void closeConn() ...{
try...{
if(CountTopicrs!=null) CountTopicrs.close();
if(Pagirs!=null) Pagirs.close();
this.stmt.close();
this.conn.close();
}catch(Exception e)...{
e.printStackTrace();
}
}

} //end.

**********************************************************

<%@ page contentType="text/html; charset=GBK" import="java.sql.*,java.util.*" %>
<jsp:useBean id="pagi" scope="page" class="beans.Page"></jsp:useBean>
<html>
<head>
<title>
agentPage
</title>
</head>
<body bgcolor="#ffffff">
<table>
<%
String nowPage="";
String str_where="";
String footer="";
String id="",cuclass="",parentclass="",author="",title="",d="";
nowPage=request.getParameter("pages");
if(nowPage==null||nowPage.equals("null"))...{
nowPage="0";
}
pagi.setPages(3);
try...{
ResultSet rs=pagi.setQuerysql("agent",str_where,"agent_page.jsp",nowPage);
footer=pagi.PageFooter();
out.print(footer);
if(pagi.intPageCount>0)...{
int i=0;
while(rs.next())...{
i++;
if(i>((pagi.intPage-1) * pagi.intPageSize)&&(i<=pagi.intPage*pagi.intPageSize))...{
id=rs.getString("AgentId")+"";
parentclass=rs.getString("AgentClass")+"";
cuclass=rs.getString("AgentSubClass")+"";
d=rs.getString("AgentNewProduct")+"";
title=rs.getString("AgentSf")+"";
author=rs.getString("AgentCorpName")+"";
%>
<tr>
<td><%=id%></td>
<td><%=parentclass%></td>
<td><%=cuclass%></td>
<td><%=d%></td>
<td><%=title%></td>
<td><%=author%></td>

</tr>
<%
}
}
}
rs.close();
}catch(Exception e)...{
e.printStackTrace();
}finally...{
pagi.closeConn();
}

%>
</table>

</body>
</html>
**********************************************************

import java.sql.*;
import java.io.*;
//连接数据库的工具类

public class DBManager ...{

public static Connection getConnection()...{
Connection conn = null;
String CLASSFORNAME = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String DBURL = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=txyao";
String USER = "txyao";
String PWD = "txyao";
try...{
Class.forName(CLASSFORNAME);
conn = DriverManager.getConnection(DBURL,USER,PWD);
}catch(Exception e)...{
e.printStackTrace();
}
return conn;
}
}

你可能感兴趣的:(sql,bean,jsp,jdbc,Microsoft)