0

checkbox.jsp

<form action="test.jsp">
    <input type="checkbox" name="id" value="Java"> Java<br>
    <input type="checkbox" name="id" value=".NET"> .NET<br>
    <input type="checkbox" name="id" value="PHP"> PHP<br>
    <input type="checkbox" name="id" value="C/C++"> C/C++<br>
    <input type="checkbox" name="id" value="PERL"> PERL <br>
    <input type="checkbox" name="id" value="vb SCRIPT"> PERL <br>
    <input type="submit" value="Submit">
</form>

test.jsp

<% 
    String select[] = request.getParameterValues("id"); 

    if (select != null && select.length != 0) {
        out.println("You have selected: ");
        for (int i = 0; i < select.length; i++) {
            out.println(select[i]); 
        }
    }

    String selected="";
    StringBuffer sb = new StringBuffer(); 

    for(int i = 0; i < select.length; i++) {
        sb.append(select[i] + ",");
    }

    String vwarnid = sb.toString(); 
    vwarnid = vwarnid.substring(0, vwarnid.length()-1);
    out.print(vwarnid);

    try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection c = DriverManager.getConnection("jdbc:odbc:Task","","");
        Statement st = c.createStatement();
        PreparedStatement pst = c.prepareStatement
            ("DELETE FROM checkdata WHERE name    IN('"+vwarnid +"')");
        int i=pst.executeUpdate();
        out.print(i);
        out.println("deleted");
    } catch(Exception e) {
    }
%>

when I select only one checkbox the value is getting deleted but not with multiple selection of checkboxes Can anyone help me out?

Deep
  • 461
  • 5
  • 9
  • 17

2 Answers2

1

Does this

SELECT * FROM checkdata WHERE name IN(('"+vwarnid+"'))");

return any rows ?

Hardik Mishra
  • 14,779
  • 9
  • 61
  • 96
1

Your IN clause should end up as

IN('Java','.NET','PHP')

but the way as you coded it, it ends up as

IN('Java,.NET,PHP')

which is obviously wrong.


Unrelated to the concrete problem, there are several serious issues in your code. Please sanitize your input from SQL injection attacks. Learn PreparedStatement. Using IN clause with PreparedStatement is outlined in this answer. Also putting raw Java code in a JSP file isn't really the recommended practice. Learn servlets. Also ignoring exceptions isn't very helpful for the case you run in to trouble. Also the DB resources should be close()d in finally block.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • then how to make it the other way – Deep Mar 21 '11 at 14:26
  • Just replace `","` by `"','"`. – BalusC Mar 21 '11 at 14:28
  • when I print the string I am getting output as Java','.NET','PHP after selecting check box.Replacing it is working ,but I am not understanding the logic.ts the String has to be 'Java','.NET','PHP'.Then how is my program working – Deep Mar 21 '11 at 15:06
  • Yes, but you have already the starting and trailing quote in your SQL :) – BalusC Mar 21 '11 at 15:07