0

I had added mysql-connector-java-8.0.12.jar from build path in Eclipse Jee. I had also registered that class by adding :

Class.forName("com.mysql.jdbc.Driver");

but still it is giving me this error :

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

This is the code of Student.java Servlet.

import javax.servlet.*;
import javax.servlet.http.*;

import java.io.*;
import java.sql.*;

public class Student extends HttpServlet
{

    public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
    {
        // business logic
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();

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

            String sql = "insert into p4 values(?,?,?,?,?,?,?)";
            PreparedStatement ps = con.prepareStatement(sql);

            String enroll = request.getParameter("enroll");
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String gender = request.getParameter("gender");
            String email = request.getParameter("email");
            String mobile = request.getParameter("mobile");
            String address = request.getParameter("address");


            ps.setString(1, enroll.toString());
            ps.setString(2, username.toString());
            ps.setString(3, password.toString());
            ps.setString(4, gender.toString());
            ps.setString(5, email.toString());
            ps.setString(6, mobile.toString());
            ps.setString(3, address.toString());

            ps.executeUpdate();

            ps.close();
        }
        catch(Exception ex) {
            System.out.println("Exception : "+ ex);
            out.println("Error");
        }
    }
    public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
    {
        doGet(request,response);
    }
}

Thanks in advance...

Mike
  • 21
  • 1
  • 5
  • That's the wrong class name if you are using Connector 8.0 or later. – Stephen C Aug 03 '19 at 06:11
  • And it shouldn't be in the **build** path. It should be in the **runtime** classpath, i.e. under WEB-INF/lib in your generated war file. – JB Nizet Aug 03 '19 at 06:12
  • @JBNizet how do i add jar file in WEB-INF/lib...? – Mike Aug 03 '19 at 07:59
  • It depends on how you're building your application. Assuming you're using Eclipse (which is the only tool using the term "build path" AFAIK), you just drop the jar in WebContent/WEB-INF/lib of your web project IIRC. – JB Nizet Aug 03 '19 at 08:01
  • @JBNizet Eclipse dosen't allow me to drop the jar file in WebContent/WEB_INF/lib folder... – Mike Aug 03 '19 at 08:07
  • Then use your file explorer or your command line to put it there. http://help.eclipse.org/kepler/index.jsp?topic=%2Forg.eclipse.wst.webtools.doc.user%2Ftopics%2Ftwplib.html – JB Nizet Aug 03 '19 at 08:12
  • You haven't needed the `Class.forName()` at all since 2007. Just remove it. – user207421 Aug 03 '19 at 08:50

1 Answers1

0

You need to use com.mysql.cj.jdbc.Driver instead of com.mysql.jdbc.Driver while using MySQL connector 8. Please refer the documentation here.

Kavitha Karunakaran
  • 1,340
  • 1
  • 17
  • 32
  • ```java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver``` still getting this error using ```Class.forName("com.mysql.cj.jdbc.Driver").newInstance();``` – Mike Aug 03 '19 at 08:00
  • are you using Tomcat to run the servlet? In that case, you may need to copy the connector to tomcat/lib folder as well. I've seen an error previously while working with tomcat server. – Kavitha Karunakaran Aug 03 '19 at 08:04
  • yes i am using tomcat server v7.0 to run my program... Can you please give me the location of tomcat/lib folder. – Mike Aug 03 '19 at 08:10