分页Bean的同步问题,,请大家帮忙解决

nickcour 04-08-13

请问各位:

我做一个分页的Bean,在一个页面上应用没有问题,但应用在一个框架的网页上多个页面同时刷新或读取,就出现[Microsoft][SQLServer 2000 Driver for JDBC]No more data available to read
但是我在一个页面上应用一点事也没有,那个错误,只要刷新一下出错的框加就没有问题,请问大家,这是否一个同部的问题!!

我的Bean是这样的:

package pmc.xpages;

import java.sql.*;
import javax.servlet.http.*;
import java.util.*;

import pmc.database.*;

public class Fen_Page{
final private String Btn_First="<font face=webdings>9</font>"; //定义第一页按钮显示样式
final private String Btn_Prev="<font face=webdings>3</font>"; // '定义前一页按钮显示样式
final private String Btn_Next="<font face=webdings>4</font>"; // '定义下一页按钮显示样式
final private String Btn_Last="<font face=webdings>:</font>"; // '定义最后一页按钮显示样式
final private String XD_Align="Center"; // '定义分页信息对齐方式
final private String XD_Width="100%"; // '定义分页信息框大小

private String Sql_Msg ;

private int Now_Page=1; //当前页数
private int Total_Page=0; //总页数
private int PageSize=10; //第页大小
private int Total_Rec=0; //总记录数

private HttpServletResponse response;
private HttpServletRequest request;

public Fen_Page(){}
public Fen_Page(int pz,HttpServletResponse res,HttpServletRequest req){
this.PageSize=pz;
this.response=res;
this.request=req;
}

public void setPageSize(int psize){ //设置分页的大小,每页多少条记录
if (psize==0 ){
psize=10;
}
this.PageSize=psize;
}
public int getPageSize(){ //反回每页多少条记录
return PageSize;
}

public void setTotal_Rec(int tr){
Total_Rec=tr;
}
public int getTotal_Rec(){
return(Total_Rec);
}
public int getNow_Page(){
return(Now_Page);
}
public void setNow_Page(int np){
Now_Page=np;
}
public synchronized ArrayList getAbsolute(String sql)throws Exception{

ResultSet rs=null;
ResultSetMetaData RSM=null;
ArrayList AL=new ArrayList();
DBConnect dbc=new DBConnect();

int Absolute=1;
try{
dbc.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=dbc.executeQuery();
if (rs.next()){
rs.last();
Total_Rec=rs.getRow(); //获取总记录数
}

getPageInfo();
Absolute=(Now_Page-1)*PageSize+1; //获取绝对的记录地址

RSM = rs.getMetaData();

rs.absolute(Absolute);
rs.previous();

//System.out.println("AbsPage="+Absolute);
//System.out.println("PageSize="+PageSize);
int count=0;
while(rs.next() && count<this.PageSize){
Hashtable htable=new Hashtable();
for (int i=1;i<=RSM.getColumnCount();i++){
String col_Name= RSM.getColumnName(i);
String col_Type=RSM.getColumnTypeName(i);
Object obj=null;
if (col_Type.equals("nvarchar") || col_Type.equals("varchar") || col_Type.equals("text")){
try{
obj=new String(rs.getString(col_Name));
}catch(Exception e){
obj=new String("");
}
}else if ((col_Type.equals("int")) || (col_Type.equals("int identity")) ){
try{
obj=new Integer(rs.getInt(col_Name));
}catch(Exception e){
obj=new Integer(0);
}
}else if (col_Type.equals("money")){
try{
obj=new Float(rs.getFloat(col_Name));
}catch(Exception e){
obj=new Float(0);
}
}else if (col_Type.equals("decimal")){
try{
obj=new Double(rs.getDouble(col_Name));
}catch(Exception e){
obj=new Double(0);
}
}else if (col_Type.equals("datetime")){
try{
obj=rs.getTimestamp(col_Name);
}catch(Exception e){
obj=new String("");
}
}
htable.put(col_Name,obj);
}
AL.add(htable);
count++;
}
rs.close();
}catch(Exception e){
throw new Exception("Bean is Worng!"+e.getMessage());
}finally{
if (rs!=null)rs.close();
dbc.close();
}
return(AL);
}


private void getPageInfo(){

//System.out.println("Total_Rec % PageSize=" + Total_Rec % PageSize);
if (Total_Rec % PageSize==0){ //-------------------------获取总页数
Total_Page = Total_Rec/PageSize;
}else{
Total_Page = Total_Rec / PageSize + 1;
//System.out.println("Total_Rec / PageSize + 1=" + Total_Page);
}

String npage=request.getParameter("page");
if ("".equals(npage) || npage==null) {
Now_Page=1;
}else{
Now_Page=Integer.parseInt(npage);
}
if (this.Now_Page>Total_Page) Now_Page=Total_Page;
if (this.Now_Page<1) Now_Page=1; //-------------------------当前页
}

public String ShowPage(){
String show_tab;
getPageInfo();
show_tab="<table border=0 width=" + XD_Width + "><tr><td align=" + XD_Align + ">";
show_tab+=ShowFirstPrv();
//show_tab+=showNumBtn();
show_tab+=ShowNextLast();
show_tab+=ShowPageInfo();
show_tab+="</td></tr></table>";
return(show_tab);

}

private String ShowFirstPrv(){

String str_tmp="";
int int_prvpage;
if (Now_Page==1){
str_tmp=Btn_First + " " + Btn_Prev;
}else{
int_prvpage=Now_Page-1;
str_tmp="<a href=" + geturl() + "1>" + Btn_First + "</a> <a href=" + geturl() + int_prvpage +">"+ Btn_Prev+"</a>";
}
return(str_tmp);
}

private String ShowNextLast(){
String str_tmp="";
int int_Nextpage;
if (Now_Page==Total_Page){
str_tmp=Btn_Next + " " + Btn_Last;
}else{
int_Nextpage=Now_Page+1;
str_tmp="<a href=" + geturl() + int_Nextpage + ">" + Btn_Next + "</a> <a href=" + geturl() + Total_Page +">"+ Btn_Last+"</a>";
}
return(str_tmp);
}
private String showNumBtn(){
String str_tmp="";
int StartPage=0;
int EndPage=0;

StartPage=Now_Page-4;
EndPage=Now_Page+5;

if (StartPage<=0) StartPage=1;
if (StartPage==1) EndPage=10;
if (EndPage>Total_Page) EndPage=Total_Page ;

for(int i=StartPage;i<=EndPage;i++){
str_tmp=str_tmp + "<a href=" + geturl() + i +">["+i+"]</a> ";
}
return(str_tmp);
}
private String ShowPageInfo(){
String str_tmp;
str_tmp="页次:" + Now_Page + "/" + Total_Page + "页 共" + Total_Rec + "条记录 " + PageSize + "条/每页";
return(str_tmp);
}

private String geturl(){
String FindString="page=";
String HttpFile;
String queryString;

HttpFile=request.getRequestURI();
HttpFile=HttpFile.substring(HttpFile.lastIndexOf("/")+1); //获取文件名


queryString=request.getQueryString(); //获取参数
//System.out.println("queryString=" + queryString);
if ("".equals(queryString) || queryString==null){
HttpFile+="?page=";
}else{
int fs=queryString.lastIndexOf(FindString);

//System.out.println("LastIndexOf(page)==" + fs);

if (fs<0){
//HttpFile+="?" + queryString + FindString;***************************2004/4/27
HttpFile+="?" + queryString + "&page=";
}else{
fs=queryString.lastIndexOf(FindString)-1;
//System.out.println("lastIndexOf(FindString)-2==" + fs);
if (fs<0){
HttpFile+="?page=";
}else{
queryString=queryString.substring(0,fs);
//System.out.println("queryString.substring(1,fs)==" + queryString);
HttpFile+="?" + queryString + "&page=";
}
}
}
return(HttpFile);
}


}

//========以下是使用的JSP

<%
String sql="select * from product ";
Fen_Page fp=new Fen_Page(15,response,request);
ArrayList al=fp.getAbsolute(sql);
out.print(al.size());
for (int i=0;i<al.size();i++){
Hashtable htable=(Hashtable)al.get(i);
out.print(htable.get("ProductName")+"<BR>");
}
%>
<%=fp.ShowPage()%>

nickcour
2004-08-13 22:33

//========以下是使用的JSP

<%
String sql="select * from product ";
Fen_Page fp=new Fen_Page(15,response,request);
ArrayList al=fp.getAbsolute(sql);
out.print(al.size());
for (int i=0;i<al.size();i++){
Hashtable htable=(Hashtable)al.get(i);
out.print(htable.get("ProductName")+"<BR>");
}
%>
<%=fp.ShowPage()%>

nickcour
2004-08-13 22:35

<%
String sql="select * from product ";
Fen_Page fp=new Fen_Page(15,response,request);
ArrayList al=fp.getAbsolute(sql);
out.print(al.size());
for (int i=0;i<al.size();i++){
Hashtable htable=(Hashtable)al.get(i);
out.print(htable.get("ProductName")+"<BR>");
}
%>
<%=fp.ShowPage()%>