JSP Radio Button Example

JSP Radio Button Example


Posted in : Java Posted on : May 8, 2012 at 6:58 PM Comments : [ 0 ]

In this tutorial you will learn about how to fetch the value of selected radio button and save this value to the database.

JSP Radio Button Example

In this tutorial you will learn about how to fetch the value of selected radio button and save this value to the database.

Here we are giving a simple example for you which is demonstrating about how to fetch the value of radio button you have selected and saved it to the database. Radio Button is used for selecting only one of a limited number of options. So, to find out the value you have selected and to save them to the database at first I have created a database table named cuisine then created a JSP page where I have designed a form to give some radio button options to select them. Then to save this fetched value to the database table I have written code in java for creating a database connection with MySql. After establishing a connection with database I have used the insert query to insert the value in the database table. Below I am giving the code that I have written :

At first create a database table in MySql like :

Database table cuisine :

CREATE TABLE `cuisine` ( 
`favoriteCuisine` varchar(15) default NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1

jspRadioButton.jsp

<%@ page
language="java" 
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" 
import="java.sql.DriverManager,
java.sql.Connection, 
java.sql.PreparedStatement, 
java.sql.ResultSet, 
java.sql.SQLException"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>JSP Radio Button</title>
</head>
<body>
<form>
<h3>Select Your Favorite Cuisine :</h3>
<p><input type="radio" name="dish" value="Indian"> Indian</input></p>
<p><input type="radio" name="dish" value="Continental"> Continental</input></p>
<p><input type="radio" name="dish" value="Chinese"> Chinese</input></p>
<p><input type="radio" name="dish" value="Italian"> Italian</input></p>
<p><input type="radio" name="dish" value="Russian"> Russian</input></p>
<input type="submit" value="Submit">
</form>
<br>
<%! 
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://192.168.10.13/data";
String uid = "root";
String psw = "root";
Connection con;
ResultSet rs;
PreparedStatement ps;
%>
<%
try
{
Class.forName(driver);
con = DriverManager.getConnection(url,uid,psw);
}
catch(ClassNotFoundException cx)
{
out.println(cx);
}
String option = request.getParameter("dish");
if(option !=(null))
{ 
try{

String sql = "INSERT INTO cuisine(favoriteCuisine) VALUES(?)";
ps= con.prepareStatement(sql);
ps.setString(1, option);
int i = ps.executeUpdate();
if(i != 0)
{
out.println("Your order has been made successfully");
}
else
out.println("Sorry your order can not be made try again later");

%>
<p>You Cuisine order is for 
<%= option %></p>
<%
}
catch(Exception e)
{
out.println(e);
}
finally
{
if(rs != null)
{
rs.close();
}
if(ps != null)
{
ps.close();
}
if(con != null)
{
con.close();
}
}
} 
%>
</body>
</html>

Output :

1. Database table cuisine that I have created as :

2. When you will execute the above JSP page You will get the output as :

3. When you will select one of the option from the above radio button options and will click on "submit" button you will get the output as follows :

4. And in the database table value will be stored as follows :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics