cheng719693624 2015-10-16 01:27 采纳率: 0%
浏览 2324

jsp分页问题(oracle数据库),只能打印出第一页内容,点击下一页就没有内容显示,

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="java.sql.*" %>
<%@ page import="org.zshwd.vo.User" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">



<%
int allCount = 0; //共多少条记录
int pageSize = 2; //每页多少条
int pageCount = 1; //共分多少页
int currentPage = 1; //当前页
%>
<%
try{
//当前页 采用地址栏传值方式
currentPage = Integer.valueOf(request.getParameter("page"));

}
catch(Exception e){
//e.printStackTrace();
}
//out.println(currentPage);
%>
<%!
private static final String DBDRIVER = "oracle.jdbc.OracleDriver";
private static final String DBURL = "jdbc:oracle:thin:@localhost:1521:orcl";
private static final String DBUSER = "scott";
private static final String DBPASSWORD = "tiger";
    private Connection conn = null;
%>

<%
    try{
        Class.forName(DBDRIVER);
        conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
    }
    catch (Exception e) {
        e.printStackTrace();
    }
%>
<%
    //count 数量
    String strSQL = "Select Count(*) As Num From Users";
    PreparedStatement pstmt;
    pstmt = conn.prepareStatement(strSQL);
    ResultSet rs = pstmt.executeQuery();
    if(rs.next()){
        allCount = rs.getInt(1);
    }
%>
<%
    int tempNum = 0;
    //取余数
    tempNum = allCount % pageSize;
    //有余数
    if(tempNum > 0){
        pageCount = allCount / pageSize + 1;
    }
    else{
        pageCount = allCount / pageSize;
    }
%>
<%
    //当前页
    if(currentPage <=0 ){
        currentPage = 1;
    }
    //大于总页数
    if(currentPage > pageCount){
        currentPage = pageCount;
    }
%>
<%
    /* strSQL = "Select UserId,UserName From Users limit " + (currentPage - 1) * pageSize + "," + pageSize; */
   strSQL = "select UserId,UserName from ( select t.*,rownum from Users t) where rownum>"+(currentPage-1)*pageSize+" and rownum<="+(currentPage)*pageSize; 
   /*  strSQL = "select UserId,UserName from ( select t.*,rownum from Users t) where rownum>0 and rownum<=2"; */
    List<User> listUser = new ArrayList<User>();

    // 准备sql
    pstmt = conn.prepareStatement(strSQL);  
    rs = pstmt.executeQuery();
    try {
        while (rs.next()) {
            User user = new User();
            user.setUserId(rs.getInt(1));
            user.setUserName(rs.getString(2));
            listUser.add(user);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
%>
<table>
    <tr>
        <td>主键Id</td>
        <td>用户名</td>
        <td>操作</td>
    </tr>
    <%
        Iterator iter = listUser.iterator();
        while(iter.hasNext())
        {
            User user = (User)iter.next();
    %>
    <tr>
        <td><%=user.getUserId() %></td>
        <td><%=user.getUserName() %></td>
        <td>修改   删除</td>
    </tr>
    <%
        }
    %>
</table>
<table>
    <tr><td align="center">共<font color="#f00"><%=allCount%></font>条 
    每页<font color="#f00"><%=pageSize%></font>条 
    分<font color="#f00"><%=pageCount%></font>页  
    当前<font color="#f00"><%=currentPage%></font>页  
    <%
        //当前页大于1
        if(currentPage > 1){
    %>
        <a href="?page=1">首页</a>  <a href="?page=<%=currentPage-1%>">上一页</a>
    <%      
        }
        else{
    %>
        首页 上一页 
    <%
        }
    %>
    <%//***************************** %>
    <%
        //当前页小于共分多少页
        if(currentPage < pageCount){
    %>
        <a href="?page=<%=currentPage+1%>" >下一页 </a>  <a href="?page=<%=pageCount%>">尾页</a>
    <%      
        }
        else{
    %>
        下一页 尾页 
    <%
        }
    %>
    <input type="text" name="txtCurrentPage" id="txtCurrentPage" size="3" value="<%=currentPage%>">
    <input type="button" value="GO" onclick="Jump()" /></td></tr>
    <script language="javascript" type="text/javascript">
        function Jump(){
            //通过Id查找表单  值 为空
            if(document.getElementById("txtCurrentPage").value == ""){
                //弹出对话框
                alert("请输入数字");
                //获得焦点
                document.getElementById("txtCurrentPage").focus();
                return false;
            }

            //只能输入正整数  不大于 最大页数
            //判断输入是否是数字
            var pattern=/^[0-9]*[1-9][0-9]*$/;
            var currentPage;
            currentPage = document.getElementById("txtCurrentPage").value;
            //用正则表达式 验证 输入的值   false
            if(!pattern.test(currentPage)){
                alert("请输入正整数");
                document.getElementById("txtCurrentPage").focus();
                return false;
            }
            if(currentPage > <%=pageCount%>){
                alert("请输入1到" + <%=pageCount%> + "之间的正整数");
                document.getElementById("txtCurrentPage").focus();
                return false;
            }

            location.href="?page=" + document.getElementById("txtCurrentPage").value;
        }
    </script>
</table>

<%
    conn.close();
%>


  • 写回答

7条回答 默认 最新

  • cheng719693624 2015-10-16 01:29
    关注

    不知道到底是哪里问题,各路大神看过,小弟现在这拜谢了

    评论

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的