jsp-单击更新按钮后无法更新MySQL中的表

时间:2019-01-29 02:49:51

标签: java mysql jsp

我创建了三个不同的JSP页面,我的目标是选择所需的用户并编辑其详细信息并更新db中的表。

首页如下所示:

Screenshot

updateuser.jsp:

<body>
<%

    Class.forName("com.mysql.jdbc.Driver");
    String connectionURL = "jdbc:mysql://localhost:3306/gmc";
    String username = "root";
    String password = "root";
    Connection conn = null;

    conn = DriverManager.getConnection(connectionURL, "root", "root");

    String query = "select ausername, ausertype from auser where AUSTATE='Y'";
    Statement stmt = conn.createStatement();
    ResultSet resultset = stmt.executeQuery(query);
%>


    <table align="center" cellpadding="2" cellspacing="2" border="1">
        <tr bgcolor="#d9ac26">
            <th>Username </th>
            <th>User Type </th>
            <th>Action </th>


        </tr>
            <% while(resultset.next()){ %>
        <tr>

            <td><%=resultset.getString("ausername")%> </td>
            <td><%=resultset.getString("ausertype")%> </td>

            <td><a href="edit.jsp?name=<%=resultset.getString("ausername") %> "><input type="button" value="Edit"></a></td>





        </tr>
            <% } %>




</body>

然后,将进入如下所示的“编辑”页面:

Edit form

EDIT.JSP:

<%

        String user = request.getParameter("name");


            Class.forName("com.mysql.jdbc.Driver");


        Connection con = null;

        Statement stmt = null;
        con=DriverManager.getConnection("jdbc:mysql://localhost:3306/gmc","root","root");
        Statement statement = con.createStatement() ;
        ResultSet resultset = statement.executeQuery("select * from AUSER where AUSERNAME='"+user+"'") ;
%>



    <form method="get" action="updatetable.jsp">



        <% while(resultset.next()){ %>
    <% String autype = resultset.getString("ausertype");%>
        Current Username: <input type="text" name="old" value=<%=resultset.getString("AUSERNAME")%> readonly/>
        New Username: <input type="text" name="new" />
        <br><br>
        User Type :<br>

        <% if (autype.equalsIgnoreCase("SUPERUSER")) {%>
        <input type ="radio" name="myradio" value="superuser" checked >SUPERUSER<br/>
        <%}else{%>
        <input type ="radio" name="myradio" value="superuser">SUPERUSER<br/>
        <%}%>

        <% if (autype.equalsIgnoreCase("BASIC")) {%>
        <input type ="radio" name="myradio" value="basic" checked >BASIC<br/>
        <%}else{%>
        <input type ="radio" name="myradio" value="basic">BASIC<br/>
        <%}%>

        <% if (autype.equalsIgnoreCase("VIEW")) {%>
        <input type ="radio" name="myradio" value="view" checked >VIEW<br/>
        <%}else{%>
        <input type ="radio" name="myradio" value="view">VIEW<br/>
        <%}%>



          <%  }
%>


        </table>

    <br>
        <input type="submit" value="Update" />
    </form>


</body>

用户可以选择输入新的用户名(可选)并更改其用户类型。这样做并单击“更新”后,它应该能够将这些更改更新到具有“用户名”列和“用户类型”列的表中。

UpdateTable.jsp:

<body>

<%

     class UpdateUserDetails extends HttpServlet {




        protected void doGet(HttpServletRequest request,
                             HttpServletResponse response) throws ServletException, IOException {

            String newname=request.getParameter("new");
            String oldname= request.getParameter("old");
            String myradio= request.getParameter("myradio");






            try {
                Class.forName("com.mysql.jdbc.Driver");
                java.sql.Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/gmc","root","root");

// constructs SQL statement
                String sql = "UPDATE `AUSER` SET `AUSERNAME`=?,`AUSERTYPE`=? WHERE `AUSERNAME`=?";
                PreparedStatement statement = con.prepareStatement(sql);
                statement.setString(1, newname);
                statement.setString(2, myradio);

                statement.setString(3, oldname);


                statement.executeUpdate();
   // sets the message in request scope

                    request.setAttribute("Status", "Updated Succesfully");



            }

            catch (Exception ex) {
                ex.printStackTrace();
            }

        }
    }

%>




</body>

前两种形式工作正常,唯一的问题是单击更新后,我的表db中没有任何更改。我做错什么了吗?

我怀疑与updatetable.jsp

有关

0 个答案:

没有答案