java - 如何在tomcat 7中启用mysql数据库驱动程序?

标签 java web-services rest jdbc jax-rs

我正在使用 JAX-RS 创建一个 REST Web 服务。当我将其作为 java 应用程序运行时,后端类正常运行。但是当我在服务器上运行它并尝试访问如下资源时:

http://localhost:8080/WS/rest/metric/10  

我得到以下异常:

ERROR [http-bio-8080-exec-3] (Database.java:42) - Exception occured: No suitable driver found for jdbc:mysql://localhost:3306/
ERROR [http-bio-8080-exec-3] (Database.java:42) - Exception occured: No suitable driver  found for jdbc:mysql://localhost:3306/
Jun 19, 2012 11:27:29 AM com.sun.jersey.spi.container.ContainerResponse  mapMappableContainerException
SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP  container
java.lang.NullPointerException
.....

前两行是我在数据库连接方法中的日志:

public static Connection connect()
{
    try
    {
        return DriverManager.getConnection(domain, username, password);
    }

    catch (Exception e)
    {
        logger.error("Exception occured: "+ e.getMessage());
        return null;
    }
}

找不到驱动程序导致 NullPointerException。 我已将驱动程序 jar 文件复制到 WEB-INF/lib 中。但问题还是一样。

这是完整的异常跟踪:

 Jun 19, 2012 11:27:29 AM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
java.lang.NullPointerException
at com.hp.silas.dao.EstimationDAOImpl.getLatestResult(EstimationDAOImpl.java:126)
at com.hp.silas.ws.EstimationResource.getLastEstimation(EstimationResource.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1483)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1414)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1363)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1353)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:414)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

最佳答案

缺少的部分是使用此行注册驱动程序:

Class.forName("com.mysql.jdbc.Driver"); 

因此连接方法将如下所示:

public static Connection connect()
{
    try
    {
        Class.forName("com.mysql.jdbc.Driver");   
        return DriverManager.getConnection(domain, username, password);
    }

    catch (Exception e)
    {
        logger.error("Exception occured: "+ e.getMessage());
        return null;
    }
}

关于java - 如何在tomcat 7中启用mysql数据库驱动程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11099274/

相关文章:

java - 抛出异常以指示未找到元素

java - RecyclerView 没有填充项目,而 Adapter 和 ArrayList 已经有数据

mysql - 使用 windows phone 7 使用 mySQL 数据库

linux - 是否有用于 Linux 手册页的(RESTful)API?

rest - 使用 Apache Camel 的微服务

Spring Controller : RequestParam not validated despite @Valid and @Size

java - 在JAVA中读取和打印CSV文件

java - 无法使用 rtc 客户端库设置自定义属性值(枚举类型)

c# - 是什么驱动数据传输对象的设计?

java - 使用 Metro,向客户端添加 MTOMFeature 会导致 MIMEParsingException,为什么?