Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using Combo Box in JSP

 
Raghuraman Muthuswamy
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
I am trying to access database records and put all the records in a combo box. I tried the following code. But it fetches only one record. I want the code snippet for getting all the records and populating it in a Combo box. Your help is very much appreciated.

JSP Code:
//I am using Access Data base
<%
Statement s = con.createStatement();
ResultSet re = s.executeQuery("select * from exist");

while (re.next())
{
String un = re.getString(1);
%>
<option value = "<%= re.getString(1)%>"
<%
}
%>
 
Niki Nono
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try the following and let me know what happened.

<%
Statement s = con.createStatement();
ResultSet re = s.executeQuery("select * from exist");
%>
<select name=test>
<%
while (re.next())
{
String un = re.getString(1);
%>
<option value = "<%= un %>">
<%
}
%>
</select>

I think this should work.
 
Bhavin Sanghani
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi raghu,

probably closing <option> tag would be an issue...check code from following link...

http://forum.java.sun.com/thread.jspa?threadID=592834&messageID=3103335

-bhavin
 
Raghuraman Muthuswamy
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Niki,
Its working perfectly after changing the code as per your guidelines. But i cant understand that if you directly put re.getString in your option tag why it is not working. pls. give me the reason why my code didnt work.
Expecting your reply.

regards,
Raghu
 
Heonkoo Lee
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raghuraman,

You only have values, missing text to display.

<select name="test">
<option value="1" selected>One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>

<!-- closing tag is a good practice -->

Regards,
 
Heonkoo Lee
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Raghuraman Muthuswamy:

while (re.next())
{
String un = re.getString(1);
%>
<option value = "<%= re.getString(1)%>" <---
<%
}
%>


If you see the generated html source code, you will probably see something like this:

<option value="some value" // > is missing
<option value="some value"
<option value="some value"
<option value="some value"
<option value="some value"

Regards,
[ March 31, 2005: Message edited by: Heonkoo Lee ]
 
Niki Nono
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raghuraman
It will work even if you put re.getString(1) in the option tag.
I just made it better by replacing it in the option tag by the string var that you have declared.
the main problem here was you had missed the ">" that closes the tag. also as others have pointed out it is a good practice to append the </option> tag to the existing <option> tag.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic