日期:2014-05-16  浏览次数:20417 次

经典的jsp分页显示
Java代码

< %@ page contentType="text/html;charset=gb2312" % >  
 
< %@ page language="java" import="java.sql.*" % >  
 
 
< script language="javascript" >  
 
function newwin(url) {  
 
var  
 
 
newwin=window.open(url,"newwin","toolbar=no,location=no,directories=no,status=no,  
 
 
menubar=no,scrollbars=yes,resizable=yes,width=600,height=450");  
 
newwin.focus();  
 
return false;  
 
}  
 
< /script >  
 
< script LANGUAGE="javascript" >  
 
function submit10()  
 
{  
 
self.location.replace("fenye1.jsp")  
 
}  
 
< /script >  
 
< %//变量声明  
 
java.sql.Connection sqlCon; //数据库连接对象  
 
java.sql.Statement sqlStmt; //SQL语句对象  
 
java.sql.ResultSet sqlRst; //结果集对象  
 
java.lang.String strCon; //数据库连接字符串  
 
java.lang.String strSQL; //SQL语句  
 
int intPageSize; //一页显示的记录数  
 
int intRowCount; //记录总数  
 
int intPageCount; //总页数  
 
int intPage; //待显示页码  
 
java.lang.String strPage;  
 
int i;  
 
//设置一页显示的记录数  
 
intPageSize = 4;  
 
//取得待显示页码  
 
strPage = request.getParameter("page");  
 
if(strPage==null){//表明在QueryString中没有page这一个参数,此时显示第一页数据  
 
intPage = 1;  
 
}  
 
else{//将字符串转换成整型  
 
intPage = java.lang.Integer.parseInt(strPage);  
 
if(intPage< 1) intPage = 1;  
 
}  
 
//装载JDBC驱动程序  
 
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
 
//设置数据库连接字符串  
 
strCon = "jdbc:odbc:heyang";  
 
//连接数据库  
 
sqlCon = java.sql.DriverManager.getConnection(strCon,"sa","");  
 
//创建一个可以滚动的只读的SQL语句对象  
 
sqlStmt =  
 
 
sqlCon.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.Result  
 
 
Set.CONCUR_READ_ONLY);//准备SQL语句  
 
strSQL = "select user_id,user_name from userinfo order by user_id desc";  
 
//执行SQL语句并获取结果集  
 
sqlRst = sqlStmt.executeQuery(strSQL);  
 
//获取记录总数  
 
sqlRst.last();//??光标在最后一行  
 
intRowCount = sqlRst.getRow();//获得当前行号  
 
//记算总页数  
 
intPageCount = (intRowCount+intPageSize-1) / intPageSize;  
 
//调整待显示的页码  
 
if(intPage >intPageCount) intPage = intPageCount;  
 
% >  
 
< html >  
 
< head >  
 
< meta http-equiv="Content-Type" content="text/html; charset=gb2312" >  
 
< title >会员管理< /title >