This question already has an answer here:
Cannot create JDBC driver of class '' for connect URL 'null' -:ERROR while creating and using a Datasource in tomcat 6
                                
                                    (1个答案)
                                
                        
                                3年前关闭。
            
                    
我正在通过访问JNDI数据源来学习如何使用servlet,但是无法建立连接。这是我的代码:

Tomcat的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的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"/>**


最后的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>**


问题发生在这里:

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**


错误堆栈跟踪:

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

最佳答案

我的最终解决方案是生成一个context.xml,其中“ Resource”标记位于该位置,然后将其移动到目录“ / WebContent / META-INF”。我的IDE是Eclipse。

这是代码:

 <?xml version="1.0" encoding="UTF-8"?>
<Context>
 <Resource name="jdbc/testDB" auth="Container" type="javax.sql.DataSource"
    maxActive="100" maxIdle="30" maxWait="10000" username="test" password="123"
    driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://192.168.56.101:3306/testDB" />
</Context>


目录是:

enter image description here

终于可以了。但是,我仍然找不到正确的方法来直接在tomcat中并置server.xml和context.xml以获取连接。

10-05 21:10
查看更多