• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

using database values to populate drop down menus

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to have two drop down menus where the first one is automatically populated upon visiting the page from a database column. When the user selects a value from that first drop down menu I want the second drop down menu to be populated depending from the database using that value.

I've heard there are ways to do this with ajax, php and javabeans but I also heard that the easiest way to do it is with just jsp variables(maybe session?) so when one value is picked in the first drop down menu the page is refreshed and that value is passed in as a parameter and the second drop down menu takes that value and updates the second drop down menu.

I am able to retrieve the values from the database using a ResultSet but from there I'm having a tough time figuring out how to populate the first drop down list and I'm also not sure how to reload the page with the selected value used as a parameter. Will I be using jsp to write HTML out to make the drop down menus?

Anyways any help would be greatly appreciated. Thanks in advance.
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer to your last question is yes.

To answer the general question: Firstly, result set and JSP should never be used in the same sentence. Write model classes that do all the database access completely separated from any UI aspects. The data should be copied from the result set into Java collections/beans as appropriate, and the results sets closed as soon as possible.

Basically, the UI should know nothing about the database, and vice versa.

Controlling the action of the model class(es) is the job of the page controller. Once the controller gets the collections, is can place them onto the request as scoped variables and forward to the JSP page where JSTL/EL can take over the task of creating the HTML for the dropdowns.
 
clay jay
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the quick response Bear. Yeah I'm not very familiar with standards such as having all database work separate from the UI although it does make sense as I guess I should always think of MVC. I've been using Dreamweaver and each page I produce is just one giant JSP, they don't separate anything, there is also no functions. All Dreamweaver produces is a bunch of java followed by some HTML.
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Throw DreamWeaver away and use a real Java IDE. For example Eclipse or IntelliJ IDEA.

Develop code yourself. Do not drag'n'drop code before you actually understands what all that autogenerated code does and you can write it yourself without help of a visual editor.
 
clay jay
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do use Eclipse and it is my favorite IDE. Just a few weeks ago I wrote my first servlet. I'm working with someone else though who has done most of this websites design using Dreamweaver and the stuff that he is unable to do I've been going into the code and fixing, this just happens to be one of the problems we've run into that Dreamweaver can't fix itself.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fine. It was just a bad start. Hope you know better for the next time.

For the rest I don't have anything to add what Bear already mentioned. Separate model from view. Learn about the DAO pattern.
 
This tiny ad is wafer thin:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic