使用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'}