高分求救~~!!哪位大侠可以给我一份完整的jsp分页代码(可以在我机器上运行的)
一份完整的jsp分页代码(可以在我机器上运行的)谢谢拉
[解决办法]
import java.io.*;
import javax.naming.*;
import java.sql.*;
import javax.sql.*;
import java.util.*;
public class PagesBean
{
String[] before_next;
public String[] getBefore_next()
{
return before_next;
}
/**
public Connection getConnection()
{
Connection conn=null;
try
{
Context initContext=new InitialContext();
Context envContext=(Context)initContext.lookup( "java:/comp/env ");
DataSource ds=(DataSource)envContext.lookup( "jdbc/sample_db ");
conn=ds.getConnection();
}catch(Exception e)
{
System.out.println( "获取连接时出错: "+e.toString());
}
return conn;
}
*/
public Vector getContents(double eachNum,int pageNum,String tabName,String[] fids,String condition,String jspName)//eachNum返回信息数,pageNum第几业,tabName获取信息的表名,fids获取信息列的数组,condition排序的条件,jspName显示分页的JSP文件名
{
Vector contents=new Vector();
try
{
Context initContext=new InitialContext();
Context envContext=(Context)initContext.lookup( "java:/comp/env ");
DataSource ds=(DataSource)envContext.lookup( "jdbc/sample_db ");
Connection conn=ds.getConnection();
Statement stmt2=conn.createStatement();
ResultSet rs2=stmt2.executeQuery( "select count(*) from "+tabName+ " "+condition);
rs2.next();
int totalNum=rs2.getInt(1);
rs2.close();
stmt2.close();
int totalPageNum=1;
if (totalNum/eachNum> (int)(totalNum/eachNum))
{
totalPageNum=(int)(totalNum/eachNum)+1;
}else
{
totalPageNum=(int)(totalNum/eachNum);
}
getBeforeNext(totalNum,pageNum,totalPageNum,jspName);
//System.out.print( "总共有 "+totalPageNum+ "页 "+ " <BR> ");
//开始取数据
int start_id;
int end_id;
String sql= "select ";
for (int m=0;m <fids.length-1;m++ )
{
sql=sql+fids[m]+ ", ";
}
sql=sql+fids[fids.length-1];
sql=sql+ " from "+tabName+ " "+condition;
//System.out.println( "sql: "+sql);
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sql);
start_id=(int)((pageNum-1)*eachNum)+1;
//System.out.println( "start_id: "+start_id);
for (int i=0;i <start_id ;i++ )
{
rs.next();
}
for (int i=0;i <eachNum;i++ )
{
String[] content=new String[fids.length];
for (int f=0;f <fids.length ;f++ )
{
content[f]=rs.getString(f+1);
}
/*for (int n=0;n <fids.length ;n++ )
{
System.out.println( "content: "+content[n]);
}*/
contents.add(content);
if ( !rs.next()) break;
}
rs.close();
stmt.close();
conn.close();
}catch(Exception e)
{
System.out.println( "从数据库中取值时出错: "+e.toString());
}
return contents;
}
public void getBeforeNext(int totalNum,int pageNum,int totalPageNum,String jspName)
{
if (pageNum==1 && totalPageNum==1)
{
before_next=new String[1];
before_next[0]= "共有信息 "+totalNum+ "条 ";
return;
}
if (pageNum==1 && totalPageNum> 1)
{
before_next=new String[3];
before_next[0]= "共有信息 "+totalNum+ "条 ";
before_next[1]= " <a href= ' "+jspName+ "?pageNum= "+(pageNum+1)+ " '> 下一页 </a> ";
before_next[2]= " <a href= ' "+jspName+ "?pageNum= "+totalPageNum+ " '> 尾页 </a> ";
return;
}
if (pageNum !=1 && pageNum==totalPageNum)
{
before_next=new String[3];
before_next[0]= "共有信息 "+totalNum+ "条 ";
before_next[1]= " <a href= ' "+jspName+ " '> 首页 </a> ";
before_next[2]= " <a href= ' "+jspName+ "?pageNum= "+(pageNum-1)+ " '> 上一页 </a> ";
return;
}
if (pageNum !=1 && pageNum <totalPageNum)
{
before_next=new String[5];
before_next[0]= "共有信息 "+totalNum+ "条 ";
before_next[1]= " <a href= ' "+jspName+ " '> 首页 </a> ";
before_next[2]= " <a href= ' "+jspName+ "?pageNum= "+(pageNum-1)+ " '> 上一页 </a> ";
before_next[3]= " <a href= ' "+jspName+ "?pageNum= "+(pageNum+1)+ " '> 下一页 </a> ";
before_next[4]= " <a href= ' "+jspName+ "?pageNum= "+totalPageNum+ " '> 尾页 </a> ";
return;
}
}
}
自己研究怎么用吧
[解决办法]
给你一份简单的例子,自己可以根据需要看看,不过不是很规范的,只能满足你的分页要求,这是最简单的,假如给好的给你,要贴好多出来.这不太方便:数据是msyql的,自己按类去建一个表就可以了.
//jsp页面
<%@ page contentType= "text/html; charset=GBK " %>
<%@ page import= "com.itfuture.workflow.admin.FenPage "%>
<%@ page import= "java.sql.* "%>
<%@ page import= "java.util.Vector "%>
<html>
<head>
<title>
employee
</title>
</head>
<body bgcolor= "#ffffff ">
<h1>
<p align=center> 部门信息管理 </p>
</h1>
<%
int curpage=1; //当前页
int allpage=0; //总页数
int allrecord=0; //总记录数
int page_record=10; //分页单位
int pages=0; //用户请求的页面
String countSQL= "select count(*) from dept ";
if(request.getParameter( "page ")!=null){
pages=Integer.parseInt(request.getParameter( "page "));
}
String sql= "select deptid,deptname,deptnum from dept order by deptid ";
FenPage fenpage=new FenPage(page_record,pages,countSQL,sql);
Vector vector=fenpage.FenYe();
allrecord=Integer.parseInt(vector.get(0).toString());
allpage=Integer.parseInt(vector.get(1).toString());
curpage=Integer.parseInt(vector.get(2).toString());
ResultSet rs=(ResultSet)vector.get(3);
%>
<table border=1 width=600 align=center>
<tr> <td> 部门编号 </td> <td> 部门名称 </td> <td> 部门人数 </td> <td> 操作 </td> </tr>
<%while(rs.next()){
int deptid=rs.getInt(1);
%>
<tr> <td> <%=deptid%> </td> <td> <%=rs.getString(2)%> </td>
<td> <%=rs.getInt(3)%>
</td>
<td> <a href=./updatedept.jsp?type=update&deptid= <%=deptid%> > 修改 </a>
<a href=./updatedept.jsp?type=view&deptid= <%=deptid%> > 查看 </a>
<a href=./updatedept.jsp?type=delete&deptid= <%=deptid%> onclick= "return confirm( '确实要删除吗! '); "> 删除 </a>
</td>
</tr>
<%
}
%>
</table>
<p align=center>
<a href=./dept.jsp?page=1> 首页 </a>
<a href=./dept.jsp?page= <%=curpage-1%> > 上一页 </a>
<a href=./dept.jsp?page= <%=curpage+1%> > 下一页 </a>
<a href=./dept.jsp?page= <%=allpage%> > 尾页 </a>
共 <%=allpage%> 页数 第 <%=curpage%> 页 共 <%=allrecord%> 条记录 每页显示 <%=page_record%> 条
</p>
<p align=right> <a href=./newdept.jsp> 新增 </a> </p>
</body>
</html>
-----------
下序