日期:2014-05-18  浏览次数:20716 次

高分求救~~!!哪位大侠可以给我一份完整的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