servlet在表单发布后没有向mysql数据库提交记录

时间:2014-06-10 16:21:40

标签: java mysql servlets jdbc

我使用ajaxservlet提交数据。 ajax调用似乎是成功的。我相信我正确捕获发布的数据并使用jdbc将数据提交到mysql,但记录没有添加,并且控制台中没有异常错误消息。

继承servlet代码:

@WebServlet(description = "submits comment to mysql database", urlPatterns = { "/CommentController" })
public class CommentController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    private static Connection conn; 
    private PreparedStatement prep;
    private String sql;
    private ResultSet rs;
    private static final String driver = "com.mysql.jdbc.Driver", host = "jdbc:mysql://host/", dbName = "name", username = "user", password = "pw";

    /**
     * @see HttpServlet#HttpServlet()
     */
    public CommentController() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String commentName = request.getParameter("name");
        String comment = request.getParameter("comment");
        int postID = Integer.parseInt(request.getParameter("postID"));

        try{
            Class.forName(driver);
            Connection conn = (Connection) DriverManager.getConnection(host + dbName, username, password);
            sql="INSERT INTO crm_comments (comment_name, comment_content, comment_date, post_id) VALUES (?, ?, NOW(), ?)";
            prep = conn.prepareStatement(sql);
            prep.setString(1, commentName);
            prep.setString(2, comment);
            prep.setInt(3, postID);
            rs = prep.executeQuery();

        }catch(Exception e){
            e.printStackTrace();
        }
        System.out.println("yes");
    }
}

html表单:

<form id="commentForm" name="commentForm" action="http://localhost:8080/chrismepham/CommentController" method="POST">
    <input type="text" name="name" placeholder="Your name" required="required">
    <textarea name="comment" placeholder="Enter your comment here (max. 500 characters)" required="required"></textarea>
    <input type="hidden" name="catcher" id="catcher">
    <input type="hidden" name="postID" id="postID" value="<%= postID %>">
    <input type="submit" name="submit" id="commentSubmit" value="submit">
</form>

AJAX:

$("#commentSubmit").click(function(e){
    var postData = $("#commentForm").serializeArray();
    var formURL = $("#commentForm").attr("action");
    //var catcher = $("#catcher").val();
        $.ajax(
        {

            type: "POST",
            url : formURL,
            data : postData,
            success:function(data, textStatus, jqXHR) 
            {
                $("#commentFormWrap").html("<p>Success</p>");
            },
            error: function(jqXHR, textStatus, errorThrown) 
            {
                $("#commentFormWrap").html("<p>ss"+errorThrown+textStatus+jqXHR+"</p>");
            }
        });
    e.preventDefault(); //STOP default action
    $("#commentForm").hide();
});

有人可以告诉我这里可能出现的问题是阻止数据记录在MySQL中吗?

4 个答案:

答案 0 :(得分:1)

将executeQuery()更改为executeUpdate(),如果row count成功则返回update,否则zero, 调用

conn.commit()

答案 1 :(得分:0)

executeQuery()更改为executeUpdate()

注意:您的代码中没有关闭闭包

答案 2 :(得分:0)

请检查以下内容:

  1. servlet中的数据是在servlet中正确接收的。
  2. 检查自动提交的数据库属性。
  3. 检查数据库连接属性:主机,驱动程序,数据库名和数据库凭据。
  4. 您还可以尝试executeupdate并在代码中尝试连接提交。

答案 3 :(得分:0)

变量主机没有定义端口 尝试将主机更改为&#34; jdbc:mysql:// localhost:[您的mysql服务器端口] / &#34;

还要确保您的用户名和密码配置正确。