Tomcat 8 - java.sql.SQLException: 无法为连接 URL 'jdbc:mysql://xxx/myApp' 创建 class '' 的 JDBC 驱动程序
Tomcat 8 - java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'jdbc:mysql://xxx/myApp'
为了解决我的问题,我在网上到处找,但我没有运气! :(
我正在尝试开发一个能够连接到 MySQL 数据库(连接池)并将其部署在 Tomcat 8 服务器上的 servlet。
我在 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>
在 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>
最后,在 servlet class 上,我使用:
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env");
dataSource = (DataSource) envContext.lookup("jdbc/testdb");
...
connection = dataSource.getConnection();
但是在这条线上,当我尝试从数据源获取连接时,出现以下异常:
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
我认为可能是没有JDBC驱动导致的,所以我把mysql-connector-java-5.1.34-bin.jar
放在:
- pom.xml依赖
- Tomcat LIB 文件夹
- WEB-INF/lib webapp 中的文件夹
但运气不好。
你能告诉我我做错了什么吗?我觉得很菜鸟...:(
非常感谢您花时间阅读(并希望回答)我的问题!!!
看起来你一切都很好,所以:
driverClassName="com.mysql.jdbc.Driver"(大写可能很重要)。
你放置 mysql-connector-java-5.1.34-bin.jar(它必须有 jar 扩展才能在 tomcat/lib 中检测到(不要放它在你的 webapp 路径上,它应该由 tomcat class 加载器加载)。
如果它没有帮助并且您正在从 IDE 启动您的网络应用程序。尝试启动 tomcat 表单控制台并手动部署您的应用程序。如果您安装了多个 tomcat,请确保 CATALINA_HOME 设置为您放置 mysql jar 的那个。
可能的问题是 Tomcat 在 class 加载期间多次遇到驱动程序 jar。驱动程序 jar 只需要在 Tomcat lib 目录下,而不是在您的 Web 应用程序的 WEB-INF/lib
下。在这两个位置安装驱动程序 jar 对我来说导致了奇怪的 class 加载问题,表现为 "jdbc driver with empty class and null url could not be created" 的错误形式。请参阅此答案以了解不同数据库中的类似问题:
Update :我不再从 Eclipse 中启动 Tomcat 以确保 Eclipse 和 Tomcat 设置不会混淆(相反,我只需从 bin 目录启动 tomcat 并从 Eclipse 远程调试 jvm。一旦我做了这个更改,我就可以在 Web-INF/lib 和 Tomcat lib 目录中拥有驱动程序 jar 并且它没关系。
修复 Netbeans/Tomee/MySQL 连接问题和解决失败的步骤 - 在上下文路径 /RA7Web-1.0-SNAPSHOT 部署应用程序但上下文无法启动:
使用 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>
为了解决我的问题,我在网上到处找,但我没有运气! :(
我正在尝试开发一个能够连接到 MySQL 数据库(连接池)并将其部署在 Tomcat 8 服务器上的 servlet。
我在 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>
在 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>
最后,在 servlet class 上,我使用:
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env");
dataSource = (DataSource) envContext.lookup("jdbc/testdb");
...
connection = dataSource.getConnection();
但是在这条线上,当我尝试从数据源获取连接时,出现以下异常:
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
我认为可能是没有JDBC驱动导致的,所以我把mysql-connector-java-5.1.34-bin.jar
放在:
- pom.xml依赖
- Tomcat LIB 文件夹
- WEB-INF/lib webapp 中的文件夹
但运气不好。
你能告诉我我做错了什么吗?我觉得很菜鸟...:(
非常感谢您花时间阅读(并希望回答)我的问题!!!
看起来你一切都很好,所以:
driverClassName="com.mysql.jdbc.Driver"(大写可能很重要)。
你放置 mysql-connector-java-5.1.34-bin.jar(它必须有 jar 扩展才能在 tomcat/lib 中检测到(不要放它在你的 webapp 路径上,它应该由 tomcat class 加载器加载)。
如果它没有帮助并且您正在从 IDE 启动您的网络应用程序。尝试启动 tomcat 表单控制台并手动部署您的应用程序。如果您安装了多个 tomcat,请确保 CATALINA_HOME 设置为您放置 mysql jar 的那个。
可能的问题是 Tomcat 在 class 加载期间多次遇到驱动程序 jar。驱动程序 jar 只需要在 Tomcat lib 目录下,而不是在您的 Web 应用程序的 WEB-INF/lib
下。在这两个位置安装驱动程序 jar 对我来说导致了奇怪的 class 加载问题,表现为 "jdbc driver with empty class and null url could not be created" 的错误形式。请参阅此答案以了解不同数据库中的类似问题:
Update :我不再从 Eclipse 中启动 Tomcat 以确保 Eclipse 和 Tomcat 设置不会混淆(相反,我只需从 bin 目录启动 tomcat 并从 Eclipse 远程调试 jvm。一旦我做了这个更改,我就可以在 Web-INF/lib 和 Tomcat lib 目录中拥有驱动程序 jar 并且它没关系。
修复 Netbeans/Tomee/MySQL 连接问题和解决失败的步骤 - 在上下文路径 /RA7Web-1.0-SNAPSHOT 部署应用程序但上下文无法启动:
使用 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>