Search code examples
htmljspjdbcscriptlet

For loop over ResultSet#getFetchSize() in JSP doesn't produce any HTML


I have the following code:

 <%
            for (int i = 0; i < rs.getFetchSize(); i++) {
                System.out.print("test");
                //blah
%>
        <div id="Test<%= out.print(i) %>">
            <div class="<%= oddOrEven(i)%>Header">
                <div class="<%= oddOrEven(i)%>A">Test<% out.print(i);%></div>
                <div class="<%= oddOrEven(i)%>B"> 
                //Stuff here

Odd or Even simply responds with the word odd or even based on the number i passed to it, that should make my css style alternate between grey colors.

When I compile the page it works, but this part is totally omitted and doesn't appear in the source. Can someone help me get this working?


Solution

  • The ResultSet#getFetchSize() doesn't return the amount of returned records as you seemed to expect. It just returns the configured fetch size. It may for instance just return 0 depending on the JDBC driver configuration and semantics.

    Just use ResultSet#next() the usual way to move the cursor to the next row.

    for (int i = 0; rs.next(); i++) {
    

    That said, writing Java code in a JSP file is officially discouraged since a decade. I'd suggest to work on that part as well.