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>");

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.