为什么我需要在Tomcat Web应用程序中调用Class.forName,即使我使用的是JDBC Driver 4.0?

时间:2015-01-23 21:53:03

标签: java mysql tomcat jdbc

我正在研究Java Web,在学习期间我了解到,从JDBC的第4版开始,无需使用方法Class.forName来加载驱动程序。我在此link确认了此信息。

基于这些信息,我开发了没有该调用的getConnection方法,但是当我启动我的Tomcat并且我去测试我的方法时,抛出了以下异常。

jan 23, 2015 7:55:48 PM org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [br.com.caelum.servlet.AdicionaContatoServlet] in context with path [/fj21-agenda] threw exception
java.lang.RuntimeException: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/fj21
    at br.com.caelum.jdbc.ConnectionFactory.getConnection(ConnectionFactory.java:14)
    at br.com.caelum.jdbc.dao.ContatoDAO.<init>(ContatoDAO.java:19)
    at br.com.caelum.servlet.AdicionaContatoServlet.service(AdicionaContatoServlet.java:51)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1556)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1513)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/fj21
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at br.com.caelum.jdbc.ConnectionFactory.getConnection(ConnectionFactory.java:12)
    ... 25 more

我正在使用我的测试:

  • Tomcat 8
  • Java 8
  • Eclipse Luna Service Release 1(4.4.1)
  • MySQL的连接器的Java-5.1.34-bin.jar

如果我包含行Class.forName("com.mysql.jdbc.Driver");,一切正常。有人对此有任何想法吗?

1 个答案:

答案 0 :(得分:10)

在Tomcat启动期间,Service Provider mechanism扫描$CATALINA_BASE/lib文件夹中的数据库驱动程序以进行注册。但是,不会为每个Web应用程序执行此过程。

  

因此,在WEB-INF/lib目录中具有数据库驱动程序的Web应用程序不能依赖服务提供程序机制,应该明确注册驱动程序。

Apache Tomcat 8 - JNDI Datasource HOW-TO中查看更多内容。