在jsp中查看和删除

时间:2014-04-23 06:46:02

标签: mysql jsp

我对jsp很新,所以我试图实现视图,多重删除等操作但我无法实现这一点。它没有表现出任何错误,但有些人可以对此有所帮助,我曾尝试过:

view.jsp的:

<form action="Del.jsp" method="post">
<center>
<TABLE style="background-color: #ECE5B6;" WIDTH="30%"  border="2px">
        <tr>
            <td>Name</td>
            <td>Password</td>
            <td>Emailid</td>
            <td>Phone no</td>
        </tr>
        <%
            try
            {
                Class.forName("com.mysql.jdbc.Driver");
                String url = "jdbc:mysql://localhost/mine";
                String username = "root";
                String password = "android";
                String query = "select * from Reg";
                Connection conn = DriverManager.getConnection(url, username,password);
                Statement stmt = conn.createStatement();
                ResultSet rs = stmt.executeQuery(query);
                int j = 0;
                while (rs.next())
                {
                    j=j+1;
        %>
        <tr>
            <td><%=rs.getString("name")%></td>


            <td><%=rs.getString("pass")%></td>

            <td><%=rs.getString("emailid")%></td>

            <td><%=rs.getString("phoneno")%></td>
            <td><a href>Edit</a></td>
            <td><input type="checkbox" name="delete" value=<%=rs.getString("name")%>></td>

        <td><input type="hidden" name="recordcount" value="<%=j%>"></td>
        <td><input type="submit" value="Delete" ></td>
        </tr>
        <%
            }
        %>
    </table>
    <%
        rs.close();
            stmt.close();
            conn.close();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    %>

    </center>
    </form>

del.jsp

<body>
<%
    String select[] = request.getParameterValues("name");

    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 a = sb.toString();
    a = a.substring(0, a.length() - 1);
    out.print(a);

    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/Reg", "root", "android");
        Statement st = c.createStatement();
        PreparedStatement pst = c.prepareStatement("DELETE FROM Reg WHERE name ='" + a + "'");
        int i = pst.executeUpdate();
        out.print(i);
        out.println("deleted");
        if (i > 0)
            c.commit();
        else
            c.rollback();
    } catch (Exception e) {
    }

%>
</body>

0 个答案:

没有答案