user2479973
user2479973

Reputation: 11

Loop through resultset

UPDATE: I now need to get the string in a row form rather than in a column form.

How do you loop through a ResultSet (rs) using a for loop or a while loop in order to make the

<%
  <%=rs.getString(n)%>
%>

dynamically loop through all of the records in an SQL table?

This is possible with the following code but is "hard coded" and not dynamically created

<%
while(rs.next()){
%>

<%=rs.getString(1)%>

<%
}
%>

Thanks for your help.

Upvotes: 1

Views: 129

Answers (1)

kamoor
kamoor

Reputation: 2949

You can get total number of columns from MetaData and loop through it.

int numOfCols = rs.getMetaData().getColumnCount();
while(rs.next()){
   for(int i=1;i<=numOfCols;i++){
        rs.getString(i); // Anything except blob can be retrieved as getString()
   }
}

Upvotes: 1

Related Questions