no suitable driver found解决方法

使用maven学习连接池时遇到的no suitable driver found问题。
网上的方法看了很多基本都没法解决。
ps:首先将c3p0-config.xml配置文件放在resources下面

报错内容

Exception in thread "main" java.lang.RuntimeException: 获取数据库连接失败
	at com.etime.Utils.C3P0Util.getConnection(C3P0Util.java:17)
	at com.etime.C3P0Test.testC3P0(C3P0Test.java:20)
	at com.etime.C3P0Test.main(C3P0Test.java:14)
八月 20, 2022 4:24:54 下午 com.mchange.v2.resourcepool.BasicResourcePool 
警告: Having failed to acquire a resource, com.mchange.v2.resourcepool.BasicResourcePool@2d6a9952 is interrupting all Threads waiting on a resource to check out. Will try again in response to new client requests.
八月 20, 2022 4:24:54 下午 com.mchange.v2.resourcepool.BasicResourcePool 
警告: Having failed to acquire a resource, com.mchange.v2.resourcepool.BasicResourcePool@2d6a9952 is interrupting all Threads waiting on a resource to check out. Will try again in response to new client requests.
八月 20, 2022 4:24:54 下午 com.mchange.v2.resourcepool.BasicResourcePool 
警告: com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@5cb9963b -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30). Last acquisition attempt exception: 
java.sql.SQLException: No suitable driver
	at java.sql.DriverManager.getDriver(DriverManager.java:315)
	at com.mchange.v2.c3p0.DriverManagerDataSource.driver(DriverManagerDataSource.java:285)
	at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:161)
	at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:161)
	at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:147)
	at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:202)
	at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1138)
	at com.mchange.v2.resourcepool.BasicResourcePool.doAcquireAndDecrementPendingAcquiresWithinLockOnSuccess(BasicResourcePool.java:1125)
	at com.mchange.v2.resourcepool.BasicResourcePool.access$700(BasicResourcePool.java:44)
	at com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask.run(BasicResourcePool.java:1870)
	at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:696)

原因:缺少dbutils依赖

解决方法

在pom.xml中导入依赖

<dependency>
  	<groupId>log4j</groupId>
   	<artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
<dependency>
	<groupId>commons-dbutils</groupId>
   	<artifactId>commons-dbutils</artifactId>
   	<version>1.6</version>
</dependency>

加入后报错内容

Exception in thread "main" java.lang.RuntimeException: 获取数据库连接失败
	at com.etime.Utils.C3P0Util.getConnection(C3P0Util.java:17)
	at com.etime.C3P0Test.testC3P0(C3P0Test.java:20)
	at com.etime.C3P0Test.main(C3P0Test.java:14)

解决方法

需要在C3P0Utils.java 工具类中的创建数据库连接池中通过标识名来创建相应连接池

//通过标识名来创建相应连接池
    private static DataSource dataSource = new ComboPooledDataSource("mysql");

添加mysql标识名以后正常查询

Student{studentId=1, studentName='lili'}
Student{studentId=2, studentName='dodo'}
Student{studentId=3, studentName='tutu'}
Student{studentId=4, studentName='mqmq'}
Student{studentId=5, studentName='yzj'}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值