在 servlet 中使用数组中的多个参数

发布于 2024-11-06 05:07:27 字数 1582 浏览 0 评论 0原文

这是我的 .jsp 页面中的 HTML:

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

因此,用户将在此处选择复选框并提交将其删除。但我希望他们能够选择多个复选框。例如,如果他们选择了两个复选框,例如 # 的 5 和 6,那么它将作为 5COM 和 6COM 到达 servlet(这是由于糟糕的表设计)。接下来,我尝试从 jsp 页面获取多个参数,并将它们写入 servlet 中的数据库。这是我的定义:

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

因此,使用上面的示例,comEdit 数组应包含 5COM 和 6COM...接下来,我尝试获取数组“comEdit”中每个项目的子字符串,然后尝试将其放入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]);
  }

所以坚持使用相同的示例,我试图创建一个新的 int 数组,它将给我 [5, 6]....现在上面的部分很可能是不正确的,但我试了一下,并且你可以看到我的意图。在这里,我尝试在可调用语句中使用该数组:

    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));

因此,再次使用该示例,stmt.setArray 函数应将数组 [5,6] 发送到 My_function。这又是我的尝试,可能不正确?任何帮助表示赞赏。谢谢!

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 = ArrayDescriptor.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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静水深流 2024-11-13 05:07:27

根据您对我的评论的回复,这符合您的要求吗?

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));
}

我不熟悉这些 Oracle 对象,所以我不确定以这种方式传递“comEditDels”会发生什么,但我认为这就是您想要的?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文