Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Jsp Option Value 
 

In this section, we are going to create a select box by retrieving the value from the database in jsp.

 

Jsp Option Value

                          

In this section, we are going to create a select box by retrieving the value from the database in jsp. For further processing, first of all you need to create a table in the database. 

We create a table 'countries' that has the specified fieldnames and datatypes respectively.

 

 

 

 

Create Table 'countries':

CREATE TABLE countries 
(countries_id varchar(10) NOT NULL,
  countries_name varchar(100) default       NULL);

Then we have used the html tag <select>. Its syntax is:

<select name="">
<option value="">--select--</option>
<option value="">--select--</option>
- - - - - - --  - - - - - -- - - - -- - - -- 
- - - - - - - -- - - - -- - - --  -- - -- -- 
</select>

Do the database connectivity and use the query "select * from countries" in order to retrieve the values from the table 'countries'. Then we have create a variable 'name' of string type and using the ResultSet method getString(2), all the values are retrieved from the database and stored into this variable. Then put this variable inside the <option> tag like: <option value="<%= name %>">

Here is the code of optionValue.jsp:

<%@page import="java.sql.*"%>
<html>
<form name="form" method="post" >
<b>Select a country:</b>&nbsp;</td>
<select name="sel"><option value=""><---Select---></option>
<%
Class.forName("com.mysql.jdbc.Driver").newInstance();
String connectionURL = "jdbc:mysql://localhost:3306/test";
Connection connection= DriverManager.getConnection(connectionURL, "root", "root");
PreparedStatement psmnt = connection.prepareStatement("select * from countries ");
ResultSet results = psmnt.executeQuery();
while(results.next()){
String name = results.getString(2);
String id = results.getString(1);
%><option value="<%= name %>">
<% out.println(name); %>
</option>
<%} results.close(); psmnt.close(); %>
</select><br>
<input type="submit" value="Submit"/><br>

</form>
<%String option=request.getParameter("sel");
if(option==null){
}
else{
out.println("You have selected: <b>"+option+"</b>"+"<br>");
}
%>
</html>

Output will be displayed as:

On clicking the dropdown button, it will show you a list of countries:

After selecting the country, click the button, a message is displayed:

Download Source Code:

                          

» View all related tutorials
Related Tags: java c oop jsp io user word loop iteration ip page numbers key value number state this ai oo js

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.