java.lang.ClassNotFoundException:com.mysql.jdbc.Driver [Eclipse,Tomcat]

时间:2015-06-17 18:16:55

标签: java mysql eclipse tomcat jdbc

我知道这是一个过于质疑的问题,但我仍然无法做到这一点。 我正在Eclipse上运行一个必须与mysql数据库交互的Web应用程序。它全部运行在localhost上的tomcat 7.0.27服务器上。

尽管我尝试将我的mysql-connector-java-5.1.21-bin.jar添加到java构建路径,但我仍然得到相同的错误。显然,如果我将它作为外部库或在我的Webcontent / META-INF / lib文件夹中放置并不重要。还尝试了不同版本的连接器(18和25)。

  

java.lang.ClassNotFoundException:com.mysql.jdbc.Driver       在   org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)       在org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)       at java.lang.Class.forName0(Native Method)       at java.lang.Class.forName(Unknown Source)       at ns.Data.getInfo(Data.java:36)       在org.apache.jsp.home_jsp._jspService(home_jsp.java:78)       在org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:728)       在org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)       在org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)       在org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:728)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)       在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)       在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)       在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)       at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)       在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)       在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)       在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)       在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)       在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)       at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:589)       at org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:312)       at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)       at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)       在java.lang.Thread.run(未知来源)

这是调用驱动程序的类:

    package ns;
import java.sql.*;
import java.util.*;
public class Data { 

    public Data() {

    };

    public int prova = 4;

    {

    try { 
        Class.forName("com.mysql.jdbc.Driver"); 
        Connection con = DriverManager.getConnection("jdbc:mysql://" + "127.0.0.1" + "/" + "test" + "?" + "user=" + "matt" + "&password=" + "pass"); 
        Statement cmd = con.createStatement();

 // TODO QUERY
        ResultSet rs = cmd.executeQuery("SELECT * FROM carshare.passaggi");


        con.close(); } 
    catch (Exception e) { System.out.println("Errore con DB o Query errata"); 
    e.printStackTrace(); }


}

    public String[] getInfo(String table, String column) {
        String[] values = new String[100];
        int i = 0;
        String temp;

        try { 
            Class.forName("com.mysql.jdbc.Driver"); 
            Connection con = DriverManager.getConnection("jdbc:mysql://" + "127.0.0.1" + "/" + "test" + "?" + "user=" + "matt" + "&password=" + "pass"); 
            Statement cmd = con.createStatement();

     // QUERY DA ESEGUIRE 
            ResultSet rs = cmd.executeQuery("SELECT * FROM carshare."+ table);
        /*              
            while (rs.next()==true) {
                temp = rs.getString(column);
                System.out.print(temp);
                values[i] = temp;
                i++;
            }
            */
            con.close(); } 
        catch (Exception e) { System.out.println("Errore con DB o Query errata"); 
        e.printStackTrace(); }      


        return values;
    }

}

我会欢迎任何建议或建议,如果需要,还需要更多信息。

1 个答案:

答案 0 :(得分:1)

应该在您的Webcontent / WEB-INF / lib文件夹中而不是 Webcontent / META-INF / lib