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个回答

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

">尾页 这种写法貌似有问题,用这个试试

你的超链接这种写法 "?page=<%=currentPage+1%>"貌似不行

可以用这种试试:“当前页?page=<%=currentPage+1%>”

当前页?page=<%=currentPage+1%> 这种方式不行,

description The requested resource is not available.

谢谢

就是用 超链接的相对路径, 当前页是 你这个文件叫 **.jsp?page=<%=currentPage+1%>

用你的方法
地址栏显示
http://localhost:8080/FenYe/index.jsp?page=2

但是没有数据 没有执行sql语句 不知道为啥

解决了 其实就是因为 sql 语句中 没有给 伪列 起别名, 晕~ 就因为这个

不管怎样谢谢你 兄弟~(这还是我第一次发贴)

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐