This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java and have Dr. Raoul-Gabriel Urma & Richard Warburton on-line!
See this thread for details.
Win a flower (🌹) or copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java (📚) this week in the Agile and Other Processes forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Bear Bibeault
  • Liutauras Vilda
  • Devaka Cooray
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

populating drop down list from database table

 
pioneer
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

I am sorry that i posted this topic previously in wrong forums. please excuse me for that.

I am using jsf.

i have following questions:
1) How can we populate a drop down list from a database table? My drop down list should contain all the section numbers that satisfy a particular condition. I have written a query that would fetch all the correct section numbers. Now I want to add them into a drop down menu. Can someone tell me any way to achieve it?

2) Is there any way by which we can populate a drop down using all the elements in the array? For example:
Let the array be
String s []={"abc","def","xyz"};
How do i get the three string instances into the drop down menu?

3) Any other data structure (HashMap or Hashtable) will also do.


Sample code would help.


Thanks
 
Marshal
Posts: 67357
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please do not cross-post the same question in multiple forums. It wastes people's time when multiple redundant conversations take place. Please read this for more information.
 
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I hope I understood you correctly, unless it has been answered somewhere else,here's how I solved it: so you create the component GUI and bind it to the managed bean that fetches the info from the DB.
In my case I needed a class that fetches several country names, the final result looks like this:

The gui component:
<blockquote>code:
<pre name="code" class="core">
<h:selectManyListbox value="#{CountriesBean.selectedItems}" size="8" styleClass="defaultText">
<f:selectItems value="#{CountriesBean.selectItems}" />
</h:selectManyListbox>
</pre>
</blockquote>
The CountriesBean.java class is declared in faces-config.xml like this:
<blockquote>code:
<pre name="code" class="core">
<managed-bean>
<managed-bean-name>Countries</managed-bean-name>
<managed-bean-class>xlinuks.Countries</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</pre>
</blockquote>
Its source code:
<blockquote>code:
<pre name="code" class="core">
package xlinuks;

import javax.faces.model.*;
import java.util.*;

public class Countries {

List<String> selectedItems; // + getter + setter
List<SelectItem> selectItems; // + getter only

public Countries() {
}


public List<SelectItem> getSelectItems() {

if( selectItems == null ) {
//so it actually calls another class called Connector
//that fetches the data from the DB
selectItems = Connector.getCountries( language );
}

return selectItems;
}

public void setSelectItems( List<SelectItem> selectItems ) {
this.selectItems = selectItems;
}

public List<String> getSelectedItems() {
return selectedItems;
}

public void setSelectedItems( List<String> selectedItems ) {
this.selectedItems = selectedItems;
}
}
</pre>
</blockquote>
These are the main steps, unfortunately I can't post a detailed description.
 
Would anybody like some fudge? I made it an hour ago. And it goes well with a tiny ad ...
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!