2013-05-07 39 views
0

我正在嘗試使用HTML表單寫入MySql數據庫,使用Jquery我無法讓servlet寫入數據庫。我沒有看到任何明顯的表現,我做錯了。這裏是servlet的代碼和HTML文件的代碼。Jquery表單數據插入MySQL數據庫

的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> 
+0

閱讀有關特別是servlet和HTML的一般信息。你的代碼大部分太笨拙了。 – skuntsel 2013-05-07 14:25:00

回答

0

(1)你必須給你的提交按鈕的id="submit"它與您的點擊功能工作。

(2)將Var更改爲var

+0

這兩個都是固定的,謝謝,但這仍然沒有按預期工作。 – imsofnbamf 2013-05-07 14:26:44

+0

儘管修正後JavaScript會有效,但我懷疑OP的servlet會被調用。 – skuntsel 2013-05-07 14:27:58

+0

@skuntsel我做錯了什麼叫這個servlet? – imsofnbamf 2013-05-07 14:29:16