开发者

Using multiple parameters from an array in a servlet

开发者 https://www.devze.com 2023-03-05 00:40 出处:网络
Here is the HTML from my .jsp page: <tr> <td><input type=\"checkbox\" name=<%=editID%>COM /> </td>

Here is the HTML from my .jsp page:

<tr>
  <td><input type="checkbox" name=<%=editID%>COM /> </td>    
  <td>blah blah</td>
  <td>blah blah</td>
</tr>

So here the user will select checkboxes and submit them to be deleted. But I want them to be able to select multiple checkboxes. For instance, if they chose two checkboxes, say #'s 5 and 6, it will come to the servlet as 5COM and 6COM (this is due to bad table design). So next, I am trying to get multiple parameters from my jsp page, and write them to my database in my servlet. Here are my definitions:

  String[] comEdit = request.getParameterValues("editIDCom");   
  String comEditDel = "";
  int[] comEditDels = null;

So using the above example, the comEdit array should contain 5COM and 6COM....Next, I am trying to get the substring of every item in the array 'comEdit' and then I am trying to put it into a int Array:

  for (int i = 0; i < comEdit.length; ++i) {
      String com = comEdit[i];
      comEditDel = com.substring(0, com.length() - 3);    
      comEditDels = new int[comEditDel == null ? 0: comEditDel.length];
      comEditDels[i] = Integer.parseInt(comEditDel[i]);
  }

So sticking with the same example, I am trying to make a new int Array that will give me [5, 6]....Now the above part is most likely incorrect, but I gave it a shot, and you can see my intentions. Here I am trying to use the array in my callable statement:

    CallableStatement stmt = null;
    PreparedStatement pstmt = null;
    Connection conn = null;
    ArrayDescriptor ad = null;
    String dSource = getServletContext().getInitParameter("dataSource");

   stmt = conn.prepareCall("{?= call My_function(?)}");
   stmt.registerOutParameter(1, Types.INTEGER);
   ad = ArrayDesc开发者_开发技巧riptor.createDescriptor("NUM_ARRAY", conn);
   stmt.setArray(2,new ARRAY(ad, conn, comeditDels));

So again, using the example, the stmt.setArray function should send the array [5,6] to My_function. Again this is my attempt, and it is prob incorrect? Any help is appreciated. Thanks!


Based on your reply to my comment, does this do what you want?

String[] comEdit = request.getParameterValues("editIDCom");   
int[] comEditDels = null;

if (comEdit != null) {
    comEditDels = new int[comEdit.length];

    for (int i = 0; i < comEdit.length; ++i) {
        String com = comEdit[i];
        if (com != null && com.length() - 3 > 0) {
            try {
                comEditDels[i] = Integer.parseInt(com.substring(0, com.length() - 3));
            } catch (NumberFormatException exception) {
                // do something or possibly ignore since the value isn't a valid integer
            }
        }
    }
}

if (comEditDels != null) {
    CallableStatement stmt = null;
    PreparedStatement pstmt = null;
    Connection conn = null;
    ArrayDescriptor ad = null;
    String dSource = getServletContext().getInitParameter("dataSource");

    stmt = conn.prepareCall("{?= call My_function(?,?,?,?)}");
    stmt.registerOutParameter(1, Types.INTEGER);
    ad = ArrayDescriptor.createDescriptor("NUM_ARRAY", conn);
    stmt.setArray(2,new ARRAY(ad, conn, comEditDels));
}

I'm not familiar with those Oracle objects so I'm not sure what to expect from passing in 'comEditDels' that way, but I'm thinking that's what you want?

0

精彩评论

暂无评论...
验证码 换一张
取 消