ClassNotFoundException com.mysql.jdbc.Driver JAVA JSP

0

Problem:

I have a method of type ArrayList that returns an object of the same, but when I invoke it in the JSP it gives me the following error:

javax.servlet.ServletException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

I have the following method:

public ArrayList<Producto> con()throws SQLException, ClassNotFoundException {

    qry = "SELECT * FROM productos";
    st = con.conectar().createStatement();
    rst = st.executeQuery(qry);
    while (rst.next()){
        list.add(0,rst.getInt("ID_PRODUCTO"));
        list.add(1,rst.getString("COD_COLOR"));
        list.add(2,rst.getString("COD_SIZE"));
        list.add(3,rst.getString("NOMBRE"));
        list.add(4,rst.getDouble("PRECIO"));
        list.add(5,rst.getString("IMAGEN"));
    }
    con.conectar().close();
    return list;
}

and in the JSP I call it from the following photo:

ProductoM prod;
prod = new ProductoM();
ArrayList arrayprod = prod.con();

That's when I call the method, but I get the error that it can not find the mysql connector driver.

This is my connection class:

public class Coneccion {

public Connection conectar() throws SQLException, ClassNotFoundException {

    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/runner","root","");
    return con;

}

}

Create a class and I made a main where I called the data of the database and everything worked well, I reloaded the connector, but nothing, in the JSP I still get the error that the Driver can not be found.

Note: I'm using Itellij IDE, the connector: mysql-connector-java-5.1.45-bin.jar

Complete error:

Exception

org.apache.jasper.JasperException: An exception occurred processing JSP        page [/catalogo.jsp] at line [17]

14: <%
15:     ProductoM prod;
16:     prod = new ProductoM();
17:     ArrayList arrayprod = prod.con();
18:     int cont = 0;
19: %>
20: <section>


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:584)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:466)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Root Cause

javax.servlet.ServletException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:909)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:838)
org.apache.jsp.catalogo_jsp._jspService(catalogo_jsp.java:184)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Root Cause

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1291)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119)
java.base/java.lang.Class.forName0(Native Method)
java.base/java.lang.Class.forName(Class.java:292)
model.Coneccion.conectar(Coneccion.java:9)
model.ProductoM.con(ProductoM.java:94)
org.apache.jsp.catalogo_jsp._jspService(catalogo_jsp.java:126)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    
asked by Francisco Castle 05.04.2018 в 09:28
source

1 answer

0

The JSP that you run has been deployed in a container (Apache Tomcat, maybe?), and surely there is no added jar with the driver classes. Find the lib folder inside your container and put your JAR there. Another option is to add the JAR to your WAR file, make sure you have the driver as a dependency of your project.

If you are learning to use JSP with these tests, I recommend you do not use Java code inside a JSP , the code is complicated to maintain or debug.

If that code is part of a real application, you may have a lot of problems if you create a connection for each query, you should use a connection pool .

    
answered by 05.04.2018 в 09:45