无法从数据库中删除用户

时间:2015-12-28 13:39:27

标签: mysql jsp jsp-tags

ViewUser.jsp

    <%
        String username = (String) session.getAttribute("name");
    %>
    <%@ page contentType="text/html; charset=utf-8" language="java"
        import="java.sql.*" errorPage=""%>
    <%@page
        import="com.oreilly.servlet.*,java.sql.*,java.lang.*,java.text.SimpleDateFormat,java.util.*,java.io.*,javax.servlet.*, javax.servlet.http.*"%>
    <%@page import=" java.security.MessageDigest"%>
    <%@page import=" java.security.*"%>
    <%@page import="javax.crypto.*"%>
    <!DOCTYPE html>
    <!--[if lt IE 7]> <html class="lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
    <!--[if IE 7]> <html class="lt-ie9 lt-ie8" lang="en"> <![endif]-->
    <!--[if IE 8]> <html class="lt-ie9" lang="en"> <![endif]-->
    <!--[if gt IE 8]><!-->
    <html lang="en">
    <!--<![endif]-->
    <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>View User</title>
    <link rel="stylesheet" href="home.css">
    <!--[if lt IE 9]><script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
    </head>
    <body>
        <br>
        <br>
        <p class="user">
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            Welcome &nbsp;<%=username%></p>

        <header>
            <nav>
                <ul class="nav">
                    <li><a href="Admin_Home.jsp" class="icon home"><span>Home</span></a></li>
                    <li><a href="ViewUser.jsp">View User</a></li>
                    <li><a href="ViewData.jsp">View Data</a></li>
                    <li><a href="index.html">Logout</a></li>
                </ul>
            </nav>
        </header>
        <br>
        <br>
        <br>
        <marquee class="head1" behavior="scroll" direction="left">Information Of Users...! </marquee>
        <div id="page" style="margin-top: 25px;"></div>
            <div id="content">

                <div style="clear: both;">&nbsp;</div>

                <div style="width: 100%; margin-top: 40px;">
                    <table class="head1"
                        style="border: 4px solid black; border-spacing: 15px; border-collapse: collapse; width: 60%; border-radius: 10px; box-shadow: #000 2px 2px 2px 2px; margin: 80px;">
                        <tr style="color: white;">
                            <th
                                style="padding: 3px; width: 480px; margin-top: 0px; margin-left: -10px; padding-left: 00px;">USERNAME</th>
                            <th
                                style="padding: 3px; width: 220px; margin-top: 0px; margin-left: -10px; padding-left: 00px;">PASSWORD</th>
                            <th
                                style="padding: 3px; width: 230px; margin-top: 0px; margin-left: -10px; padding-left: 00px;">PHONE NO.</th>
                            <th
                                style="padding: 3px; width: 230px; margin-top: 0px; margin-left: -10px; padding-left: 00px;">EMAIL</th>

                            <th
                                style="padding: 3px; width: 230px; margin-top: 0px; margin-left: -10px; padding-left: 00px;">Remove User</th>

                        </tr>

                        <%
                            try {
                                Class.forName("com.mysql.jdbc.Driver");
                                Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/db", "root", "root123");

                                Statement st = con.createStatement();
                                ResultSet rs = st.executeQuery("SELECT username,password,phone,address FROM newuser");
                                while (rs.next()) {
                                    username=rs.getString("username");
                                    String password = rs.getString("password");
                                    String address = rs.getString("address");
                                    String phone = rs.getString("phone");
                        %>
                        <tr>
                            <td style="padding: 10px; height: 60px;"><span
                                style="margin-left: 5px;"><%=username%></span></td>
                            <td style="padding: 3px; height: 60px;"><span
                                style="margin-left: 5px;"><%=password%></span></td>
                            <td style="padding: 3px; height: 60px;"><span
                                style="margin-left: 5px;"><%=phone%></span></td>
                            <td style="padding: 3px; height: 60px;"><span
                                style="margin-left: 5px;"><%=address%></span></td>

                            <td style="padding: 3px; height: 60px;"><span
                                style="margin-left: 5px;"><a href="RemoveUser.jsp"
                                    <%=username%>>Remove</a></span></td>



                        </tr>

                        <%
                            }

                            } catch (Exception e) {
                            }
                        %>


                    </table>

                </div>




                <div style="clear: both;">&nbsp;</div>
            </div>
    </body>
    </html>


RemoveUser.jsp


<%
    String username = (String) session.getAttribute("name");
%>
<%@page
    import="com.oreilly.servlet.*,java.sql.*,java.lang.*,java.text.SimpleDateFormat,java.util.*,java.io.*,javax.servlet.*, javax.servlet.http.*"
    errorPage="Error.jsp"%>
<%@page import=" java.security.MessageDigest"%>
<%@page import=" java.security.*"%>
<%@page import="javax.crypto.*"%>


<%
    username = request.getParameter("username");
    Connection con = null;

    try {

        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/db", "root", "root123");
        } catch (ClassNotFoundException e) {

            e.printStackTrace();
        }

        Statement st = con.createStatement();
        int x = st.executeUpdate("delete from newuser where username='" + username + "'");
        if (x != 0) {
            out.println("Success..!!");
        } else {
            out.println("Failed..!!");
        }

    } catch (Exception e) {
        out.println(e);
    }
%>

您好, 我在删除用户时遇到问题。它显示执行后输出失败。我认为它不需要来自viewUser的用户名。请帮我。 查询没问题,数据库连接仍然存在问题,取用用户名并删除它。 谢谢 !

2 个答案:

答案 0 :(得分:0)

还有其他例外吗? 因为x = 0表示更新已执行,但没有发现删除,请注意mysql区分大小写,还需要提交操作。

答案 1 :(得分:0)

Remove.jsp页面中:
...

username = request.getParameter("username");

没有任何参数是&#39;用户名&#39;,您需要在?之后的删除链接中设置参数Remove.jsp?<%=username%>

ViewUser.jsp页面中:
...
<td style="padding: 3px; height: 60px;"> <span style="margin-left: 5px;"> <a href="RemoveUser.jsp"<%=username%>>Remove</a> </span> </td>

应该修改:
...
<td style="padding: 3px; height: 60px;"> <span style="margin-left: 5px;"> <a href="RemoveUser.jsp?<%=username%>">Remove</a> </span> </td>