我正在学习Java Web,在学习期间,我从JDBC的版本4中了解到,无需使用该方法Class.forName来加载驱动程序。我在此链接中确认了此信息。
Class.forName
根据此信息,我开发了没有该调用的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
我在测试中使用:
如果我加入这一行,Class.forName("com.mysql.jdbc.Driver");一切正常。有人对此有任何想法吗?
Class.forName("com.mysql.jdbc.Driver");
在Tomcat启动期间,服务提供者机制会扫描$CATALINA_BASE/lib 文件夹中的数据库驱动程序以进行注册。但是不会为每个Web应用程序执行此过程。
$CATALINA_BASE/lib
因此,WEB-INF/lib目录中具有数据库驱动程序的Web应用程序不能依赖服务提供者机制,而应显式注册驱动程序。
WEB-INF/lib
请参阅 Apache Tomcat 8-JNDI数据源HOW- TO中的更多内容 。