Reputation: 115
I have 1 applications in my project called select.jsp ,in select.jsp i have one dropdown box, if i don't select anything in dropdown box ,all the values in that box should insert in mysql table using jsp,please help me to solve this.
i tried the following code:
select.jsp contains the following code:
<%@page import="java.sql.*"%>
<html>
<head>
</head>
<body>
<form id="frm1" name="frm_addUser" method="post" action="./index.jsp">
Select Programming Language:
<select name="lang" id="t">
<option value="select">select language</option>
<option value="C/C++">C/C++</option>
<option value="C#">C#</option>
<option value="Java">Java</option>
<option value="Perl">Perl</option>
<option value="Python">Python</option>
</select><br>
<input type="text" id="cmblen" name="cmbna">
<input type="submit" value="Submit" ><br>
</form>
<%!
String tlen="";
String languages="";%>
<%
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","sumith");
Statement st=con.createStatement();
int j=0;
String lang[]=request.getParameterValues("lang");
for(int i=0; i<lang.length; i++)
{
//languages+=lang[i]+", ";
j=st.executeUpdate("insert into combo(language) values('"+lang[i]+"')");
}
out.println("Data is successfully inserted into database.");
}
catch(Exception e)
{
//out.println(e);
//e.printStackTrace();
}
%>
</body>
</html>
but myproblem is in mysql table i am able to insert only firstvalue of combobox,but the remaining values are not inserting in to the mysql table,can you please help me to solve this.
Upvotes: 0
Views: 9404
Reputation: 974
if (!selectedValue.equals("select")) {
int i = st.executeUpdate("insert into combo (combocol) values ('" + selectedValue + "')");
} else {
int i = st.executeUpdate("insert into combo (combocol) values ('C/C++'),('C#'),('java'),('perl'),('python')");
}
Upvotes: 0