File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Using Combo Box in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Using Combo Box in JSP" Watch "Using Combo Box in JSP" New topic
Author

Using Combo Box in JSP

Raghuraman Muthuswamy
Ranch Hand

Joined: Mar 18, 2003
Posts: 73
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

Joined: Mar 20, 2005
Posts: 256
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.


Life called,so here I am.<br />Cheers<br />Niki.:-)
Bhavin Sanghani
Ranch Hand

Joined: Dec 17, 2003
Posts: 67
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

Joined: Mar 18, 2003
Posts: 73
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

Joined: Feb 10, 2005
Posts: 85
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

Joined: Feb 10, 2005
Posts: 85
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

Joined: Mar 20, 2005
Posts: 256
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using Combo Box in JSP