By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,654 Members | 1,517 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,654 IT Pros & Developers. It's quick & easy.

how I can fetch data from mysql database to show in a form's drop down box with jsp

P: 52
there is one registration form where using dropdown box i select one country and it save into mysql database.
now i want to fetch that value of mysql database table into edit profile form where i can edit it.
plz help me with jsp coding ..
best regard
vishal prada
Mar 2 '12 #1
Share this Question
Share on Google+
4 Replies


10K+
P: 13,264
You retrieve the value using your data access classes and then when you build the options for the edit screen you set the selected flag

If you are hard coding country options on the page you would use
Expand|Select|Wrap|Line Numbers
  1. <c:choose>
  2.    <c:when test="${databaseValue == 1}">
  3.         <option value="1" selected> 
  4.           <c:out value="India" />
  5.         </option>
  6.     </c:when>
  7.    <c:otherwise>
  8.        <option value="1"> 
  9.           <c:out value="India" />
  10.         </option> 
  11.    </c:otherwise>
  12. </c:choose>
With hard coding you would do this for every one of your options.

If you took earlier advise and put all those countries into a List then your code is much simpler and shorter, just something like
Expand|Select|Wrap|Line Numbers
  1. <select name="countryId"> 
  2.    <c:forEach var="country" items="${countries}" >
  3.    <c:choose>
  4.    <c:when test="${databaseValue == country.id}">
  5.         <option value="${country.id} selected> 
  6.          <c:out value="${country.name}" />
  7.         </option>
  8.     </c:when>
  9.    <c:otherwise>
  10.       <option value="${country.id}">
  11.           <c:out value="${country.name}" />
  12.         </option> 
  13.    </c:otherwise>
  14.   </c:choose>
  15.    </c:forEach>
  16. </select>
  17.  
  18.  
  19.  
and it would cover all countries.
Mar 2 '12 #2

P: 52
tell me one thing ur given code is in jsp or jsf ?
Mar 2 '12 #3

10K+
P: 13,264
It's JSTL which you should use in JSPs or JSF. It helps keep Java code away from JSPs.
Mar 2 '12 #4

P: 52
can u give me one demo of change password from database with jsp code.
Mar 2 '12 #5

Post your reply

Sign in to post your reply or Sign up for a free account.