Java Servlet无法连接到MySQL

时间:2016-05-06 23:59:45

标签: java mysql servlets

我在java servlet中的代码是:

Class.forName("com.mysql.jdbc.Driver");
        Connection connectionToSql = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/table_name", "root", "root");
        PreparedStatement ask = connectionToSql.prepareStatement("SELECT COUNT(*) FROM table_name");
        ResultSet result = ask.executeQuery();

但它会抛出异常e并System.out.println(e.getMessage());打印com.mysql.jdbc.Driver。相同的代码在标准java类中工作,但在servlet中失败。我无法找出问题所在?求助。

以下是完整的错误消息..

    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
quiz.QuestionsPreparer.prepareQuestions(QuestionsPreparer.java:81)
quiz.QuestionsPreparer.doPost(QuestionsPreparer.java:42)
quiz.QuestionsPreparer.doGet(QuestionsPreparer.java:36)
javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
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:505)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Unknown Source)

2 个答案:

答案 0 :(得分:0)

如果不仔细查看项目文件夹结构和代码,很难为您提供解决方案。

但是链接下面的Statement声明了Web Application文件夹结构的重要性。 https://tomcat.apache.org/tomcat-5.5-doc/appdev/deployment.html

/ WEB-INF / classes / - 此目录包含应用程序中的Java类文件。

/ WEB-INF / lib / - 此目录包含JAR文件,其中包含应用程序所需的Java类文件(及相关资源) ,例如第三方类库或JDBC驱动程序。

你可以解压war文件并在WEB_INF / lib上获取mysql jar文件的战利品。

答案 1 :(得分:-1)

你可以这样做:

function reduce(collection, combine, start){
    var result = start, hasValue = arguments.length > 2;

    each(collection, function(element){
        if(hasValue){
            result = combine(result, element);
        }else{
            result = element;
            hasValue = true;
        }
    });

    if(!hasValue){
        //no start/default-value, no elements in the collection,
        //what result do you expect from this reduce-task?
        throw new Error("reduce of empty array with no initial value");     
    }
    return result;
}

function filter(collection, predicate){
    return reduce(collection, function(result, elm){
        //the functional-approach: no mutations
        return predicate(elm)?
            result.concat( [elm] ):
            result;

        //or a more sane way to do this in JS
        if( predicate(elm) ) result.push( elm );
        return result;
    }, []);
}