Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Jquery表单数据插入MySQL数据库_Java_Jquery_Mysql_Database_Servlets - Fatal编程技术网

Java Jquery表单数据插入MySQL数据库

Java Jquery表单数据插入MySQL数据库,java,jquery,mysql,database,servlets,Java,Jquery,Mysql,Database,Servlets,我试图使用HTML表单写入MySql数据库,但使用Jquery无法让servlet写入数据库。我看不出我做错了什么。下面是servlet的代码和HTML文件的代码 Servlet: package com.david.servlets; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import jav

我试图使用HTML表单写入MySql数据库,但使用Jquery无法让servlet写入数据库。我看不出我做错了什么。下面是servlet的代码和HTML文件的代码

Servlet:

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>

myForm
$(“#提交”)。单击(函数(){
Var ID=$(“#ID”).val();
Var FirstName=$(“#FirstName”).val();
Var LastName=$(“#LastName”).val();
$.post(“myForm”,“form”).serialize(),insertCallback);
});
函数insertCallback(结果){
如果(结果=“成功”){
警报(“已添加记录!”);
}否则{
警报(“无法添加记录!”);
}
}
身份证件:
名字:
姓氏:
(1)您需要给提交按钮一个
id=“submit”
,它才能与您的单击功能一起工作


(2) 将
Var
更改为
Var

阅读关于servlet和HTML的详细信息。您的大多数代码都太笨拙了。谢谢,这两个都已修复,但仍然无法按预期工作。尽管更正后JavaScript仍然有效,但我怀疑OP的servlet是否会被调用。@user2358627,请遵循skuntsel的建议。我可以证明表单正在被序列化,回调正在被调用(),但我不能帮助处理其他事情。顺便说一句,你不需要浪费资源的可变分配;只需序列化表单并发布即可。@DerekHenderson谢谢,我可以序列化了,我现在只是对我的servlet有点问题。