Link to home
Start Free TrialLog in
Avatar of cgttsp01
cgttsp01

asked on

How to build dynamic 'select' drop-down list?

How in JSP can I make a select drop-down list dynamic?  That is, I want my select drop-down populated by a query.
How do I output the query results to the drop-down list?
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Something like:

<select>
<%
ResultSet rs = statement.executeQuesy(sql);
while (rs.next())
{
   String item = rs.getString(1);
%>
   <option><%= item %></option>
<%
}
%>
And of course close the select :)

<select>
<%
ResultSet rs = statement.executeQuesy(sql);
while (rs.next())
{
  String item = rs.getString(1);
%>
  <option><%= item %></option>
<%
}
%>
</select>
Avatar of cgttsp01
cgttsp01

ASKER

What am I doing wrong here?  The 'select' drop-down populates with the same date in each option of the select box.  It looks like it creates the same date for every record in the DB2 table.

Here is my code.  Any suggestions on how I should fix it?

\********************************************************\
<TABLE BORDER=1 CELLPADDING=0 CELLSPACING=0>
<TR> <TD> CURDATE  </TD>
     <TD> TITLE </TD>
     <TD> TECHNICIAN </TD>
</TR>
<%
  Class.forName("ibm.sql.DB2Driver");
  java.sql.Connection connection = java.sql.DriverManager.getConnection("JDBC:db2os390:DB2TSG");
  java.sql.Statement statement = connection.createStatement();
  java.sql.ResultSet columns = statement.executeQuery("SELECT * FROM CBSTDTFI.STATUS");
  while(columns.next()) {
    String CURDATE  = columns.getString("CURDATE");
    String TITLE = columns.getString("TITLE");
    String TECHNICIAN = columns.getString("TECHNICIAN");     %>
      <TR>  <TD> <%= CURDATE  %> </TD>
            <TD> <%= TITLE %> </TD>
            <TD> <%= TECHNICIAN %> </TD>
      </TR>


<FORM ACTION="search.cfm" METHOD="POST" target="main">
<tr><td colspan="3"><select>
<%

while (CURDATE.next())
{

%>
 <option><%= CURDATE %></option>
<%
}
%>
</select>

<% } %></td></tr>
<INPUT TYPE="Submit" NAME="" VALUE="go">
</form>
</TABLE>
Surprised that even ran due to this statement??

while (CURDATE.next())

as CURDATE is a String.
Your problem is you are creating a select list for *every* row in the result set.
If you need to display the row details in both a table and a select list then you'll need to save the required data from the row in some Collection for later use.
I don't need to display the row details.
Just the select drop-down.

I'm walking my way through trying to convert an existing web program into JSP, and as you probably picked up I'm new to JSP.

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial