【问题标题】:Jquery Form Data Insert MySQL DatabasejQuery 表单数据插入 MySQL 数据库
【发布时间】:2013-05-01 12:51:16
【问题描述】:

我正在尝试使用 HTML 表单写入 MySql 数据库,但使用 Jquery 我无法让 servlet 写入数据库。我没有看到任何明显的我做错了。这是 servlet 的代码和 HTML 文件的代码。

小服务程序:

package com.david.servlets;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;


/**
 * Servlet implementation class myForm
 */
public class myForm extends HttpServlet {

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
        {
              //Get parameters
            String id = request.getParameter("ID");
            String fname = request.getParameter("FirstName");
            String lname = request.getParameter("LastName");


            //Get Connection
            try {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("Found a driver");
            Connection dbConnect = null;
            try {
                dbConnect = getConnection("localhost", 7001);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (NamingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            System.out.println("Made a connection");


                //Create Query
            String query = "INSERT INTO test.customer (ID, FirstName, LastName) " + 
                    "VALUES (" + id + ", " + fname + ", " + lname + ")";
            PreparedStatement dbStatement = null;
            try {
                dbStatement = dbConnect.prepareStatement(query);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //Execute Query
            try {
                dbStatement.executeUpdate(query);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            //close connection
            try {
                dbStatement.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                dbConnect.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }





public Connection getConnection(String server, int port)
        throws SQLException, NamingException {
    Context ctx = null;
    Hashtable ht = new Hashtable();
    ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
    ht.put(Context.PROVIDER_URL, "t3://"+server+":"+port);
    ctx = new InitialContext(ht);
    DataSource ds = (javax.sql.DataSource) ctx.lookup ("localmysql");
    Connection conn =  ds.getConnection();
    //conn.setAutoCommit( true );
    return conn;
}    





}

HTML:

<html>
<head>
<title>myForm</title>
<script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.js" type="text/javascript"></script>
<script type="text/javascript">

    $("#submit").click(function () {
        Var ID = $("#ID").val();
        Var FirstName = $("#FirstName").val();
        Var LastName = $("#LastName").val();
        $.post("myForm", $("form").serialize(), insertCallback);
    });

    function insertCallback(result) {
        if (result == "success") {
            alert("Record added!");
        } else {
            alert("Could not add Record!");
        }
    }

</script>
</head>
<body>
<form action="myForm" method="POST">
ID: <input type="number" name="ID">
FirstName: <input type="text" name="FirstName"> <br>
LastName: <input type="text" name="LastName">  <br>
<input type="button" value="submit">
</form>
</body>
</html>

【问题讨论】:

  • 阅读有关 servlet 和 HTML 的一般信息。你的大部分代码都太笨拙了。

标签: java jquery mysql database servlets


【解决方案1】:

(1) 你需要给你的提交按钮一个id="submit" 以便它与你的点击功能一起工作。

(2) 将Var 更改为var

【讨论】:

  • 这两个都已修复,谢谢,但这仍然无法按预期工作。
  • 尽管 JavaScript 在更正后仍然有效,但我怀疑 OP 的 servlet 是否会被调用。
  • @user2358627,听从 skuntsel 的建议。我可以证明表单正在序列化并且正在调用回调 (jsfiddle.net/9ZVTn),但我无法帮助解决其他问题。顺便说一句,您不需要通过可变分配浪费资源;只需序列化表单并发布即可。
  • @DerekHenderson 谢谢,我可以序列化了,我现在的 servlet 有问题。
猜你喜欢
  • 1970-01-01
  • 2019-01-12
  • 1970-01-01
  • 1970-01-01
  • 2014-06-21
  • 2014-04-04
  • 1970-01-01
  • 1970-01-01
  • 2013-11-30
相关资源
最近更新 更多