从jsp页面获取复选框的值并插入数据库!
如下为课堂练习中,个人博客中的留言版模块:
复选框(爱好)这一栏怎么读入数据库呢?请听如下分解:
第一种方法:复选框所有内容都设置不同的name值,用jsp页面接受。接受页面使用if语句判断是否传值为空。代码如下:
guest.jsp
<form id="form1" name="form1" method="post" action="guest_panduan.jsp">
<table width="80%" height="263" border="1">
<tr>
<td align="left">姓名:
<label for="textfield2"></label>
<input type="text" name="name" id="name" /></td>
</tr>
<tr>
<td align="left">学号:
<label for="textfield3"></label>
<input type="text" name="xuehao" id="xuehao" /></td>
</tr>
<tr>
<td align="left">性别:
<input type="radio" name="sex" id="radio" value="男" />
<label for="sex">男
<input type="radio" name="sex" id="radio2" value="女" />
女</label></td>
</tr>
<tr>
<td align="left">所在地:
<label for="select"></label>
<select name="select" id="select">
<option value="海南">海南</option>
<option value="北京">北京</option>
<option value="上海">上海</option>
<option value="广东">广东</option>
</select>
<label for="textarea"></label></td>
</tr>
<tr>
<td align="left">爱好:
<input name="checkbox1" type="checkbox" id="checkbox11" value="学习" />
<label for="checkbox">学习
<input name="checkbox2" type="checkbox" id="checkbox21" value="编码" />
编码
<input name="checkbox3" type="checkbox" id="checkbox31" value="唱歌" />
唱歌</label></td>
</tr>
<tr>
<td align="left">请留下宝贵意见:</td>
</tr>
<tr>
<td align="left"><label for="textarea"></label>
<textarea name="textarea" id="textarea" cols="40" rows="5"></textarea></td>
</tr>
<tr>
<td align="left"><input name="button" type="submit" id="button" value=" 提交" />
<input type="reset" name="button2" id="button2" value="重置" />
</td>
</tr>
</table>
</form>
guset_panduan.jsp
<%
request.setCharacterEncoding("utf-8");
String name_0=request.getParameter("name");
String xuehao_0=request.getParameter("xuehao");
String sex_0=request.getParameter("sex");
String select_0=request.getParameter("select");
String checkbox_0=request.getParameter("checkbox1");
String checkbox_1=request.getParameter("checkbox2");
String checkbox_2=request.getParameter("checkbox3");
String a=null;
if(checkbox_0!=null)
{
a=checkbox_0;
if(checkbox_1!=null)
{
a=checkbox_0+checkbox_1;
if(checkbox_2!=null)
{
a=checkbox_0+checkbox_1+checkbox_2;
}
}
else
{
if(checkbox_2!=null)
{
a=checkbox_0+checkbox_2;
}
}
}
else
{
if(checkbox_1!=null)
{
a=checkbox_1;
if(checkbox_2!=null)
{
a=checkbox_1+checkbox_2;
}
}
else
{
if(checkbox_2!=null)
{
a=checkbox_2;
}
}
}
//System.out.println(checkbox_0+checkbox_1+checkbox_2);
String textarea_0=request.getParameter("textarea");
Connection con = null;
Statement stmt = null;
String url = "jdbc:sqlserver://127.0.0.1:1433;databaseName=zhuce;user=sa;password=sa";//sa身份连接
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(url);
String SQL = "insert into liuyan values('"+name_0+"','"+xuehao_0+"','"+sex_0+"','"+select_0+"','"+a+"','"+textarea_0+"')";
stmt = con.createStatement();
int i = stmt.executeUpdate(SQL);
if(i==1)
{
out.println("<script language='javaScript'>alert('提交成功');</script>");
response.setHeader("refresh","1;url=Tijiao.jsp");
}
else{
out.println("<script language='javaScript'>alert('提交失败');</script>");
response.setHeader("refresh","1;url=guest.jsp");
}
stmt.close();
con.close();
%>
第二种方法:设置复选框所有内容都使用同一个name值;先定义一个数组,循环读取复选框的值之后再存入数据库。详细代码如下:
guest.jsp
<form id="form1" name="form1" method="post" action="guest_panduan.jsp">
<table width="80%" height="263" border="1">
<tr>
<td align="left">姓名:
<label for="textfield2"></label>
<input type="text" name="name" id="name" /></td>
</tr>
<tr>
<td align="left">学号:
<label for="textfield3"></label>
<input type="text" name="xuehao" id="xuehao" /></td>
</tr>
<tr>
<td align="left">性别:
<input type="radio" name="sex" id="radio" value="男" />
<label for="sex">男
<input type="radio" name="sex" id="radio2" value="女" />
女</label></td>
</tr>
<tr>
<td align="left">所在地:
<label for="select"></label>
<select name="select" id="select">
<option value="海南">海南</option>
<option value="北京">北京</option>
<option value="上海">上海</option>
<option value="广东">广东</option>
</select>
<label for="textarea"></label></td>
</tr>
<tr>
<td align="left">爱好:
<input name="checkbox" type="checkbox" id="checkbox1" value="学习" />
<label for="checkbox">学习
<input name="checkbox" type="checkbox" id="checkbox2" value="编码" />
编码
<input name="checkbox" type="checkbox" id="checkbox3" value="唱歌" />
唱歌</label></td>
</tr>
<tr>
<td align="left">请留下宝贵意见:</td>
</tr>
<tr>
<td align="left"><label for="textarea"></label>
<textarea name="textarea" id="textarea" cols="40" rows="5"></textarea></td>
</tr>
<tr>
<td align="left"><input name="button" type="submit" id="button" value=" 提交" />
<input type="reset" name="button2" id="button2" value="重置" />
</td>
</tr>
</table>
</form>
guest_panduan.jsp
<%
request.setCharacterEncoding("utf-8");
String name_0=request.getParameter("name");
String xuehao_0=request.getParameter("xuehao");
String sex_0=request.getParameter("sex");
String select_0=request.getParameter("select");
String[] checkbox_0=request.getParameterValues("checkbox");
String a="";
if(checkbox_0.length>0)
{
for(int j=0;j<checkbox_0.length;j++)
{
a=a+checkbox_0[j]+",";
}
//a=a.substring(0, a.length()-1);
}
System.out.print(a);
//System.out.println(checkbox_0+checkbox_1+checkbox_2);
String textarea_0=request.getParameter("textarea");
Connection con = null;
Statement stmt = null;
String url = "jdbc:sqlserver://127.0.0.1:1433;databaseName=zhuce;user=sa;password=sa";//sa身份连接
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(url);
String SQL = "insert into liuyan values('"+name_0+"','"+xuehao_0+"','"+sex_0+"','"+select_0+"','"+a+"','"+textarea_0+"')";
stmt = con.createStatement();
int i = stmt.executeUpdate(SQL);
if(i==1)
{
out.println("<script language='javaScript'>alert('提交成功');</script>");
response.setHeader("refresh","1;url=Tijiao.jsp");
}
else{
out.println("<script language='javaScript'>alert('提交失败');</script>");
response.setHeader("refresh","1;url=guest.jsp");
}
stmt.close();
con.close();
%>