0

我正在为 Java Web Developers 和 Tomcat v7.0 使用 Eclipse Java EE IDE。我配置了服务器并将动态 Web 项目与 tomcat 相关联。我在本地工作(本地主机)。

我有我的 Connection 课程,这是:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Conexion {

    private static Connection conexion = null;

    private Conexion() {

    }

    public static Connection getInstancia() {
        if (conexion == null) {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                conexion = DriverManager.getConnection(
                        "jdbc:mysql://localhost/Esquema_VentaLibros","gustavo", "123581321");

            } catch (SQLException sqlex) {
                sqlex.printStackTrace();
            } catch(ClassNotFoundException cnfex) {
                cnfex.printStackTrace();
            }
            return conexion;
        }
        else {
            return conexion;
        }
    }

}

在我的 index.jsp 文件中,我有一个用户可以登录的表单。根据登录是否正确,我想通过名为的控制器重定向到一个视图(Welcome.jsp)或另一个视图(Denegate.jsp)ControladorLogin

所以,在我的ListaDeQueries课堂上,我有登录方法,就是这个

public class ListaDeQueries {

    private static PreparedStatement pst;
    private static String query = "";

    public static boolean loguear(String usr, String pass) {
        query = "select * from Usuario where username = ? and password = ?";

        try {
            pst = Conexion.getInstancia().prepareStatement(query);
            pst.setString(1, usr);
            pst.setString(2, pass);
            pst.executeQuery();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }

    }

}

然后在我的 Servlet 中ControladorLogin调用这样的方法,如下所示:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        if(ListaDeQueries.loguear(request.getParameter("usuario"), request.getParameter("clave"))) {
            request.getRequestDispatcher("Welcome.jsp").forward(request, response);
        }
        else {
            request.getRequestDispatcher("Denegate.jsp").forward(request, response);
        }
    }

这是我的小表格index.jsp

<form method="post" action="ControladorLogin">
        Usuario: <input type="text" name="usuario"/> <br/>
        Clave: <input type="password" name="clave"/> <br/>
        <input type="submit" value="Entrar"/>
    </form>

问题是我得到了 MySQL 驱动程序的 ClassNotFoundException :java.lang.ClassNotFoundException: com.mysql.jdbc.Driver,但我正确地添加了库。在项目的属性 > Java 构建路径 > 库 > 添加外部 JAR,然后添加此文件中的 jar: http ://www.mysql.com/downloads/connector/j/

这是完整的 StackTrace:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at edu.itla.diaz.modelo.Conexion.getInstancia(Conexion.java:18)
    at edu.itla.diaz.modelo.ListaDeQueries.loguear(ListaDeQueries.java:15)
    at edu.itla.diaz.controladores.ControladorLogin.doPost(ControladorLogin.java:30)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
04/12/2012 01:04:45 PM org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() para servlet edu.itla.diaz.controladores.ControladorLogin lanzó excepción
java.lang.NullPointerException
    at edu.itla.diaz.modelo.ListaDeQueries.loguear(ListaDeQueries.java:15)
    at edu.itla.diaz.controladores.ControladorLogin.doPost(ControladorLogin.java:30)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
4

1 回答 1

1

尽管您提到了添加 jar。但是过去很多次,我都经历过java.lang.ClassNotFoundExceptionjar 是否不在物理位置。

我建议将mysql-jdbc.jar物理位置放置在/WEB-INF/lib您的项目目录中。然后 Eclipse 将负责其余的工作。

将第三方依赖项放在/WEB-INF/lib目录下也是一个好习惯。

于 2012-04-13T03:37:45.153 回答