简体   繁体   中英

java.lang.ClassNotFoundException: com.mysql.jdbc.driver error with jar file already imported

I have been searching all around for a solution to my problem and the only solution i have found is to add the jar file that i already have. This is done in eclipse EE and is useing tomcat.

I am running a different HTML file which links to this piece of code once a button is pressed, if you want that code feel free to ask, but there is almost nothing on it.

I keep getting the error "java.lang.ClassNotFoundException: com.mysql.jdbc.driver" along with 100 other lines which i'm not sure are conencted.

Here is what i have:

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class HelloForm
 */
@WebServlet("/test5")
public class test5 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public test5() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub

        String url = "jdbc:mysql://localhost:3306/";
        String dbName = "test";
        String driver = "com.mysql.jdbc.driver";
        String userName = "root";
        String password = "games10";

        try {
            Class.forName(driver).newInstance();
            Connection conn = (Connection) `enter code     here`DriverManager.getConnection(url+dbName,userName,password);

            Statement stat = conn.createStatement();
            //stat.execute("CREATE TABLE test (Name CHAR(20))");
            stat.execute("INSERT INTO TEST VALUES('"+request.getParameter("first_name")+"')");

            conn.close();
            System.out.println("Working");

        }

        catch (Exception e) {

            e.printStackTrace();

        }

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

I apolagize if i have posted the code wrong, ive never used this site before and am tired. As i have said i have imported the "mysql-connector-java-5.1.30-bin.jar" file. All help is accepted with open arms and critisism also.

Thank you for your time

Errors in case it's not actually related to the driver:

java.lang.ClassNotFoundException: com.mysql.jdbc.driver at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at test5.doGet(test5.java:59) at javax.servlet.http.HttpServlet.service(HttpServlet.java:620) at javax.servlet.http.HttpServlet.service(HttpServlet.java:727) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(Standard WrapperValve.java:220) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607) at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2441) at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2430) at java.util.concurrent.ThreadPo olExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)

导入 jar 后,您必须将其添加到项目的构建路径中。

right click on jar file -> build path ->add to build path

You've misspelt the class name, but you don't need it. Just remove that line. It's been obsolete for years, since JDBC 4.0.

I found the problem. I needed a uppercase D in String driver = "com.mysql.jdbc.driver";

God I feel stupid now

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM