Connect to MySQL using JBDC with Java EE applying MVC design pattern

I'm using Eclipse Java EE IDE for Java Web Developers and Tomcat v7.0. I configured the Server and associated the dynamic web project with tomcat. I'm working locally (localhost).

I have my Connection class, which is this:

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;
        }
    }

}

In my index.jsp file I have a form for users can log on. Depending on whether the log in is correct or not, I want to redirect to one View (Welcome.jsp) or another View (Denegate.jsp) through the Controller called ControladorLogin

So, in my ListaDeQueries class I have the login method, which is this

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;
        }

    }

}

Then in my Servlet ControladorLogin I call such method, like this:

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);
        }
    }

This is my little form in 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>

The problem is that I'm getting an ClassNotFoundException for MySQL driver: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver, but I added the library correctrly. In project's properties > Java Build Path > Libraries > Add External JARs and then added the jar that comes in this file: http://www.mysql.com/downloads/connector/j/

This is the complete 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)

Answers


Though you have mentioned about adding jar. But many times in the past, I have experienced java.lang.ClassNotFoundException if jar is not at physical location.

I would suggest to place mysql-jdbc.jar at physical location to /WEB-INF/lib directory of your project.Then eclipse will take care for the rest.

It is also good practice to have third party dependencies under /WEB-INF/lib directory.


Need Your Help

Unwanted spacing between divs inside Html table column

html css

I can't get rid of the space between each div I'm presenting in this HTML table, in my "ArticlesBar" column. In this column I have a div for the scroll bar and 3 other divs for header body and date...

Static factory methods and mocking

java unit-testing testing tdd

How do you reconcile using static factory methods and mocking?