日期:2014-05-20  浏览次数:20805 次

oracle分页
麻烦大家给我看看,我这报错误说end标识符无效,我弄了半天不明白,求大神指点!!本人是个新手!!谢谢!

<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <base href="<%=basePath%>">
   
  <title>My JSP 'wel.jsp' starting page</title>
   
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">  
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

  </head>
  
  <body>
  登录成功!恭喜你!<%=request.getParameter("user") %> <br>
  <a href="login.jsp">返回重新登录</a>
  <hr>
  <h1>用户信息列表</h1>
  <% 
  //定义四个变量
  int pageSize=3;
  int pageNow=1;//默认显示第一页
  int rowCount=0;
  int pageCount=0;
  int end=pageSize*pageNow;
  int begin = (pageNow-1)*pageSize+1;
 
  //查询得到rowCount
  //1.加载驱动
  Class.forName("oracle.jdbc.driver.OracleDriver");
  //2.得到链接
  Connection ct=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:oracl","test","test");
  //3.创建Statement
  Statement sm = ct.createStatement();
  //4.查询
  ResultSet rs = sm.executeQuery("select count(*) from pzusers");
  //请注意,一定要rs.next();
  if(rs.next()){
  rowCount=rs.getInt(1);
  }
  //计算pageCount
  if(rowCount%pageSize==0){
  pageCount=rowCount/pageSize;
  }else{
  pageCount=rowCount/pageSize+1;
  }
  //查询出需要显示的记录
  rs = sm.executeQuery(" select * from (select a.* ,rownum rn from (select * from pzusers) a where rownum<=end)where rn >=begin");
   
  //显示
   
  %>
 
  <table border="1">
  <tr><td>用户id</td><td>用户名字</td><td>密码</td><td>邮件</td><td>级别</td></tr>
 
  <%
  while(rs.next()){
  %>
  <tr><td><%=rs.getInt(1)%></td>
  <td><%=rs.getString(2)%></td>
  <td><%=rs.getString(3)%></td>
  <td><%=rs.getString(4)%></td>
  <td><%=rs.getInt(5)%></td></tr>
  <%
  }
  %>
</table>
<%
%>
  </body>
</html>


------解决方案--------------------
当然end无效啦,
你如果这样写sql,那么你就用 PreparedStatement 然后再sql中设置 ?

 select * from (select a.* ,rownum rn from (select * from pzusers) a where rownum<=?)where rn >=?