Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
m avalla
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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!
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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>
<%
}
 
Joshi Thomas
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use the "equals" method of the string class and not "==" operator
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Many Thanks!!! Works perfectly.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic