Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I am getting class not found exception for Class.forName("com.mysql.jdbc.Driver"); [duplicate]

I am developing an web app Using : eclipse IDE(Marse), Java 8, Apache tomcat 8, MySQL. My code for user registration is as follows:

System.out.println("Connecting....");
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("Class found....");
            String url="jdbc:mysql://localhost:3306/db";
            String user="root";
            String password="root";
            System.out.println("success");
            Connection conn = DriverManager.getConnection(url,user,password);
            String query = "insert into login (username,password,emailid,country,city) values (?,?,?,?,?)";
            PreparedStatement ps = conn.prepareStatement(query);
            ps.setString(1,b.getUsername());
            ps.setString(2,b.getPassword());
            ps.setString(3,b.getEmailid());
            ps.setString(4,b.getCountry());
            ps.setString(5,b.getCity());
            int count=ps.executeUpdate();
            ps.close();
            conn.close();

My code is also correct and also I added mysql-connector-java-5.1.36 by using Build path>add_external_jar option in eclipse. But still why it gives me such a exception ?

Connecting.... java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1332) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1166) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at model.InfoModel.SaveInfo(InfoModel.java:14) at controller.Register_user.doGet(Register_user.java:35) at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1526) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1482) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Unknown Source)

like image 493
Abhijit Kumbhar Avatar asked Sep 16 '15 08:09

Abhijit Kumbhar


1 Answers

Right click on the Project, go to Properties. Choose Deployment Assembly (if not found, read the update part) from the left pane. Check if Java Build Path Entries are provided. If not, click on Add and include it. If it is already present, click Add to include your connector jar as Archive. Do a Project>Clean, restart your Server.

Update

If the Project is not being Faceted, you will not find this option. You can click on Project Facets, convert to faceted structure and add Dynamic Web Module and related dependencies.

Update

Web Deployment Assembly Problem. This answer is helpful in most cases.

like image 57
James Jithin Avatar answered Sep 30 '22 13:09

James Jithin