Tomcat 8 - java.sql.SQLException:无法为连接 URL 'jdbc:mysql://xxx/myApp' 创建类 '' 的 JDBC 驱动程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28725960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tomcat 8 - java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'jdbc:mysql://xxx/myApp'
提问by Brutus
I looked everywhere on the web in order to find a solution to my problem, but I had no luck! :(
我在网上到处寻找解决我的问题的方法,但我没有运气!:(
I'm trying to develop a servlet able to connect to a MySQL database (Connection Pool) and to deploy it on a Tomcat 8 server.
我正在尝试开发一个能够连接到 MySQL 数据库(连接池)并将其部署在 Tomcat 8 服务器上的 servlet。
I have a context.xml file in META-INF like this:
我在 META-INF 中有一个 context.xml 文件,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/DBConnectionPoolTest">
<Resource name="jdbc/testdb"
auth="Container"
type="javax.sql.DataSource"
username="xxx" password="xxx"
driverclassname="com.mysql.jdbc.Driver"
url="jdbc:mysql://xxx/myApp"
maxactive="10"
maxidle="4" />
</Context>
In WEB-INF I created the web.xml file:
在 WEB-INF 中,我创建了 web.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<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>
</web-app>
Finally, on the servlet class, I use:
最后,在 servlet 类上,我使用:
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env");
dataSource = (DataSource) envContext.lookup("jdbc/testdb");
...
connection = dataSource.getConnection();
But on this line, when I try to get a connection from the datasource, I get the following exception:
但是在这一行上,当我尝试从数据源获取连接时,出现以下异常:
java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'jdbc:mysql://xxx/myApp'
t org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2065)
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:1939)
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1412)
at DBPoolConnectionServlet.processRequest(DBPoolConnectionServlet.java:73)
...
Caused by: java.sql.SQLException: No suitable driver
I thought it could be caused by the absence of the JDBC Driver, so I putted mysql-connector-java-5.1.34-bin.jar
in:
我认为这可能是由于缺少 JDBC 驱动程序造成的,所以我输入mysql-connector-java-5.1.34-bin.jar
了:
- pom.xml dependency
- Tomcat LIB folder
- WEB-INF/lib folder inside the webapp
- pom.xml 依赖
- Tomcat LIB 文件夹
- webapp内的WEB-INF/lib文件夹
but without luck.
但没有运气。
Can you please tell me what I'm doing wrong? I feel so noob... :(
你能告诉我我做错了什么吗?我觉得很菜... :(
Thank you very much for your time spent reading (and hopefully answering) my question!!!
非常感谢您花时间阅读(并希望回答)我的问题!!!
采纳答案by Zielu
Looks like you are doing all fine, so:
看起来你一切正常,所以:
driverClassName="com.mysql.jdbc.Driver" (the capitals may matter).
driverClassName="com.mysql.jdbc.Driver"(大写可能很重要)。
You place the mysql-connector-java-5.1.34-bin.jar (it has to have jar extension to be detected in tomcat/lib (dont put it on your webapp path it should be loaded by tomcat class loader).
您放置 mysql-connector-java-5.1.34-bin.jar (它必须在 tomcat/lib 中检测到 jar 扩展名(不要将它放在您的 webapp 路径上,它应该由 tomcat 类加载器加载)。
If it is not helping and you are starting your webapp from IDE. Try to start tomcat form console and deploy your app manually. If you have more than one tomcat installed make sure that CATALINA_HOME is set to the one you place your mysql jar.
如果它没有帮助并且您正在从 IDE 启动您的 web 应用程序。尝试启动 tomcat 表单控制台并手动部署您的应用程序。如果您安装了多个 tomcat,请确保将 CATALINA_HOME 设置为您放置 mysql jar 的那个。
回答by Suketu Bhuta
Possible issue might be Tomcat encounters the driver jar multiple times during classloading. The driver jar only needs to be under the Tomcat lib directory and not under WEB-INF/lib
of your web application. Having the driver jar at both these locations resulted in weird class loading issues for me which manifested in the form of errors suggesting "jdbc driver with empty class and null url could not be created". See this answer for a similar issue in a different database: https://stackoverflow.com/a/11604084/2200690
可能的问题可能是 Tomcat 在类加载期间多次遇到驱动程序 jar。驱动程序 jar 只需要在 Tomcat lib 目录下WEB-INF/lib
,而不是在您的 Web 应用程序下。在这两个位置都有驱动程序 jar 对我来说导致了奇怪的类加载问题,这表现为错误的形式,提示“无法创建具有空类和空 url 的 jdbc 驱动程序”。请参阅此答案以了解不同数据库中的类似问题:https: //stackoverflow.com/a/11604084/2200690
Update: I no longer start Tomcat from within Eclipse to make sure the Eclipse and Tomcat settings don't get mixed up (Instead I just start tomcat from the bin directory and remote debug the jvm from Eclipse).Once I made this change, I could have the driver jar in both the Web-INF/lib and Tomcat lib directory and it didn't matter.
更新:我不再从 Eclipse 中启动 Tomcat 以确保 Eclipse 和 Tomcat 设置不会混淆(相反,我只是从 bin 目录启动 tomcat 并从 Eclipse 远程调试 jvm)。一旦我做了这个改变,我可以在 Web-INF/lib 和 Tomcat lib 目录中都有驱动程序 jar,这无关紧要。
回答by Jim Reitz
Steps to fix Netbeans/Tomee/MySQL connection issues and resolve FAIL - Deployed application at context path /RA7Web-1.0-SNAPSHOT but context failed to start:
修复 Netbeans/Tomee/MySQL 连接问题和解决 FAIL 的步骤 - 在上下文路径 /RA7Web-1.0-SNAPSHOT 部署应用程序但上下文无法启动:
Edit server.xml on your Tomee server using a full path name for appBase:
<!-- Fixed the problem of cannot deploy by providing a full path for appBase -->` <Host name="localhost" appBase="C:\apache\apache-tomee-7.0.2-plume\webapps" unpackWARs="true" autoDeploy="true">
Add the following to your project web-xml:
<resource-ref> <description>Resource Allocation database</description> <res-ref-name>jdbc/resourcealloc</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
Add the following to your Context.xml:
<Resource name="jdbc/resourcealloc" auth="Container" type="javax.sql.DataSource" username="root" password="mySecretPwd" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/resourcealloc" maxActive="15" maxIdle="3"/>
The following resources.xml was generated, but I had to edit several back-slashes out so it matched my URL:
<resources> <Resource id="jdbc/resourcealloc" type="javax.sql.DataSource"> jdbcDriver=com.mysql.jdbc.Driver password=mySecretPwd userName=root jdbcUrl=jdbc:mysql://localhost:3306/resourcealloc?zeroDateTimeBehavior=convertToNull </Resource> </resources>
使用 appBase 的完整路径名在 Tomee 服务器上编辑 server.xml:
<!-- Fixed the problem of cannot deploy by providing a full path for appBase -->` <Host name="localhost" appBase="C:\apache\apache-tomee-7.0.2-plume\webapps" unpackWARs="true" autoDeploy="true">
将以下内容添加到您的项目 web-xml 中:
<resource-ref> <description>Resource Allocation database</description> <res-ref-name>jdbc/resourcealloc</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
将以下内容添加到您的 Context.xml:
<Resource name="jdbc/resourcealloc" auth="Container" type="javax.sql.DataSource" username="root" password="mySecretPwd" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/resourcealloc" maxActive="15" maxIdle="3"/>
生成了以下 resources.xml,但我必须编辑几个反斜杠以使其与我的 URL 匹配:
<resources> <Resource id="jdbc/resourcealloc" type="javax.sql.DataSource"> jdbcDriver=com.mysql.jdbc.Driver password=mySecretPwd userName=root jdbcUrl=jdbc:mysql://localhost:3306/resourcealloc?zeroDateTimeBehavior=convertToNull </Resource> </resources>