0

Am new in JSP and the IntelliJ IDE am experiencing an error java.lang.ClassNotFoundException: com.mysql.jdbc.Driver when i try to login inside my Application. Am using IntelliJ as my IDE while developing a JSP project. i tried with mysql-connector-java version 6.0.4, 5.1.28, 5.1.23. but still it is not working and remind the same error.

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>groupId</groupId>
    <artifactId>SmartRec</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient -->
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.3</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.4</version>
        </dependency>

    </dependencies>

</project>

EmployeeRegistration.java

public class EmployeeRegistration extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        String n=request.getParameter("name");
        String l=request.getParameter("address");
        String cwp = request.getParameter("cwposition");
        String ph=request.getParameter("phone");
        String m=request.getParameter("mail");
        String psw=request.getParameter("password");

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

            System.out.println("conect");
            PreparedStatement ps=con.prepareStatement("insert into employee(name,address,cwposition,phone,email,password) values(?,?,?,?,?,?)");

            ps.setString(1,n);
            ps.setString(2,l);
            ps.setString(3,cwp);
            ps.setString(4,ph);
            ps.setString(5,m);
            ps.setString(6,psw);

            int i=ps.executeUpdate();
            if(i>0){
                String name = n;
                request.setAttribute("message", name);

            }





        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        out.close();
    }
davethecoder
  • 3,856
  • 4
  • 35
  • 66
jorge
  • 1
  • 1
  • Are you really getting a Class not found exception? You should be getting this: `Loading class 'com.mysql.jdbc.Driver'. This is deprecated. The new driver class is 'com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary. ` – Yoshua Nahar Sep 25 '17 at 09:25
  • Can you add the entire exception you are getting? – Yoshua Nahar Sep 25 '17 at 09:26
  • I got the exception like this. java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1285) – jorge Sep 25 '17 at 10:04
  • You are using Tomcat to run your project right? – Yoshua Nahar Sep 25 '17 at 10:57
  • yes. i am using Apache Tomcat/8.5.16 – jorge Sep 25 '17 at 11:00
  • 1
    Possible duplicate of [How to install JDBC driver in Eclipse web project without facing java.lang.ClassNotFoundexception](https://stackoverflow.com/questions/2353141/how-to-install-jdbc-driver-in-eclipse-web-project-without-facing-java-lang-class) – Yoshua Nahar Sep 25 '17 at 11:46

1 Answers1

0

Check out this question. It is related to yours.

They basically say to:

  1. Remove the dependency from Maven.
  2. Manually download it from here.
  3. Add the jar to your /WEB-INF/lib or $TOMCAT\lib folder.

Also for future reference check the official Tomcat documentation at: http://tomcat.apache.org/tomcat-8.0-doc/jndi-datasource-examples-howto.html

Yoshua Nahar
  • 1,304
  • 11
  • 28