0

How do you display a drop down box with a resultset in a loop? I just get a sql exception with my current servlet code.

any help appreciated

Thanks

out.println("Select Reading List");
out.println("<select name=" + rs1.getString("degree")+ ">");
while(rs1.next()){
out.println("<option value=" + rs1.getString("idcol") + ">" + rs1.getString("degree") + "</option>");
}
out.println("</select>");
2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by ~s.o.s~
0

Initially the ResultSet cursor is positioned before the first record. You compulsorily need to do a next() before any call to getXXX() so that the cursor is properly positioned. It's actually a good idea to read the documentation of the function you are using in case of any exception / strange behavior.

if(rs1.next()) {
    out.println("<select name=" + rs1.getString("degree")+ ">");
}
while(rs1.next()) {
    rs1.getString("degree") + "</option>");
}

But it's a bad practice to use scriptlets let alone accessing the database in a JSP file, as they make your code difficult to maintain, messy and buggy.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.