Code Bye

Cannot create JDBC driver of class '' for connect URL '

I”m learning how to use a servlet by accessing the JNDI Datasource and however, the connection couldn”t be established.Here is my Code:
Tomcat”s Server.xml:
**<Resource name=”jdbc/testDB”
auth=”Container”
type=”javax.sql.DataSource”
driverClassName=”com.mysql.jdbc.Driver”
url=”jdbc:mysql://192.168.56.101:3306/testDB?characterEncoding=utf-8″
username=”test”
password=”123″
maxActive=”200″
maxIdle=”50″
maxWait=”3000″/>**
Tomcat”s Context.xml:
**<Resource name=”jdbc/testDB”
auth=”Container”
type=”javax.sql.DataSource”
driverClassName=”com.mysql.jdbc.Driver”
url=”jdbc:mysql://192.168.56.101:3306/testDB?characterEncoding=utf-8″
username=”test”
password=”123″
maxActive=”200″
maxIdle=”50″
maxWait=”3000″/>**
and final web.xml:
<servlet-mapping>
<servlet-name>ViewDictionary</servlet-name>
<url-pattern>/servlet/ViewDictionary</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
**<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/testDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>**
The problem happens here:
javax.naming.Context ctx = new javax.naming.InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource)ctx.
lookup(“java:/comp/env/jdbc/testDB”);
**Connection conn = ds.getConnection();  //Couldn”t get the Connection**
Error Stack Trace:
java.sql.SQLException: Cannot create JDBC driver of class “” for connect URL “null”
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2160)
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:2032)
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1532)
at chapter4.ViewDictionary.doGet(ViewDictionary.java:28)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
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:217)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
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:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(DriverManager.java:315)
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2144)
… 26 more
解决方案

40

引用:

@zhangweihan01  谢谢。把xml文件放到META-INF目录下就解决问题了。本人是在Tomcat的conf/context.xml和server.xml中配置的,可是没解决问题。“把xml文件放到META-INF目录下这种配置”只是对当前web项目起作用,按理来说在tomcat中直接配置不就行吗?为什么会报错?这样应该是全局性的配置。

本人也不太清楚,java学习中,今天刚看的老视频。上面说是tomcat类加载器和web项目的类加载器不一样,给你的方法是使用web项目本人的类加载器加载的连接池。假如要用整个tomcat的加载器加载就是配置在server.xml中,然后在本地tomcat路径的一个lib下加入数据库连接的jar包就ok了。但是本人看的版本较老,可能不适用,你可以在网上搜搜试试


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Cannot create JDBC driver of class '' for connect URL '