permaculture playing cards*
The moose likes JSP and the fly likes Selecting a value in a combo box that equals the value from the database record? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Selecting a value in a combo box that equals the value from the database record?" Watch "Selecting a value in a combo box that equals the value from the database record?" New topic
Author

Selecting a value in a combo box that equals the value from the database record?

m avalla
Greenhorn

Joined: Oct 02, 2002
Posts: 10
I am very new to JSP. I have a combo box that is being populated with values from the database using a resultset and works fine. Now I want to dynamically select the value in the combo box that equals the value from the database record while I am populating the combo box.
Currently the code populating the combo box is as follows:
<select name="class">
<%
while rs.next
{
%>
<option value="<%=rs.getString(1)%>"
<%
}
%>
</select>
Thanks!
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
If I understand what you want to do, something like this should do it for you:

Just decide what criteria you need to use to decide which record is th correct one and then put that comparison in the if statement.


Dave
m avalla
Greenhorn

Joined: Oct 02, 2002
Posts: 10
Thanks Dave. I tried that and keep getting the error message that the "else" does not have a matching "if". By the way, I'm using Oracle JDeveloper 9i.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60780
    
  65

Always use the braces around blocks in a JSP page. As in:

On the other hand, I'd write something along the lines of (ignoring that fact that I'd never expose a result set to a view page):

hth,
bear
[ October 02, 2002: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
m avalla
Greenhorn

Joined: Oct 02, 2002
Posts: 10
Thanks for the replies. I got the brackets right and it is working...sort of. Is there some trick when comparing a String variable to a literal? I'm comparing the string variable which has the value from the database to a literal and it never matches (falls into the if). I know the values are the same because I can see the them in the debugger. Following is the test code:
while (rs.next())
{
if (returnValue == "SEC")
{
stringValue="IN IF";
%>
<option selected value="IN IF"></option>
<%
}
else
stringValue="IN ELSE";
%>
<option value="IN ELSE "></option>
<%
}
Joshi Thomas
Greenhorn

Joined: Oct 03, 2002
Posts: 7
Please use the "equals" method of the string class and not "==" operator


SCJP<br />SCWCD
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60780
    
  65

Joshi is correct. In Java, the == operator is a test for identity -- that is, are the objects, rather than their values, the same. The .equals() method tests for value equality.
hth,
bear
m avalla
Greenhorn

Joined: Oct 02, 2002
Posts: 10
Many Thanks!!! Works perfectly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Selecting a value in a combo box that equals the value from the database record?
 
Similar Threads
Struts Html select tag
populate combox with list with default selected value
Using Combo Box in JSP
Displaying record from combo box
urgent...select ....option... doubt