java.lang.ClassNotFoundException:com.mysql.jdbc.driver错误,已经导入了jar文件

时间:2014-04-15 23:05:28

标签: java mysql tomcat jdbc driver

我一直在寻找解决我的问题的方法,我找到的唯一解决方案是添加我已经拥有的jar文件。这是在eclipse EE中完成的,并且正在使用tomcat。

我正在运行一个不同的HTML文件,一旦按下按钮就链接到这段代码,如果你想让代码随意询问,但几乎没有任何内容。

我不断收到错误“java.lang.ClassNotFoundException:com.mysql.jdbc.driver”以及其他100行,我不确定是否会接受这些错误。

这就是我所拥有的:

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
    }

}

如果我发布了错误的代码,我就会出现问题,我以前从未使用过这个网站而且很累。 正如我所说,我已经导入了“mysql-connector-java-5.1.30-bin.jar”文件。所有的帮助都是张开双臂和批评所接受的。

感谢您的时间

错误,如果它实际上与驱动程序无关:

java.lang.ClassNotFoundException:com.mysql.jdbc.driver     在org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)     在org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)     at java.lang.Class.forName0(Native Method)     at java.lang.Class.forName(Unknown Source)     在test5.doGet(test5.java:59)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:620)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:727)     在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)     在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)     在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)     在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)     在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)     在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)     在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)     在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)     在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)     在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)     在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)     在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)     在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)     at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:607)     在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.ThreadPoolExecutor.runWorker(Unknown Source)     at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)     在java.lang.Thread.run(未知来源)

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

您错过了班级名称,但您并不需要它。只需删除该行。自JDBC 4.0以来,它已经过时多年了。

答案 2 :(得分:0)

我发现了问题。我在String driver中需要一个大写的D =" com.mysql.jdbc.driver&#34 ;;

上帝我现在感到愚蠢