Java servlet无法更新数据库

时间:2017-04-12 01:46:27

标签: java sql servlets

此Web应用程序允许用户更新名为Phonebook的数据库表的名称和电话。

电话簿(UID,姓名,电话)

但是我的代码无法更新它。它显示以下消息。

ERROR: The record is failed to update.

对于这一行,抱歉我没有在此处提供详细信息以保护自己。当我运行实际代码时,包含了信息,我可以成功连接到数据库。

con = DriverManager.getConnection(";databaseName=", "", "");

这是我的更新方法的代码。我真的不知道为什么它运作不好。我希望有人能告诉我哪一行写错了。谢谢!

private void doUpdateEntry(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
     response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Phonebook</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Phonebook (Update)</h1>");
        out.println("<div style='width:600px'>");
        out.println("<fieldset>");

        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String uid = request.getParameter("uid");
        Connection con = null;
        out.println("UID: "+uid);


        if (name != null && !name.equalsIgnoreCase("") &&
            phone != null && !phone.equalsIgnoreCase("") &&
                uid != null ) {

            // Register the JDBC driver, open a connection
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            con = DriverManager.getConnection(";databaseName=", "", "");



           out.println("UID2: "+uid);
            // Create a preparedstatement to set the SQL statement           
            PreparedStatement pstmt = con.prepareStatement("UPDATE [phonebook] SET [Name] = ?, [Phone] = ? WHERE [UID] = ?");
            pstmt.setString(1, name);
            pstmt.setString(2, phone);
            pstmt.setString(3, uid);
            // execute the SQL statement
            int rows = pstmt.executeUpdate();
            out.println("Rows: "+rows);
            if (rows > 0) {
                out.println("<legend>The record is sucessfully updated.</legend>");
                // display the information of the record just added including UID
                Statement stmt = con.createStatement();
                ResultSet rs = stmt.executeQuery("SELECT @@IDENTITY AS [@@IDENTITY]");
                if (rs != null && rs.next() != false) {
                        out.println(rs);
                        out.println("<p>UID: " + rs.getInt(1) + "</p>");

                        rs.close();
                }
                if (stmt != null) {
                        stmt.close();
            }

                out.println("<p>Name:" + this.htmlEncode(name) + "</p>");
                out.println("<p>Phone Number:" + phone + "</p>");
            }
            else {
                out.println("<legend>ERROR: The record is failed to update.</legend>");
            }
        }
        else {
            if (name == null) {
                name = "";
            }
            if (phone == null) {
                phone = "";
            }       


            out.println("<legend>Please fill in the form</legend>");
            out.println("<form method='POST' action='" + request.getRequestURI() + "'>");
            out.println("<input name='action' type='hidden' value='update' />");

            out.println("<p>Name:");
            out.println("<input name='name' type='text' size='25' maxlength='255' value='" + this.htmlEncode(name) + "' /></p>");
            out.println("<p>Phone Number:");
            out.println("<input name='phone' type='text' size='8' maxlength='8' value='" + phone + "' /></p>");
            out.println("<input name='uid' type='hidden' value='"+uid+"' />");
            out.println("<input type='submit' value='Update!' />");
            out.println("</form>");
        }
        out.println("<br/><a href='" + request.getRequestURI() + "'>Back to Phonebook Directory</a>");
        out.println("</fieldset>");
        out.println("</div>");
        out.println("</body>");
        out.println("</html>");
    } catch (ClassNotFoundException e) {
   out.println("<div style='color: red'>" + e.toString() + "</div>");
    } catch (SQLException e) {
        out.println("<div style='color: red'>" + e.toString() + "</div>");
    } finally {
        out.close();
    }



}

0 个答案:

没有答案
相关问题