2017-04-10 32 views
0
package mycode; 
import java.sql.* 
import java.io.IOException; 
import java.io.PrintWriter; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

public class Login extends HttpServlet { 

private static final long serialVersionUID = -4546189621945422719L; 

String URL = "jdbc:mysql://127.0.0.1"; 
String USER = "root"; 
String PASS = "1234"; 

@Override 
protected void doGet(HttpServletRequest req,HttpServletResponse res) 
     throws IOException,ServletException{ 

} 

public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { 
    String username = req.getParameter("username"); 
    String password = req.getParameter("password"); 
    res.setContentType("text/html"); 
    PrintWriter out = res.getWriter(); 

    try { 
     Connection con = DriverManager.getConnection(URL, USER, PASS); 

     String sql = "INSERT INTO cloud.cloud_table (Username,Password)" + 
       "VALUES (?, ?)"; 
     PreparedStatement pst = con.prepareStatement(sql); 
     pst.setString(1, username); 
     pst.setString(2, password); 
     pst.executeUpdate(); 


     out.println("<html><body>"); 
     out.println("THANKS FOR CREATING AN ACCOUNT"); 
     out.println("</body></html>"); 
    } 
    catch (SQLException ex) { 
     ex.printStackTrace(); 
    } 

    } 
} 

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 
<display-name>Login</display-name> 
<servlet> 
    <servlet-name>Login</servlet-name> 
    <servlet-class>mycode.Login</servlet-class> 
</servlet> 

<servlet-mapping> 
    <servlet-name>Login</servlet-name> 
    <url-pattern>/login</url-pattern> 
</servlet-mapping> 
<welcome-file-list> 
<welcome-file>index.html</welcome-file> 
<welcome-file>index.htm</welcome-file> 
<welcome-file>index.jsp</welcome-file> 
<welcome-file>default.html</welcome-file> 
<welcome-file>default.htm</welcome-file> 
<welcome-file>default.jsp</welcome-file> 

的Tomcat/Java的 - 插入到一个MySQL数据库


+----------+----------+ 
| Username | Password | 
+----------+----------+ 
|   |   | 
+----------+----------+ 

<form action="login" method="GET"> 
    <div> 
     <h4>Please enter your password and username</h4> 
    </div> 
    <div> 
     Username <input name="username" type="text" /> 
    </div> 
    <div> 
     Password <input name="password" type="text" /> 
    </div> 
    <div> 
     <input type="submit" value="Submit" /> 
    </div> 
</form> 

用java和tomcat,我有一个简单的表格,需要在一个用户名中d密码。我希望能够从表单中检索用户名和密码并将其插入到mysql数据库中。但是,现在单击表单上的提交按钮后,什么都不会发生,数据库也不会更新。数据库连接工作正常,所以我不知道现在的问题是什么。任何帮助,将不胜感激。

+0

你能展示简单的表单代码吗? – alayor

+0

如果您点击提交并且什么也没有发生,您的html表单可能不工作。 – f1sh

+0

@alayor我已经添加了表单 –

回答

3

您正在从HTML表单调用GET方法。

将您的操作更改为POST。

<form action="login" method="POST"> 
+0

谢谢你的回复,不能相信我犯了这样一个愚蠢的错误。无论如何,我现在已经纠正了,但我得到“java.sql.SQLException:找不到合适的驱动程序为jdbc:mysql://127.0.0.1”错误 –

+0

您需要将MySQL驱动程序添加到类路径。我建议你自己调查一下这个问题,并打开一个你找不到任何东西的新问题。 – alayor