Forum Stats

  • 3,838,498 Users
  • 2,262,376 Discussions
  • 7,900,672 Comments

Discussions

How to update multiple checkbox rows in JDBC

user515049
user515049 Member Posts: 1 Red Ribbon
edited Mar 6, 2017 8:58AM in Java Servlet

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.

Here is my code that works for update:

CollDAO.java - My initial insert if 2 check boxes are selected.

public void addColl(String qId, String[] arrayColId) {  try {  PreparedStatement ps = con.preparedStatement("insert into colTable(qId, colId) values(?,?)");  for(int i = 0; i < arrayColId.length; i++) {  ps.setString(1, qId);  ps.setString(2, arrayColId[i]);  ps.executeUpdate();  }  } catch (SQLException e) {  e.printStackTrace();  }  }

This is the output with is correct

rowIDqIDcID
101121

9

100121

13

When I try to update checkbox records by clicking on 1 more checkox

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 < colId; i++) {  ps.setString(1, colId[i]);  ps.setString(2, qId);  ps.executeUpdate();  }     } catch (SQLException e) {  e.printStackTrace();  }

  ; }

If I select 3 check boxes this is what get updated.

rowIDqIDcID
105121

2

104121

9

10312113
1021219
10112113

This is what it suppose to look like.

rowIDqIDcID
103121

2

102121

9

10112113

Can someone help me?  Thanks

Tagged:
This discussion has been closed.