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?
How do I output the query results to the drop-down list?
And of course close the select :)
<select>
<%
ResultSet rs = statement.executeQuesy(sql );
while (rs.next())
{
String item = rs.getString(1);
%>
<option><%= item %></option>
<%
}
%>
</select>
<select>
<%
ResultSet rs = statement.executeQuesy(sql
while (rs.next())
{
String item = rs.getString(1);
%>
<option><%= item %></option>
<%
}
%>
</select>
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.DB2 Driver");
java.sql.Connection connection = java.sql.DriverManager.get Connection ("JDBC:db2 os390:DB2T SG");
java.sql.Statement statement = connection.createStatement ();
java.sql.ResultSet columns = statement.executeQuery("SE LECT * FROM CBSTDTFI.STATUS");
while(columns.next()) {
String CURDATE = columns.getString("CURDATE ");
String TITLE = columns.getString("TITLE") ;
String TECHNICIAN = columns.getString("TECHNIC IAN"); %>
<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>
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.DB2
java.sql.Connection connection = java.sql.DriverManager.get
java.sql.Statement statement = connection.createStatement
java.sql.ResultSet columns = statement.executeQuery("SE
while(columns.next()) {
String CURDATE = columns.getString("CURDATE
String TITLE = columns.getString("TITLE")
String TECHNICIAN = columns.getString("TECHNIC
<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.
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.
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.
ASKER
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
<select>
<%
ResultSet rs = statement.executeQuesy(sql
while (rs.next())
{
String item = rs.getString(1);
%>
<option><%= item %></option>
<%
}
%>