简体   繁体   English

Tomcat / Java-插入mysql数据库

[英]Tomcat/Java - Insert into a mysql database

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>


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

Using java and tomcat, I have a simple form that takes in a username and password. 使用java和tomcat,我有一个简单的表单,可以输入用户名和密码。 I want to be able to retrieve the username and password from the form and insert it into a mysql database. 我希望能够从表单中检索用户名和密码,并将其插入到mysql数据库中。 However, right now after clicking the submit button on the form, nothing happens and the database is not updated. 但是,现在在单击表单上的“提交”按钮之后,什么也没有发生,并且数据库未更新。 The database connection is working fine so I am not sure what the problem is right now. 数据库连接工作正常,所以我不确定现在是什么问题。 Any help would be appreciated. 任何帮助,将不胜感激。

You are calling your GET method from the HTML form. 您正在从HTML表单中调用GET方法。

Change your action to POST. 将操作更改为POST。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM