使用JSP(Eclipse)将数据插入数据库(XAMPP,MySQL)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用JSP(Eclipse)将数据插入数据库(XAMPP,MySQL)相关的知识,希望对你有一定的参考价值。

所以我试图将JSP表单中的(插入)记录注册到数据库(XAMPP,mysql)。详情如下:

register.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

    <form action = "RegisterController" method = "post">
        <input type = "text" name = "email" placeholder = "Your e-mail"><br><br>
        <input type = "text" name = "firstname" placeholder = "First name"><br><br>
        <input type = "text" name = "lastname" placeholder = "Last name"><br><br>
        <input type = "text" name = "address" placeholder = "Home address"><br><br>
        <input type = "text" name = "phone" placeholder = "Your phone number"><br><br>
        <input type = "password" name = "password"><br><br>
        <input type = "submit" name = "submit" value = "Register">
    </form>

</body>
</html>

register controller.Java

package cms.com;

import java.io.IOException;


import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class RegisterController
 */
public class RegisterController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterController() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

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

        try{

            String email = request.getParameter("email");
            String firstname = request.getParameter("firstname");
            String lastname = request.getParameter("lastname");
            String address = request.getParameter("address");
            String phone = request.getParameter("phone");
            String password = request.getParameter("password");
            String sql = "insert into users(email,firstname,lastname,address,phone,password) values(?,?,?,?,?,?)";
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cms","root","");
            PreparedStatement pst = conn.prepareStatement(sql);

            pst.setString(1,email);
            pst.setString(2,firstname);
            pst.setString(3,lastname);
            pst.setString(4,address);
            pst.setString(5, phone);
            pst.setString(6, password);

            pst.executeUpdate();
            PrintWriter out = response.getWriter();
            out.println("You have successfully registered!");

        }
        catch(ClassNotFoundException e){
            e.printStackTrace();
        }
        catch(SQLException e){
            e.printStackTrace();
        }
     }

}

因此,当我在localhost上运行并输入详细信息时,它只显示一个空页面,并且该表没有任何更新.Debugging显示没有错误以及运行时。我正在使用Eclipse Luna和Dynamic Module 2.5以及Apache Tomcat 7.0。

答案

你必须在项目中添加Mysql jar,你必须在Web.xml文件中映射RegisterController文件(WebContentWEB-INFWeb.xml

并检查Register.jsp中'form action'的路径和Web.xml中的'url-pattern'两者必须相同。

以上是关于使用JSP(Eclipse)将数据插入数据库(XAMPP,MySQL)的主要内容,如果未能解决你的问题,请参考以下文章

现在接手一个java项目 批量导入模块,需提供jsp页面上excel导入功能 求设计

jsp中插入图片却显示不出来

jsp插入数据库乱码 中文的参数怎么处理

无法使用jsp将数据插入mysql

DateTime 未使用 jsp 插入数据库

如何将数据从 JSP 插入 MySQL 数据库?