使用注册表单和servlet Java插入查询数据库MySQL

时间:2015-12-19 11:03:44

标签: java mysql servlets jdbc

我在使用servlet java访问数据库时遇到问题。我想添加一个html注册表单的用户。 这是html:

<form name="registration-form" action="RegisterUser"  method="post">
    <div class="form-group has-feedback">
        <label class="control-label">Inserisci nome*</label>
        <input type="text" class="form-control" name="name" id="inputName" placeholder="Nome">
        <span class="glyphicon  form-control-feedback"></span>
    </div>
    <div class="form-group has-feedback">
        <label class="control-label">Inserisci cognome*</label>
        <input type="text" class="form-control" name="surname" id="inputLastname" placeholder="Cognome">
        <span class="glyphicon  form-control-feedback"></span>
    </div>
    <div class="form-group has-feedback">
        <label class="control-label">Inserisci email*</label>
        <input type="email" class="form-control" name="email" id="inputEmail" placeholder="Email">
        <span class="glyphicon  form-control-feedback"></span>
    </div>
    <div class="form-group has-feedback">
        <label class="control-label">Inserisci password (compresa tra 6 e 30 caratteri)*</label>
        <input type="password" class="form-control" name="password" id="inputFirstPassword" placeholder="Password">
        <span class="glyphicon  form-control-feedback"></span>
    </div>
    <div class="form-group has-feedback">
        <label class="control-label">Reinserisci password*</label>
        <input type="password" class="form-control" id="inputSecondPassword" placeholder="Password">
        <span class="glyphicon  form-control-feedback"></span>
    </div>
    <button id="submitBtn" class="btn btn-lg btn-primary btn-block" >Registrati!</button>
</form>

这是java类(InsertUser.java):

import java.sql.*;
public class InsertUser {
public static void main(String name, String surname, String email, String password) {
    PreparedStatement insert = null;
    String queryString = "INSERT INTO Utente(Nome, "
                        + "Cognome, Email, Password) VALUES (?, ?, ?, ?)";
    Connection conn = null;
    try {
        new com.mysql.jdbc.Driver();
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        //Class.forName("org.gjt.mm.mysql.Driver").newInstance();
        String connectionUrl = "jdbc:mysql://localhost:3306/NoteDB";
        String connectionUser = "root";
        // String connectionPassword = "root";
        conn = DriverManager.getConnection(connectionUrl, connectionUser, "");
        conn.setAutoCommit(false);
        insert = conn.prepareStatement(queryString);

        insert.setString(1, name);
        insert.setString(2, surname);
        insert.setString(3, email);
        insert.setString(4, password);

        insert.executeUpdate();
        conn.close();

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
    }
}

这是servlet(RegisterUser.java)

  import java.io.IOException;
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 RegisterUser
 */
@WebServlet("/RegisterUser")
public class RegisterUser extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterUser() {
        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

        String name = request.getParameter("name");
        String surname = request.getParameter("surname");
        String email = request.getParameter("email");
        String password = request.getParameter("password");

        //InsertUser myUser = new InsertUser();
        InsertUser.main(name, surname, email, password);

    }



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

}

这是我得到的错误: error page

对不起,这是一个很长的问题,但我绝望了:(谢谢大家!

编辑:在这张图片中你可以看到mysql-connector.jar:mysqlconnector

1 个答案:

答案 0 :(得分:0)

正如Ramanlfc所提到的,你必须在你的类路径中添加mysql驱动程序。 您可以通过ide本地执行此操作,也可以使用构建管理器,如maven,gradle或ant。 在maven中,你的pom.xml看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>your.app</groupId>
<artifactId>mysql-test</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.38</version>
    </dependency>
</dependencies>