分页JavaBean

1/5/2008来源:Java教程人气:7092


  分页javaBEAN

package hzdq.fdjc.Common;

import java.sql.*;
import java.util.*;

/**
* Title:分页
* Description:
* Copyright: Copyright (c) 2004
* Company:
* author:颜喜班
* @version 1.0
*/
public class SplitPager
{
/*
* _sql_str:传入的sql语句
* _total_records: 总记录数目
* _pageSize: 每页显示的记录数目
* _page: 所分的逻辑页数
*/
PRivate Connection con=null;
private Statement stmt=null;
private ResultSet rs=null;
private ResultSetMetaData rsmd=null;
private String _sql_str;
private int _total_records;
private int _pages;
private int _pagesize;
public void setConnection(Connection con)
{
this.con=con;
if (this.con == null)
System.out.println("Failure to get a connection!");
else
System.out.println("SUCcess to get a connection!");
}
public void initialize(String sqlStr,int pageSize)
{
this._sql_str=sqlStr;
this._pagesize=pageSize;
try{
stmt=this.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(this._sql_str);
rsmd=rs.getMetaData();
if (rs!=null)
{
rs.last();
this._total_records = rs.getRow();
rs.first();
this._pages = (this._total_records - 1) / this._pagesize + 1;
}
}
catch(SQLException e){System.out.println(e.toString()); }
}
public Vector getPage(int ipage){
Vector vData=new Vector();
int n=ipage;
int m=0;
m=(n-1)*this._pagesize;
try{
if (rs!=null)
{
if (n!=1) {rs.absolute(m);}
for(int i=0;i<this._pagesize;i++){
String[] sData=new String[6];
for(int j=0;j<rsmd.getColumnCount();j++)
{
sData[j]=rs.getString(j+1);
}
if (sData==null)
{
break;
}
vData.addElement(sData);
rs.next();
}
}
rs.close();
stmt.close();
}
catch(SQLException e){System.out.println(e.toString()); }
return vData;
}
public int getPages()
{
return this._pages;
}
public int getTotalRecords()
{
return this._total_records;
}

}

jsp页面
<%@ page contentType="text/Html;charset=gb2312" language="java" %>
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="hzdq.fdjc.DbConn.*" %>
<jsp:useBean id="splitPager" scope="page" class="hzdq.fdjc.Common.SplitPager"/>
<%
//数据库连接
DBConnectionManager Conn=DBConnectionManager.getInstance();
Connection con=Conn.getConnection();
splitPager.setConnection(con);
int pageSize = 2;
String sqlStr = "select id,tilte from news";
splitPager.initialize(sqlStr,pageSize);
int page1=1;
//通过get数据获取当前页号
String strPage=null;//跳到的页数
strPage=request.getParameter("page1");
if (strPage==null)
{
page1=1;
}
else
{
try{page1=Integer.parseInt(strPage);}
catch(NumberFormatException e){}
if (page1<1) page1=1;
if (page1>splitPager.getPages()) page1=splitPager.getPages();
}
System.out.print("sqlStr:"+sqlStr+"page1:"+page1+"pageSize:"+pageSize);
Vector vData=splitPager.getPage(page1);
%>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" href="../include/ydl.CSS" type="text/css">
<script language="Javascript">
function error()
{
pages=document.sinfo.page1.value;
if(!isNumber(pages))
{
document.sinfo.page1.value="";
return false;
}
}

function isNumber(inputStr)
{
for(var I=0;I<inputStr.length;I++)
{
var oneChar=inputStr.substring(I,I+1);
if((oneChar<´0´ oneChar>´9´))
{
alert("对不起,此处只能输入数字字符!!");
return false;
}
}
return true;
}

function isValid(form)
{
if(isNumber(form.page1.value))
{
form.page1.value="";
form.page1.focus();
return false;
}
else
{
return true;
}
}

function jumpto()
{
pages=document.sinfo.page1.value;
if(!isNumber(pages))
{
document.sinfo.page1.value="1";
pages="1";
}
if(pages == "")
pages = "1";
document.sinfo.page1.value = pages;
document.sinfo.submit();
}
</script>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<br>
<table width="500" border="0" cellspacing="0" cellpadding="0" align="center" background="../images/lone_2.gif">
<tr>
<td width="15"><img src="http://www.QQread.com/images/lone_1.gif" width="15" height="30"></td>
<form name="sinfo" method="post" action="test.jsp" onSubmit="return isValid(this);">
<td align="right"> <font>共<%=splitPager.getTotalRecords()%>条 <%=page1%>/<%=splitPager.getPages()%>
<%=pageSize%>条/页
<%
if (page1!=1)
{
%>
<a href="test.jsp?page1=1">首页</a>
<a href="test.jsp?page1=<%=(page1-1)%>">上一页</a>
<%
}
if (page1!=splitPager.getPages())
{
%>
<a href="test.jsp?page1=<%=(page1+1)%>">下一页</a>
<a href="test.jsp?page1=<%=splitPager.getPages()%>">最后一页</a>
<%
}
%>
跳到</font>
<input type="text" name="page1" maxlength="4" size="4" style="font-size:9px" onChange="javascript:{error()}">
</td>
</form>
<td width="79"><input type="image" img src="http://www.qqread.com/images/lone_3.gif" width="79" height="30" onClick="jumpto()" alt="跳到" name="image"></td>
</tr>
</table>
<br>
<table width="98%" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td height="2" bgcolor="#48B9EA"></td>
</tr>
</table>
<table width="98%" border="0" cellspacing="2" cellpadding="2" align="center" background="../images/dian.gif">
<form action="" method="mothod" name="form">
<tr align="center" bgcolor="#ECECE3">
<td height="22" bgcolor="#ECECE3"><font size="2" color="#3366CC">组名</font></td>
<td width="60%" height="22"><font size="2" color="#3366CC">描述</font></td>
<td width="5%" height="22"><font size="2" color="#3366CC">选择</font></td>
</tr>
<% for(int i=0;i<vData.size();i++){
//显示数据
String[] sData=(String[])vData.get(i);
%>
<tr>
<td height="20" align="center" bgcolor="#F5F5F5"><%=sData[0]%></td>
<td width="60%" height="20" align="center" bgcolor="#F5F5F5">
<%if (sData[1]==null) out.print("");else out.print(sData[1]);%>
</td>
</tr>
<%
}

%>
</form>
</table>
<table border="0" width="400" align="center">
<tr align="center">
<td height="40">
<input type="button" name="Input" value="返 回" onClick="history.go(-1)" class="button">
</td>
<td height="40">
<input type="button" name="Input" value="添 加" onClick="add()" class="button">
</td>
<td height="40">
<input type="button" name="Input" value="修 改" onClick="mod()"class="button">
</td>
<td height="40">
<input type="button" name="Input" value="删 除" onClick="del()" class="button">
</td>
</tr>
</table>
</body>
<script language="javascript">
function add()
{
document.form.action="group_add.jsp";
document.form.method="post";
document.form.submit();
}
function mod()
{
var j=0;
var flag;
flag=false;
if (form.chkEmp.length==undefined)
{
if (form.chkEmp.checked)
flag=true;
}
for(i=0;i<form.chkEmp.length;i++)
{
if(form.chkEmp[i].checked)
{
flag=true;
j++;
}
}
if(j>1)
flag=false;
if(flag==false)
{
alert("请选择一条要修改的记录.");
return false;
}
document.form.action="group_mod.jsp";
document.form.method="post";
document.form.submit();
}

function del()
{
var flag;
flag=false;
if (form.chkEmp.length==undefined)
{
if (form.chkEmp.checked)
flag=true;
}
for(i=0;i<form.chkEmp.length;i++)
{
if(form.chkEmp[i].checked)
{
flag=true;
break;
}
}
if(flag==false)
{
alert("请选择您需要删除的记录");
return false;
}
if(!window.confirm("删除该组会删掉该组下所有朋友,确认删除(yes/No)"))
return false;
document.form.action="group_del.jsp";
document.form.method="post";
document.form.submit();
return true;
}
</script>
</html>
<%
Conn.freeConnection(con);
%>

改进如下:
package hzdq.fdjc.Common;

import java.sql.*;
import java.util.*;

/**
* Title:分页
* Description:
* Copyright: Copyright (c) 2004
* Company:
* author:颜喜班
* @version 1.0
*/
public class SplitPager
{
/*
* _sql_str:传入的sql语句
* _total_records: 总记录数目
* _pageSize: 每页显示的记录数目
* _page: 所分的逻辑页数
*/
private Connection con=null;
private Statement stmt=null;
private ResultSet rs=null;
private ResultSetMetaData rsmd=null;
private String _sql_str;
private int _total_records;
private int _pages;
private int _pagesize;
public void setConnection(Connection con)
{
this.con=con;
if (this.con == null)
System.out.println("Failure to get a connection!");
else
System.out.println("Success to get a connection!");
}
public void initialize(String sqlStr,int pageSize)
{
this._sql_str=sqlStr;
this._pagesize=pageSize;
try{
stmt=this.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(this._sql_str);
rsmd=rs.getMetaData();
if (rs!=null)
{
rs.last();
this._total_records = rs.getRow();
rs.first();
this._pages = (this._total_records - 1) / this._pagesize + 1;
}
}
catch(SQLException e){System.out.println(e.toString()); }
}
public Vector getPage(int ipage){
Vector vData=new Vector();
int n=ipage;
int m=0;
m=(n-1)*this._pagesize+1;
try{
if (rs!=null)
{
//if (n!=1) {rs.absolute(m);}
rs.absolute(m);
for(int i=0;i<this._pagesize;i++){
String[] sData=new String[6];
for(int j=0;j<rsmd.getColumnCount();j++)
{
sData[j]=rs.getString(j+1);
}
if (sData==null)
{
break;
}
vData.addElement(sData);
rs.next();
}
}
rs.close();
stmt.close();
}
catch(SQLException e){System.out.println(e.toString()); }
return vData;
}
public int getPages()
{
return this._pages;
}
public int getTotalRecords()
{
return this._total_records;
}

}