title: Modified: How to update multiple rows in java jdbc on duplicate value
body: <p>I have a .jsp page that has a multi checkbox.  I'm able to insert the multiple checkbox values but when use an update, it adds the current checkbox rows plus the additional checkbox value rows.  If I check one more box it should add 1 more row and so on.</p>
  
  <p>Here is my code that works for update:
  <strong>CollDAO.java</strong>:</p>
  
  <pre><code>//Insert checkbox records
  public void addColl(String qId, String[] arrayColId) {
    try {
      PreparedStatement ps = con.preparedStatement("insert into colTable(qId, colId) values(?,?)");
  
     for(int i = 0; i &lt; arrayColId.length; i++) {
       ps.setString(1, qId);
       ps.setString(2, arrayColId[i]);
       ps.executeUpdate();
     }
    } catch (SQLException e) {
       e.printStackTrace();
    }
  }
  </code></pre>
  
  <p><strong><em>If I select 2 checkboxes this is what it looks like.</em></strong></p>
  
  <p><strong>rowid</strong> | <strong>qID</strong> | <strong>cID</strong> --   <strong><em>CORRECT</em></strong></p>
  
  <p>:101:   | :121: | :9:</p>
  
  <p>:100:   | :121: | :13:</p>
  
  <pre><code>//Update checkbox records
  public void updateColl(String qId, String[] arrayColId) {
    try {
      String sql = "update colTable set colId=?, where qId=?";
     PreparedStatement ps = con.preparedStatement(sql);
  
     for(int i = 0; i &lt; colId; i++) {
       ps.setString(1, colId[i]);
       ps.setString(2, qId);
       ps.executeUpdate();
     }
  
    } catch (SQLException e) {
       e.printStackTrace();
    }
  }
  </code></pre>
  
  <p><strong><em>If I select 3 checkboxes this is what get updated.</em></strong> </p>
  
  <p><strong>rowid</strong> | <strong>qID</strong> | <strong>cID</strong> -- <strong><em>WRONG OUTPUT</em></strong></p>
  
  <p>:105:   | :121: | :2:</p>
  
  <p>:104:   | :121: | :9:</p>
  
  <p>:103:   | :121: | :13:</p>
  
  <p>:101:   | :121: | :9:</p>
  
  <p>:100:   | :121: | :13:</p>
  
  <p><strong><em>This is what it suppose to look like.</em></strong></p>
  
  <p><strong>rowid</strong> | <strong>qID</strong> | <strong>cID</strong> -- <strong><em>CORRECT OUTPUT</em></strong></p>
  
  <p>:103:   | :121: | :2:</p>
  
  <p>:101:   | :121: | :9:</p>
  
  <p>:100:   | :121: | :13:</p>
  
  <p>I've been working on this for a week, can someone help me?</p>
  
  <p>Thank you</p>