• Post Reply Bookmark Topic Watch Topic
  • New Topic

populate drop down menu from database  RSS feed

 
GA Anand
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I need to design a JSP page where i am required to populate the drop down menu from the database. Please let me know how it can be done. Your help would be sincerely appreciated.

Thanks
Gautam
 
Matt Hoffman
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is how I accomplished this:

You can list out the data in a list like so.. This is my bean.

public List getParentalList() throws SQLException {
List parentalList = new ArrayList();

// obtain list of titles
ResultSet results = statement.executeQuery("SELECT * FROM ADVISORY");

// get row data
while (results.next()) {
ParentalBean parental = new ParentalBean();
parental.setParentalID( results.getString( 1 ) );
parental.setParentalName( results.getString( 2 ) );

parentalList.add(parental);
}
return parentalList;
}

In your JSP:

<p>Parental Advisory:
<select name="advisory">
<option value="All" selected>All Advisorys</option>
<%
List parentalList = parentalData.getParentalList();
Iterator parentalListIterator = parentalList.iterator();
while ( parentalListIterator.hasNext() ) {
parental = ( ParentalBean ) parentalListIterator.next();
out.println("<OPTION value=\"" + parental.getParentalID() + "\">" + parental.getParentalName());
out.println("</OPTION>");
}
%>
</select>
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why do people do this? You're already in a JSP page, why build up HTML in Java when template text is some much easier and clearer?

Just one of my pet peeves.
[ June 04, 2004: Message edited by: Bear Bibeault ]
 
Charles Hasegawa
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to agree with Bear (sorry Matt), although your solution may be valid, there are a number of different ways to accomplish the original poster's goal without loading the page up with java scriptlet code.
For example, you could create a custom tag to do the work for you. You can use JSTL tags to get the data and iterate through the list.

I know that doesn't really answer the posters original question, but a JSP full of scriptlet code is a pet peeve of mine too.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well actually, it wasn't the presence of scriplets I was ranting about -- that's only a pet peeve at this point if they're doing things that obviously do not belong in JSPs -- like control code or DB access. I realize that at this point, transitioning to scriptlet-less JSP pages via JSTL and custom actions (which I do highly recommend) is something not everyone is able to do quite yet.

My rant was against using out.println() in the scriplets and building up HTML in Java code when you're already in a JSP where the HTML should be in template text. Makes no sense to me whatsoever.
 
gautam anand
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

Thanks all of you for your valuable help..thanks for providing me the information and suggestions on time..You guys really helped me out in starting on my project. I sincerely appreciate your help.

Thanks
Gautam
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!