mysql的链接驱动类,如何解决“无法为连接URL”创建类com.mysql.jdbc.Driver'的JDBC驱动程序...

Firstly I want to say that i checked all answers at stackoverflow, and i can't fix this bug! Help me please! I spend a lot of time, but no result.

I'm trying to create connection pool using 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.FutureTask.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

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

username="root" password="161acid161" driverClassName="com.mysql.jdbc.Driver"

url="jdbc:mysql:/localhost:3306/autopark" maxActive="15" maxIdle="3" />

type="javax.sql.DataSource" />

web.xml file

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

Autostation

index.jsp

Db

jdbc/autopark

javax.sql.DataSource

Container

ua.khpi.shapoval.db.DbContextListner

Content of my tomcat/lib directory and my project structure.

7pgJl.png

LNooh.png

解决方案

The JDBC URL is not correct, indeed you have a missing slash so try this:

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.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值