如何使用准备好的语句将选定的多个复选框值插入到SQL Server数据库中?

时间:2018-10-22 12:21:33

标签: html servlets jdbc

<html>
<head>
<title>Welcome</title>
</head>
<body>
<form  action="First_prgm" method="post">  
 <table>
<tr>
 <td> <b>3)Not including yourself, how many adults and children live in the same home as you on a permanent basis?</b></td>
<td><input type="checkbox" name="children" value="adults"> Adults (aged 18 and over) <br>
<input type="checkbox" name="children" value="0-4 years">Children-aged 0-4 years / house mates <br>
<input type="checkbox" name="children" value="5-8 years">Children-aged 5-8 years <br>
<input type="checkbox" name="children" value="9-11 years"> Children-aged 9-11 years <br>
<input type="checkbox" name="children" value="12-15 years"> Children-aged 12-15 years <br>
<input type="checkbox" name="children" value="16-17 years">Children-aged 16-17 years<br><br></td>
</tr>
</table>
</body>

First_prgm.java:

public  class First_prgm extends HttpServlet {

public void doPost(HttpServletRequest request,HttpServletResponse response)
                throws IOException {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            String insertTableSQL = "INSERT INTO MembersQnariesData"
                    + "(MemberID,FirstName,LastName,AgrGroup,AgeBrackets,LivingArrangement,DateOfBirth) VALUES"
                    + "(?,?,?,?,?,?,?)";


            int id = Integer.parseInt(request.getParameter("mid"));
            String fname = request.getParameter("first");
            String lname=request.getParameter("last");
            String a[] =request.getParameterValues("children");
            String age_brackets = request.getParameter("age");
            String living_arngmt  = request.getParameter("member");


           String dob = request.getParameter("DOB");




            try {

                  Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                Connection con = DriverManager.getConnection("jdbc:sqlserver://server;databaseName","username","password");

                PreparedStatement ps = con
                        .prepareStatement(insertTableSQL);

                ps.setInt(1,id);
                ps.setString(2,fname);
                ps.setString(3,lname);
                for(int j=0; j <a.length; j++){
                     ps.setString(4,a[j]);
                     out.println("selected values are"+a[j]);
                     }
                 ps.setString(5,age_brackets);
                 ps.setString(6, living_arngmt);
                 ps.setString(7,dob);

                int i = ps.executeUpdate();
                if(i>0){
                   out.println(i+ "row inserted");
                }        
            }
            catch (Exception e2) {
                e2.printStackTrace();
            }

            out.close();
        }

任何人都可以帮助我。是一个初学者。我创建了一个带复选框的Web应用程序。当用户选择值时,最新值将插入数据库中。我需要将所选值插入数据库的不同列中。

非常感谢。

1 个答案:

答案 0 :(得分:0)

下面的代码就是问题,

for(int j=0; j <a.length; j++){
                     ps.setString(4,a[j]);
                     out.println("selected values are"+a[j]);
  }

您将计数器值固定为-4,这就是为什么仅获得最后一个值的原因。

由于您事先不知道复选框的数量,因此需要使用StringBuilder动态构建查询,并在查询末尾附加a.length问号。

然后在PreparedStatement中设置值时,需要取int counter并在设置值后将其递增。

ps.setInt(1,id);
            ps.setString(2,fname);
            ps.setString(3,lname);
             ps.setString(4,age_brackets);
             ps.setString(5, living_arngmt);
             ps.setString(6,dob);
int counter = 7; //since six question marks are already added for other fields
for(int j=0; j <a.length; j++){
                         ps.setString(counter++,a[j]);
                         out.println("selected values at index : "+j+" is : "+a[j]);
      }

或者您可以初始化counter = 1并从头开始,如

int counter = 1;
ps.setInt(counter++,id);
                ps.setString(counter++,fname);
                ps.setString(counter++,lname);
                 ps.setString(counter++,age_brackets);
                 ps.setString(counter++, living_arngmt);
                 ps.setString(counter++,dob);
    ; //since six question marks are already added for other fields
    for(int j=0; j <a.length; j++){
                             ps.setString(counter++,a[j]);
                             out.println("selected values at index : "+j+" is : "+a[j]);
          }