![](/img/trans.png)
[英]Cannot create JDBC driver of class 'com.mysql.jdbc.Driver' for connect URL 'jdbc:mysql//localhost:3306/
[英]How to resolve “Cannot create JDBC driver of class 'com.mysql.jdbc.Driver' for connect URL”
首先我想說我檢查了stackoverflow的所有答案,我無法解決這個錯誤! 請幫幫我! 我花了很多時間,但沒有結果。 我正在嘗試使用Tomcat8創建連接池。 我有一個例外:
值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)在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
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();
}
}
位於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文件
<?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>
JDBC URL不正確,實際上你有一個丟失的斜杠,所以試試這個:
jdbc:mysql://localhost:3306/autopark
如果你檢查一下,真正的錯誤是沒有合適的驅動程序 ,這意味着它找不到任何支持提供的URL的JDBC驅動程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.