0

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!

4
  • 1
    I'm not too sure your intentions are clear :). Every iteration over the contents of comEdit reinitializes the variable comEditDels which I'm not sure why you'd want to do. Also comEditDel[i] will product a compilation error since a String doesn't support []. Commented May 11, 2011 at 19:06
  • @laz, sorry, about that. I am very much a novice at this. I want to use the substring of every item in the 'comEdit' array and send that to my function, and I was trying to do that throught the comEditDels variable. Does that help at all? Commented May 11, 2011 at 19:12
  • I can't really understand what it is you're trying to do either. Maybe if you posted an example of the values you're expecting, how they should be processed and what you'd like to see written to the DB, that might help clarify - a walkthrough is usually helpful. Commented May 11, 2011 at 19:20
  • @laz , @no.good.at.coding, I have updated my post, and tried to include a walkthrough....let me know what you think. Commented May 11, 2011 at 19:41

1 Answer 1

2

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?

Sign up to request clarification or add additional context in comments.

4 Comments

I think it is close....i get a null pointer because the value can be null coming from my .jsp page. So how would I wrap the for stmt in an if...else? if(comEdit != null) {for (int i = 0; i < comEdit.length; ++i) { String com = comEdit[i]; comEditDels[i] = Integer.parseInt(com.substring(0, com.length() - 3)); } else { comEditDel = -1;} Would that work?
See that compiles just fine, but I still get a nullPointer on the line 'int[] comEditDels = new int[comEdit.length];' bc if comEdit comes in null....which is possible....I get a null pointer on that line of code.
I missed moving that into the null check. Whoops!
Awesome, thank you! I finally got it running correctly. I really appreciate all of your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.