简体   繁体   中英

How to fetch database(oracle) table one by one means first it show first row then on the submit it will show the next row and so on

Below is my code but it select only 1st row again and again ... <%

    Class.forName("oracle.jdbc.driver.OracleDriver");
       Connection c=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");

    String ss=(String)session.getAttribute("ssss");
    String sss=(String)session.getAttribute("sss");
    PreparedStatement P=c.prepareStatement("Select * from QUESTION where EXAMNAME=?");
    P.setString(1,sss);

    ResultSet rs=P.executeQuery();
  if(rs.next())
   { %>
   <form action="newjsp11.jsp" method="post">
       Welcome <%=ss%><br>
       <input type="text" name="abcd" value="<%= rs.getString(1)%>"><br>
       <input type="radio" name="abc" value="<%= rs.getString(2)%>"><%= rs.getString(2)%>
       <input type="radio" name="abc" value="<%= rs.getString(3)%>"><%= rs.getString(3)%>
       <input type="radio" name="abc" value="<%= rs.getString(4)%>"><%= rs.getString(4)%>
       <input type="radio" name="abc" value="<%= rs.getString(5)%>"><%= rs.getString(5)%><br>
       <input type="submit" name="Submit"><br><br>
   </form>

      <% }
    %>  

Here String ss is Student name and sss is examname ...

I dont't have java on my computer right now, but this link might be helpful for you. I'm guessing your problem is that you don't iterate over result of statement.

https://docs.oracle.com/javase/tutorial/jdbc/basics/processingsqlstatements.html

So your code should look more-less (if your code works ofc) like this:

    Class.forName("oracle.jdbc.driver.OracleDriver");
       Connection c=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");

    String ss=(String)session.getAttribute("ssss");
    String sss=(String)session.getAttribute("sss");
    PreparedStatement P=c.prepareStatement("Select * from QUESTION where EXAMNAME=?");
    P.setString(1,sss);
    ResultSet rs=P.executeQuery();
while (rs.next()) {

    %>
   <form action="newjsp11.jsp" method="post">
       Welcome <%=ss%><br>
       <input type="text" name="abcd" value="<%= rs.getString(1)%>"><br>
       <input type="radio" name="abc" value="<%= rs.getString(2)%>"><%= rs.getString(2)%>
       <input type="radio" name="abc" value="<%= rs.getString(3)%>"><%= rs.getString(3)%>
       <input type="radio" name="abc" value="<%= rs.getString(4)%>"><%= rs.getString(4)%>
       <input type="radio" name="abc" value="<%= rs.getString(5)%>"><%= rs.getString(5)%><br>
       <input type="submit" name="Submit"><br><br>
   </form>

      <% }
    %> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM