[英]How to resolve “Cannot create JDBC driver of class 'com.mysql.jdbc.Driver' for connect URL”
Firstly I want to say that i checked all answers at stackoverflow, and i can't fix this bug! 首先我想说我检查了stackoverflow的所有答案,我无法解决这个错误! Help me please!
请帮帮我! I spend a lot of time, but no result.
我花了很多时间,但没有结果。 I'm trying to create connection pool using Tomcat8.
我正在尝试使用Tomcat8创建连接池。 I have an exception:
我有一个例外:
java.sql.SQLException: Cannot create JDBC driver of class 'com.mysql.jdbc.Driver' for connect URL 'jdbc:mysql:/localhost:3306/autopark' 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 ua.khpi.shapoval.db.DbConnector.init(DbConnector.java:31) at ua.khpi.shapoval.db.DbContextListner.contextInitialized(DbContextListner.java:48) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4842) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5303) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1407) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1397) at java.util.concurrent.Future
值java.sql.SQLException:无法创建类 'com.mysql.jdbc.Driver' 的连接URL的JDBC驱动程序 '的jdbc:mysql的:/本地主机:3306 /自动驻车' 在org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory (BasicDataSource.java:2160)在org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:2032)在org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1532)在ua.khpi.shapoval.db.DbConnector.init(DbConnector.java:31)位于org.apache.catalina.core.StandardContext.listenerStart的ua.khpi.shapoval.db.DbContextListner.contextInitialized(DbContextListner.java:48) StandardContext.java:4842)在org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5303)在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)在org.apache.catalina .core.ContainerBase $ StartChild.call(ContainerBase.java:1407)atg.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1397)at java.util.concurrent.Future Task.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) Caused by: java.sql.SQLException: No suitable driver at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2151) ... 13 more
Task.run(FutureTask.java:266)在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:617)在java.lang中.Thread.run(Thread.java:745)引起:java.sql.SQLException:org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2151)中没有合适的驱动程序... 13更多
DbConnector.class DbConnector.class
public class DbConnector {
private static Logger log = Logger.getLogger(DbConnector.class.getName());
private static DataSource dataSource;
private static Connection connection;
public static void init() throws ServletException, SQLException, NamingException, ClassNotFoundException {
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env/");
DataSource ds = (DataSource) envCtx.lookup("jdbc/autopark");
System.out.println(ds.getConnection());
}
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
}
context.xml that located in META-INF folder 位于META-INF文件夹中的context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context crossContext="true" reloadable="true">
<Resource name="jdbc/autopark" auth="Container" type="javax.sql.DataSource"
username="root" password="161acid161" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql:/localhost:3306/autopark" maxActive="15" maxIdle="3" />
<ResourceLink name="jdbc/autopark" global="jdbc/autopark"
type="javax.sql.DataSource" />
</Context>
web.xml file web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>Autostation</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<resource-ref>
<description>Db</description>
<res-ref-name>jdbc/autopark</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<listener>
<listener-class>ua.khpi.shapoval.db.DbContextListner</listener-class>
</listener>
</web-app>
Content of my tomcat/lib directory and my project structure. 我的tomcat / lib目录和我的项目结构的内容。
The JDBC URL is not correct, indeed you have a missing slash so try this: JDBC URL不正确,实际上你有一个丢失的斜杠,所以试试这个:
jdbc:mysql://localhost:3306/autopark
If you check well the real error is No suitable driver which means that it cannot find any JDBC driver that supports the provided URL. 如果你检查一下,真正的错误是没有合适的驱动程序 ,这意味着它找不到任何支持提供的URL的JDBC驱动程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.