分享
 
 
 

分页JavaBean

王朝java/jsp·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

分页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;

privateStatement 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;

privateStatement 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;

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有