现在的位置: 首页 > 综合 > 正文

jsp 获得checkbox值并写入数据库

2018年05月15日 ⁄ 综合 ⁄ 共 1709字 ⁄ 字号 评论关闭
 

 jsp 获得checkbox值并写入数据库,可通过以下方法:

    1.获得checkbox的值,并传入数组中

    2.将数组值转化为字符串

    3.写入数据库

例子:

        Checkbox.htm

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3.     <head>
  4.         <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  5.         <title>test</title>
  6.     </head>
  7.     <body>
  8. <form name="myform" id="myform" method="post" action="CorAdd.jsp">  
  9.         <label>
  10.             <input type="checkbox" name="CorpMode" value="1" />物品
  11.         </label>
  12.         <label>
  13.             <input type="checkbox" name="CorpMode2" value="2" />人头
  14.         </label>
  15.         <label>
  16.             <input type="checkbox" name="CorpMode2" value="3" />其他
  17.         </label>      
  18.         <input name="" type="button"onclick="document.myform.action='Submit.jsp?menu=add'" />
  19. </form>
        </
    body>
  20. </html>

 

   提交页面 : Submit.jsp

 

  1. <%@ page contentType="text/html; charset=utf-8" language="java" import = "java.util.*, java.text.*,java.sql.*"%>
  2. <%
  3.     if(menu.equals("add"))
  4. {
  5.     String tmp="";
  6.     String SCorpMode="";
  7.     String[] CorpMode = request.getParameterValues("CorpMode");
  8.     if(CorpMode.length>0)
  9.         {
  10.             for(int i=0;i<CorpMode.length;i++)
  11.             { 
  12.                 SCorpMode=SCorpMode+CorpMode[i]+","
  13.             }
  14.             SCorpMode=SCorpMode.substring(0,SCorpMode.length()-1);//去掉SID中的最后一个逗号 
  15.         }
  16. tmp ="insert into Corporation (CorpMode) values('+SCorpMode+');
  17.     if(dbc.executeUpdate(tmp)>=0)
  18.        out.println("<script>alert('信息添加成功!');location.href='CorpAdd.jsp'</script>");
  19.     else
  20.         
  21.         //out.println("<script>alert('添加失败!');location.href='CorpAdd.jsp'</script>");
  22.         out.print(tmp);
  23. }
  24. }
  25. %>
  26.     

抱歉!评论已关闭.