首页 > 编程 > JSP > 正文

JSP实现的简单分页显示效果代码

2020-07-27 21:29:30
字体:
来源:转载
供稿:网友

本文实例讲述了JSP实现的简单分页显示效果代码。分享给大家供大家参考,具体如下:

<%@ page contentType="text/html;charset=gb2312" %><%@ page language="java" import="java.sql.*" %><script language="javascript">function newwin(url) {varnewwin=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.ResultSet.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></head><body><form method="POST" action="fenye1.jsp">第<%=intPage%>页 共<%=intPageCount%>页<%if(intPage<intPageCount){%><ahref="fenye1.jsp?page=<%=intPage+1%>">下一页</a><%}%> <%if(intPage>1){%><a href="fenye1.jsp?page=<%=intPage-1%>">上一页</a><%}%>转到第:<input type="text" name="page" size="8"> 页<span><input class=buttonface type='submit' value='GO' name='cndok'></span></form><table border="1" cellspacing="0" cellpadding="0"><tr><th>用户名</th><th width='8%'>删除</th></tr><%if(intPageCount>0){//将记录指针定位到待显示页的第一条记录上sqlRst.absolute((intPage-1) * intPageSize + 1);//显示数据i = 0;String user_id,user_name;while(i<intPageSize && !sqlRst.isAfterLast()){user_id=sqlRst.getString(1);user_name=sqlRst.getString(2);%><tr><td><%=user_id%></td><td><%=user_name%></td><td width='8%' align='center'><a href="delete.jsp?user_id=<%=user_id%>"onClick="return newwin(this.href);">删除</a></td></tr><%sqlRst.next();i++;}}%></table></body></html><%//关闭结果集sqlRst.close();//关闭SQL语句对象sqlStmt.close();//关闭数据库sqlCon.close();%>

希望本文所述对大家JSP程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表