?
Solved

populating combo box from databse in jsp

Posted on 2003-02-19
4
Medium Priority
?
227 Views
Last Modified: 2010-08-05
I want to know how to populate key value pairs in combobox from a database in jsp. Actually i am new to java programming so please let me know the code for it.Thanx in advance.
0
Comment
Question by:harpreetmarwaha
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 

Accepted Solution

by:
rajbhaskar earned 200 total points
ID: 7980850
Try something like this:

<%@page import="java.sql.*" %>
<%
// load the database driver
Class.forName("driver.class").newInstance();

// get a connection to the database
Connection con= DriverManager.getConnection("con.string", "username", "password");
Statement stmt= con.createStatement();

// run the query
String query= "SELECT key, value, FROM YourTable";
ResultSet rs= stmt.executeQuery(query);
%>

<!-- create the combo box -->
<select name="mySelectBox">
<% while (rs.next()) { %>
<option value="<%=rs.getString("key")%>"><%=rs.getString("value")%></option>
<% } // end loop %>
</select>

<%
// close connection to database
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (con != null) con.close();
%>

A slightly more elegant way to do it would be to do all teh database stuff in a bean and return a Map which you could use to populate the combo box using something like my own HtmlForms package (http://lordofthemoon.com/computing/downloads.php?file=/computing/downloads/htmlforms.html )

HTH,
Raj.
0
 

Expert Comment

by:CleanupPing
ID: 9059253
harpreetmarwaha:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 9552856
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Accept Rajbhaskar's comment
Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Thomas Boshell
EE Cleanup Volunteer
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month11 days, 5 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question