将用户记录插入MYSQL表

时间:2019-02-07 03:04:34

标签: java mysql jsp servlets

我正在尝试创建一个注册页面供用户注册新帐户。 我正在使用带注册表格的signup.jsp页面 然后将其连接到RegisterServlet.java(获取表单参数并将参数连接到DBConnection.java后插入数据库中) DBConnection包含连接到mysql数据库的try {}。

我尝试了多种注册用户的方式,但是用户信息从未插入数据库。

signup.jsp:

<form action="RegisterServlet" method="post" onsubmit="return validate()">
                                        <div class="row">
                                            <div class="col-lg-6 col-md-6">
                                                <input type="text" placeholder="First Name" name="fname" class="form-control" />
                                            </div>
                                            <div class="col-lg-6 col-md-6">
                                                <input type="text" placeholder="Last Name" name="lname" class="form-control" />
                                            </div>
                                        </div>
                                        <div>
                                            <input type="text" placeholder="User Name" name="username" class="form-control" />
                                        </div>
                                        <div class="row">
                                            <div class="col-lg-6 col-md-6">
                                                <input type="password" placeholder="Password" name="password" class="form-control" id="pass" name="pass" />
                                            </div>
                                            <div class="col-lg-6 col-md-6">
                                                <input type="password" placeholder="Retype Password" name="confirm_password" class="form-control" id="pass2" name="pass2" />
                                            </div>
                                            <div class="col-lg-6 col-md-6">
                                                <%=(request.getAttribute("errMessage") == null) ? ""
                                                : request.getAttribute("errMessage")%>
                                            </div>
                                        </div>
                                        <div class="pull-left"><button type="submit" class="btn btn-primary">Sign Up</button></div>
                                     </form>

RegisterServlet.java:

@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public RegisterServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.sendRedirect("login.jsp");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     try
        {
        String fname = request.getParameter("fname");
        String lname = request.getParameter("lname");
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        DBConnection db = new DBConnection();
        Connection con = db.getCon();


            Statement stmt = con.createStatement();
            stmt.executeUpdate("insert into user (fname, lname, username, password)values('"+fname+"','"+lname+"','"+username+"','"+password+"')");
            System.out.println("data inserted sucessfully");
            response.sendRedirect("login.jsp");
        } catch (SQLException e)
        {
            e.printStackTrace();
        }       


    }

}

DBConnection.java


public class DBConnection {

    public Connection con;

    public Connection getCon(){
        try {
        Class.forName("com.mysql.jdbc.Driver");
        con =  DriverManager.getConnection("jdbc:mysql://localhost:3306/cultureexchange", "root", "");
        } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
        return con;
        }
}

Sql用户表:

fname    lname    username    password
varchar  varchar  varchar     varchar

我的login.jsp有效,因此必须与数据库建立连接, 提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

尝试更改这些行

 Statement stmt = con.createStatement();
            stmt.executeUpdate("insert into user (fname, lname, username, password)values('"+fname+"','"+lname+"','"+username+"','"+password+"')");

 PreparedStatement stmt = con.prepareStatement("insert into user (fname, lname, username, password)values(?,?,?,?)");
            ps.setString(1, fname);
            ps.setString(2, lname);
            ps.setString(3, username);
            ps.setString(4, password);
            ps.executeUpdate();

并确保在WEB-INF下的 lib 文件夹中具有 mysql-j-connector