JSP实现数据库分页显示源代码例
2018-11-25
分页显示数据库信息用户名 | 性别 | 电话 |
try
{
//驱动程序名
String driverName="sun.jdbc.odbc.JdbcOdbcDriver";
//连接字符串
String url="jdbc:odbc:userdata";
//加载驱动程序
Class.forName(driverName).newInstance();
Connection conn=DriverManager.getConnection(url);
//DriverManager.getConnection(ConnStr)
//创建执行语句
String sql="select * from user";
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs=stmt.executeQuery(sql);
int intPageSize; //一页显示的记录数
int intRowCount; //记录的总数
int intPageCount; //总页数
int intPage; //待显示的页码
String strPage;
int i;
//设置一页显示的记录数
intPageSize=5;
//取得待显示的页码
strPage=request.getParameter("page");
//判断strPage是否等于null,如果是,显示第一页数据
if(strPage==null)
{
intPage=1;
}else{
//将字符串转换为整型
intPage=java.lang.Integer.parseInt(strPage);
}
if(intPage<1)
{
intPage=1;
}
//获取记录总数
rs.last();
intRowCount=rs.getRow();
//计算机总页数
intPageCount=(intRowCount+intPageSize-1)/intPageSize;
//调整待显示的页码
if(intPage>intPageCount) intPage=intPageCount;
if(intPageCount>0)
{
//将记录指针定位到待显示页的第一条记录上
rs.absolute((intPage-1)*intPageSize+1);
}
//下面用于显示数据
i=0;
while(i
{
%>
rs.next();
i++;
}
//关闭连接、释放资源
rs.close();
stmt.close();
conn.close();
%>
共个记录,分页显示,当前页是:第页
for(int j=1;j<=intPageCount;j++)
{
out.print(" "+j+"");
}
%>
}
catch(Exception e)
{
e.printStackTrace();
}
%>
免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。
http://www.pinlue.com/style/images/nopic.gif