Menu



How to insert multiple checkbox value in database JSP


Insert multiple checkbox value in database using JSP Java is very easy. In the below example we explain you how to easily insert multiple checkbox value in database.

SQL table query

CREATE TABLE `sports_details` (
`id` int(11) NOT NULL,
`choosesports` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Here we using 2 files for insert checkbox value in MySQL database:

index.html

<!DOCTYPE html>
<html>
<body>
<form method="post" action="multiple-checkbox.jsp" >
Select Sports:<br>
<input type="checkbox" name="sports" value="Cricket">Cricket<br>
<input type="checkbox" name="sports" value="Football">Football<br>
<input type="checkbox" name="sports" value="Hockey">Hockey<br>
<input type="checkbox" name="sports" value="Tenis">Tenis<br>
<input type="submit" value="Submit">
</form>
</body>
</html>

multiple-checkbox.jsp

<%@page import="java.sql.*"%>
<%
String choosesports="";
String sports[]=request.getParameterValues("sports");
for(int i=0;i choosesports+=sports[i]+" ";
}
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
Statement st=con.createStatement();
int i=st.executeUpdate("insert into sports_details(choosesports) values('"+choosesports+"')");
out.println("Data is successfully inserted into database.");
}
catch(Exception e){
System.out.println(e);
}
%>