翻页显示

来源:互联网 发布:黑龙江农垦网络电视台 编辑:程序博客网 时间:2024/04/30 09:22

共四个解决方案

解决一:    
翻页显示,是连db2的,你改一下驱动就可以了
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head>
<title></title>
</head>
<body>
<%
java.sql.Connection sqlCon;
java.sql.Statement sqlStmt;
java.sql.ResultSet sqlRst;
java.lang.String strCon;
java.lang.String strSQL;
int intPageSize;
int intRowCount;
int intPageCount;
int intPage;
java.lang.String strPage;
int i,j,k; 
intPageSize = 10; 
strPage = request.getParameter("page");
if(strPage==null){
intPage = 1;
} else{
intPage = java.lang.Integer.parseInt(strPage);
if(intPage<1) intPage = 1; }
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
strCon = "jdbc:db2:sample";
sqlCon = java.sql.DriverManager.getConnection(strCon,"","");
sqlStmt = sqlCon.createStatement();
strSQL = "select count(*) from org";
sqlRst = sqlStmt.executeQuery(strSQL);
sqlRst.next();
intRowCount = sqlRst.getInt(1);
sqlRst.close();
intPageCount = (intRowCount+intPageSize-1) / intPageSize;
if(intPage>intPageCount) intPage = intPageCount;
strSQL = "select * from org";
sqlRst = sqlStmt.executeQuery(strSQL);
i = (intPage-1) * intPageSize;
for(j=0;j<i;j++) sqlRst.next(); %>
<table border="1">
<tr>
<td colspan=5 align=center>
<%=intPage%>/<%=intPageCount%>
<%if(intPage != 1){%>
<a href="pagedb2.jsp?page=1">first</a>
<%}else{%>
first
<%}%>
<%if(intPage>1){%>
<a href="pagedb2.jsp?page=<%=intPage-1%>"><<</a>
<%
}else{
%>
<<
<%}%>
<%if(intPage<intPageCount){%>
<a href="pagedb2.jsp?page=<%=intPage+1%>">>></a>
<%
}else{
%>
>>
<%}%>
<%if(intPage != intPageCount){%>
<a href="pagedb2.jsp?page=<%=intPageCount%>">last</a>
<%}else{%>
last
<%}%>
all<%=intRowCount%>row
</td>
</tr>
<tr>
<td>deptnumb</td>
<td>deptname</td>
<td>manage</td>
<td>division</td>
<td>location</td>
</tr>
<%
i = 0;
while(i<intPageSize && sqlRst.next()){ %>
<tr>
<td><%=sqlRst.getString(1)%></td>
<td><%=sqlRst.getString(2)%></td>
<td><%=sqlRst.getString(3)%></td>
<td><%=sqlRst.getString(4)%></td>
<td><%=sqlRst.getString(4)%></td>
</tr>
<% i++; } %>
</table>
<%
sqlRst.close();
sqlStmt.close();
sqlCon.close();
%>
</body>
</html>

 


解决二:
把分页写在bean里吧!!!
给你一个例子

package com.bbs.bean;

import java.io.*;
import java.util.*;

public class NoteListBean implements Serializable{

private ArrayList noteWriters=new ArrayList();
private ArrayList noteHitNums=new ArrayList();
private ArrayList noteIDs=new ArrayList();
private ArrayList noteTitles=new ArrayList();
private ArrayList notePics=new ArrayList();
private ArrayList noteRewriteNums=new ArrayList();
private ArrayList noteLastTimes=new ArrayList();

public NoteListBean(){}

public int getLength(){
 return noteIDs.size();
}


public void setNoteIDs(String noteID){
 noteIDs.add(noteID);
}

public String getNoteIDs(int i){
 return (String)noteIDs.get(i);
}

public void setNotePics(String notePic){
 notePics.add(notePic);
}

public String getNotePics(int i){
 return (String)notePics.get(i);
}

public void setNoteWriters(String noteWriter){
 noteWriters.add(noteWriter);
}

public String getNoteWriters(int i){
 return (String)noteWriters.get(i);
}

public void setNoteTitles(String noteTitle){
 noteTitles.add(noteTitle);
}

public String getNoteTitles(int i){
 return (String)noteTitles.get(i);
}

public void setNoteRewriteNums(String noteRewriteNum){
 noteRewriteNums.add(noteRewriteNum);
}

public String getNoteRewriteNums(int i){
 return (String)noteRewriteNums.get(i);
}

public void setNoteHitNums(String noteHitNum){
 noteHitNums.add(noteHitNum);
}

public String getNoteHitNums(int i){
 return (String)noteHitNums.get(i);
}


public void setNoteLastTimes(String noteLastTime){
 noteLastTimes.add(noteLastTime);
}

public String getNoteLastTimes(int i){
 return (String)noteLastTimes.get(i);
}

}


package com.bbs.util;

import java.io.*;
import java.util.*;
import com.bbs.bean.NoteListBean;

public class PageList{

private int pageSize=15;
private int recordCount=0;
private int pageCount=0;
private int showPage=1;
private int pageInt=0;

NoteListBean Note=new NoteListBean();


public int getPageSize(){
 return pageSize;
}


public void setRecordCount(int recordCount){
 this.recordCount=recordCount;
}

public int getRecordCount(){
 return recordCount;
}

public void setPageCount(){
 this.pageCount=(recordCount % pageSize==0)?(recordCount/pageSize):(recordCount/pageSize+1);
 }

public int getPageCount(){
 return pageCount;
}

public void setShowPage(String page){
 setPageCount();
 if(page!=null){
 this.showPage=Integer.parseInt(page);
 if(this.showPage>pageCount)
  this.showPage=pageCount;
  else if(this.showPage<=0)
  this.showPage=1;
 }else
      this.showPage=1;
}

public int getShowPage(){
 return showPage;
}

public void setPageInt(){
 if(recordCount>0)
  this.pageInt=(showPage-1)*pageSize;
 }

public int getPageInt(){
 setPageInt();
 return pageInt;
}

}


解决三:
<table width="760" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="484" valign="top"><a href="add.jsp">add</a>
 <jsp:useBean id="ConBean" scope="page" class="beans.Dbcon" />
      <%!
int PageSize=12;
int ShowPage=1;
int RowCount=0;
int PageCount=0;
%>
      <%
ResultSet rs=ConBean.rs;
Statement stmt=ConBean.stmt;
rs=stmt.executeQuery("select * from user1");
%>
      <%
  rs.last();
  RowCount=rs.getRow();
  PageCount=((RowCount%PageSize)==0?(RowCount/PageSize):(RowCount/PageSize)+1);
  %>
      <%
  String ToPage=request.getParameter("ToPage");
  if(ToPage!=null){
  ShowPage=Integer.parseInt(ToPage);
  if(ShowPage>PageCount){
  ShowPage=PageCount;
  }else if(ShowPage<=0){
  ShowPage=1;
  }
  }
  rs.absolute((ShowPage-1)*PageSize+1);

  %>
      <%
for (int i=1;i<=PageSize;i++)
{
%>
      <table width="760" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <td width="134"><div align="center"><%=rs.getString(1)%></div></td>
          <td width="302"><div align="center"><%=rs.getString(2)%></div></td>
          <td width="136"><div align="center"><%=rs.getString(3)%></div></td>
          <td width="94"><a href="del.jsp?id=<%=rs.getString(1)%>">删除</a></td>
          <td width="94"><a href="edit.jsp?id=<%=rs.getString("id")%>">修改</a></td>
        </tr>
      </table>
      <%if(!rs.next())
   break;
   }%>
      <%
 rs.close();
    stmt.close();
 %>
    </td>
  </tr>
  <tr>
    <td><table width="749" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <td width="1536"><div align="center"></div>
            <table width="740" border="0" align="center" cellpadding="0" cellspacing="0">
              <tr>
                <td width="469" height="70"><div align="right">共<%=PageCount%>/<font color="#FF0000"><%=ShowPage%></font>页</div></td>
                <td width="56"><div align="center">
                    <%if(ShowPage!=1){%>
                    <a href=index.jsp?ToPage=<%=1%>>首页</a>
                    <%}else{%>
                    <font color="#CCCCCC">首页</font>
                    <%}%>
                  </div></td>
                <td width="49"><div align="center">
                    <%if(ShowPage!=1){%>
                    <a href=index.jsp?ToPage=<%=ShowPage-1%>>上页</a>
                    <%}else{%>
                    <font color="#CCCCCC">上页</font>
                    <%}%>
                  </div></td>
                <td width="54"><div align="center">
                    <%if(ShowPage!=PageCount){%>
                    <a href=index.jsp?ToPage=<%=ShowPage+1%>>下页</a>
                    <%}else{%>
                    <font color="#CCCCCC">下页</font>
                    <%}%>
                  </div></td>
                <td width="46"><div align="center">
                    <%if(ShowPage!=PageCount){%>
                    <a href=index.jsp?ToPage=<%=PageCount%>>尾页</a>
                    <%}else{%>
                    <font color="#CCCCCC">尾页</font>
                    <%}%>
                  </div></td>
                <td width="66" valign="bottom"><div align="center">
                    <table width="99%" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td> <form action=index.jsp method=post>
                            <input name=ToPage type="text"  value=<%=ShowPage%> size="4">
                            页</form></td>
                      </tr>
                    </table>
                  </div></td>
              </tr>
            </table></td>
        </tr>
      </table></td>
  </tr>
</table>

用我这一个吧.很简单的.
很不错的啊...
^_^

解决四:script
楼上的兄弟们说得都很好呀,但是我觉得一般查询得到的数据都放在table中把,要是想实现分页用javascript实现也很方便:),代码还是原来一样写,自动在表中就有滚动条了。。。。下边是以前某个老大给的例子,我用的时候改了一下没改完很乱但是能用,呵呵:)
<html>
<head>
<title>
main
</title>
<STYLE type=text/css>
.sort-table {
        BORDER-RIGHT: buttonshadow 1px solid; BORDER-TOP: buttonshadow 1px solid; BACKGROUND: window; FONT: Icon; BORDER-LEFT: buttonshadow 1px solid; COLOR: windowtext; BORDER-BOTTOM: buttonshadow 1px solid
}
.sort-table THEAD {
        BACKGROUND: buttonface
}
.sort-table THEAD TD {
        BORDER-RIGHT: buttonshadow 1px solid; BORDER-TOP: buttonhighlight 1px solid; BORDER-LEFT: buttonhighlight 1px solid; CURSOR: default; BORDER-BOTTOM: buttonshadow 1px solid
}
.sort-table TBODY TD {
        BORDER-RIGHT: buttonshadow 1px solid; BORDER-TOP: buttonhighlight 1px solid; BORDER-LEFT: buttonhighlight 1px solid; CURSOR: default; BORDER-BOTTOM: buttonshadow 1px solid
}

</STYLE>
<script language="JavaScript">
function fixTable()

 for(var i=0;i<mainTableHead.rows[0].cells.length;i++)
 {
  if(mainTableHead.rows[0].cells[i].offsetWidth>eval("col" + i).offsetWidth)
  {
   eval("col" + i).style.width = mainTableHead.rows[0].cells[i].offsetWidth-10
  }
  else
  {
   mainTableHead.rows[0].cells[i].style.width=eval("col" + i).offsetWidth-10
  }
 }
 
 if(mainTableHead.offsetWidth>mainTableBody.offsetWidth)
 {
  mainTableBody.style.width = mainTableHead.offsetWidth
 }
 else
 {
  mainTableHead.width = mainTableBody.offsetWidth
 }
 Layer1.style.width = Layer1.scrollWidth+16
 Layer1.style.height = document.body.clientHeight - Layer1.offsetTop

}
window.attachEvent("onresize",new Function("fixTable()"))
window.attachEvent("onload",new Function("fixTable()"))
</script>
</head>
</head>


<TABLE class=sort-table cellSpacing=0 cellPadding=4 id=mainTableHead>
  <THEAD>
    <TR id="mainHead" align="middle">
      <TD noWrap>权限</TD>
      <TD noWrap>表名/操作</TD>
    </TR>
  </THEAD>
  </TABLE>
  <DIV id=Layer1 style="Z-INDEX: 1; OVERFLOW: auto;  POSITION: absolute;height=300">
  <TABLE class=sort-table cellSpacing=0 cellPadding=4 id="mainTableBody">
  <COLGROUP>
  <COL id="col0">
  <COL id="col1">
  </COLGROUP>
  <TBODY>
    <TR>
      <TD noWrap width="60"><input type="checkbox" name="C1" value="ON"></TD>
      <TD noWrap>表表表表表表表表表表表表表表表表表表表表表表表表表表表表表表表</TD>
     

    </TR>
    <TR>
      <TD noWrap width="60"><input type="checkbox" name="C2" value="ON"></TD>
      <TD noWrap>13</TD>
    
    </TR>
    <TR>
      <TD noWrap width="60"><input type="checkbox" name="C3" value="ON"></TD>
      <TD noWrap>13</TD>
    
    </TR>
    <TR>
      <TD noWrap width="60"><input type="checkbox" name="C4" value="ON"></TD>
      <TD noWrap>13</TD>
    
    </TR>
    <TR>
      <TD noWrap width="60"><input type="checkbox" name="C5" value="ON"></TD>
      <TD noWrap>13</TD>
     
    </TR>
    <TR>
      <TD noWrap width="60"><input type="checkbox" name="C7" value="ON"></TD>
      <TD noWrap>13</TD>
    
    </TR>
    <TR>
      <TD noWrap width="60"><input type="checkbox" name="C6" value="ON"></TD>
      <TD noWrap>13</TD>
   
    </TR>
    <TR>
      <TD noWrap width="60"><input type="checkbox" name="C8" value="ON"></TD>
      <TD noWrap>13</TD>
    
    </TR>
    <TR>
      <TD noWrap width="60"><input type="checkbox" name="C9" value="ON"></TD>
      <TD noWrap>13</TD>
   
    </TR>
    <TR>
      <TD noWrap width="60"><input type="checkbox" name="C10" value="ON"></TD>
      <TD noWrap>13</TD>
     
    </TR>
    <TR>
      <TD noWrap width="60"><input type="checkbox" name="C11" value="ON"></TD>
      <TD noWrap>13</TD>
     
    </TR>
    <TR>
      <TD noWrap width="60"><input type="checkbox" name="C12" value="ON"></TD>
      <TD noWrap>13</TD>
     
    </TR>
    <TR>
      <TD noWrap width="60"><input type="checkbox" name="C13" value="ON"></TD>
      <TD noWrap>13</TD>
    
    </TR>
    <TR>
      <TD noWrap width="60"><input type="checkbox" name="C14" value="ON"></TD>
      <TD noWrap>13</TD>
    
    </TR>
    <TR>
      <TD noWrap width="60"><input type="checkbox" name="C15" value="ON"></TD>
      <TD noWrap>13</TD>
    
    </TR>
    <TR>
      <TD noWrap width="60"><input type="checkbox" name="C16" value="ON"></TD>
      <TD noWrap>13</TD>
    </TR>
    <TR>
      <TD noWrap width="60"> </TD>
      <TD noWrap>13</TD>
    </TR>
    <TR>
      <TD noWrap width="60"> </TD>
      <TD noWrap>13</TD>
    </TR>
    <TR>
      <TD noWrap width="60"> </TD>
      <TD noWrap>13</TD>
    </TR>
    <TR>
      <TD noWrap width="60"> </TD>
      <TD noWrap>13</TD>
    </TR>
    <TR>
      <TD noWrap width="60"> </TD>
      <TD noWrap>13</TD>
    </TR>
    <TR>
      <TD noWrap width="60"> </TD>
      <TD noWrap>13</TD>
    </TR>
    <TR>
      <TD noWrap width="60"> </TD>
      <TD noWrap>13</TD>
    </TR>
    <TR>
      <TD noWrap width="60"> </TD>
      <TD noWrap>13</TD>
    </TR>
    <TR>
      <TD noWrap width="60"> </TD>
      <TD noWrap>13</TD>
    </TR>
    <TR>
      <TD noWrap width="60"> </TD>
      <TD noWrap>13</TD>
    </TR>
    <TR>
      <TD noWrap width="60"> </TD>
      <TD noWrap>13</TD>
    </TR>
    <TR>
      <TD noWrap width="60"> </TD>
      <TD noWrap>13</TD>
    </TR>
    <TR>
      <TD noWrap width="60"></TD>
      <TD noWrap>13</TD>
    </TR>
  </TBODY>
</TABLE>
</DIV>
</body>
</html>

原创粉丝点击