从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(); 
%>

转载于:https://www.cnblogs.com/skjsg/p/4417910.html

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值