通过servlet获取第2行,第3行...... oracle中的200行表

时间:2015-02-09 14:21:06

标签: java sql oracle jsp servlets

我在oracle中有一个名为abc的表,有4条记录如下:

   F         S        
  apple    mango 
  tiger    lion
  oak      banana
  pink     orange

针对home.jsp

 <form action="NewServlet" method="post">
 <input type="text" name="count"  value="first" style="display:none;">
 <input type="submit" value="NEXT">
 </form>

NewServlet.java

if(request.getParameter("count").equals("first")){// when i clicked on next button having value first

    rs=st.executeQuery("SELECT * FROM abc ORDER BY 'F','S' FETCH FIRST 1 ROWS ONLY"); 
    }// fetches apple and mango
    else{
rs=st.executeQuery("SELECT * FROM abc ORDER BY 'F','S' OFFSET 1 ROWS FETCH NEXT 1 ROWS ONLY");// when i clicked on next button having value second (in display.jsp)
        }// fetches tiger and lion

request.getRequestDispatcher("display.jsp").forward(request, response);

display.jsp

<form action="NewServlet" method="post">
<input type="text" name="count"  value="second" style="display:none;">
<input type="submit" value="NEXT">
</form>

 String abc="", abc1=""
 abc= (String) request.getAttribute("fi");// prints apple 1st time and tiger 2nd time but 3rd and 4th record is not fetched
 abc1= (String) request.getAttribute("fi1");//prints mango 1st time and lion 2nd time but 3rd and 4th record is not fetched

当我一直点击home.jspdisplay.jsp中的NEXT按钮时,如何获取第3和第4条记录,依此类推至第100行?

1 个答案:

答案 0 :(得分:2)

您可以使用count变量作为请求属性传递。 例如:

NewServlet.java

String count = request.getParameter("count");
int c = 0;
String query;
if(count != null && count.matches("\\d+")){
    c = Integer.parseInt(count);
    query = "SELECT * FROM abc ORDER BY 'F','S' OFFSET "+String.valueOf(c)+" ROWS FETCH NEXT 1 ROWS ONLY";
}else{
   query = "SELECT * FROM abc ORDER BY 'F','S' FETCH FIRST 1 ROWS ONLY";
}
request.setAttribute("count",++c);

display.jsp

<input type="text" name="count"  value="<%=(Integer)request.getAttribute("count")%>" style="display:none;">

针对home.jsp

 <form action="NewServlet" method="post">
 <input type="submit" value="NEXT">
 </form>

count移除home.jsp输入,以便您第一次调用servlet count参数时为空。