This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Servlets and the fly likes dynamic drop down boxes Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "dynamic drop down boxes" Watch "dynamic drop down boxes" New topic
Author

dynamic drop down boxes

liliya woland
Ranch Hand

Joined: Apr 11, 2006
Posts: 134
Hello everyone,
I have a rather long list of select options, and I also have a mysql table that contains that very same data. I'd like to be able to streamline the data from the table into my select menu.
I know enough of .jsp (and some java) to read data from a table but don't know enough to do something like this. I know that I need to write a bean, but that's about all. Could anyone please refer me to any code examples so I could see what needs to happen? Thanks in advance.
vjy chin
Ranch Hand

Joined: Feb 17, 2005
Posts: 279
Hi,
One simple way is to connect to the database and get the values and store them in an array or arraylist.
The inside the select box, you just need to iterate the array or arraylist.



Hope this helps
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56204
    
  13

Though preferably with the JSTL rather than scriptlets.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
liliya woland
Ranch Hand

Joined: Apr 11, 2006
Posts: 134
Hi again,
I did try the code above and for some reason my options get populated with "vOptions.get(i)" instead of data.
I understand the problem has to do with my syntax, but could someone tell me please what syntax to use to get the array data out?
....
Vector vOptions = new Vector();
while(rs.next()){

vOptions.addElement(rs.getString(1));
}
.... then:

<select name="T50">
<%
for (int i = 0; i< vOptions.size(); i++) {
%>
<option value="<%=vOptions.get(i)%>">vOptions.get(i)</option>
<%
}
%>
</select>
liliya woland
Ranch Hand

Joined: Apr 11, 2006
Posts: 134
I got it! ... Just had to do <%=vOptions.get(i)%>
vjy chin
Ranch Hand

Joined: Feb 17, 2005
Posts: 279
OOPS, Sorry I forgot the <%= %> .
Good you noticed it and got the results.

Thanks
mamidi subrahmanyam
Greenhorn

Joined: Jun 29, 2005
Posts: 18
Hi liliya

Better use ArrayList than Vector.

(if there is no problem with performance issues, then you can continue with Vector also)
[ April 13, 2006: Message edited by: mamidi subrahmanyam ]
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: dynamic drop down boxes
 
Similar Threads
SQL Query
limiting the size of a resultset - Breaking up large resultset into chunks
oracle table caching in java Map
Is there a way to perform SQL queries on local data?
SQL query