• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
m avalla
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
m avalla
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
<%
}
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use the "equals" method of the string class and not "==" operator
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many Thanks!!! Works perfectly.
reply
    Bookmark Topic Watch Topic
  • New Topic