我就简单的写了个纯的JDBC配置C3P0竟然出错了,我自己都没想得到会出错
给大家看一下我的配置吧,很简单的,这种配置我改成oracle的数据库就OK一换成sqlserver的就报错了,自己就也不知道为什么,难道两者有什么差异?求教!
C3P0配置:
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <default-config> <property name="user">sa</property> <property name="password">qaz123</property> <property name="driverClass">com.microsoft.sqlserver.jdbc.SQLServerDriver</property> <property name="jdbcUrl">jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ZCPD</property> <property name="initialPoolSize">20</property> <property name="maxIdleTime">50</property> <property name="maxPoolSize">30</property> <property name="minPoolSize">10</property> </default-config> </c3p0-config>
public class C3P0Utils { private C3P0Utils(){} private static ComboPooledDataSource ds=null; static{ try { ds=new ComboPooledDataSource(); } catch (Exception e) { e.printStackTrace(); } } public static Connection getConnection(){ Connection conn=null; try { conn=ds.getConnection(); } catch (Exception e) { e.printStackTrace(); } return conn; } }
另外我的用户名、密码都是OK的,服务也启动
报错信息:
Managed Threads: 3 Active Threads: 3 Active Tasks: com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@1cf4a2c (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2) com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@eac5a (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1) com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@15575e0 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0) Pending Tasks: com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@1addb59 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@11bed71 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@81b1fb com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@cbdb20 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@19f3736 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@1e1dadb com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@e79839 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@bac9b9 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@111b910 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@8ceeea com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@995a79 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@2c35e com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@1687e7c com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@14835fb com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@1cffeb4 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@5b0668 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@1d5a0 Pool thread stack traces: Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2,5,main] java.lang.Thread.sleep(Native Method) com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1805) com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547) Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1,5,main] java.lang.Thread.sleep(Native Method) com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1805) com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547) Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0,5,main] java.lang.Thread.sleep(Native Method) com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1805) com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547) 2014-5-18 11:10:34 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask run 警告: com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@1cf4a2c -- 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:264) at com.mchange.v2.c3p0.DriverManagerDataSource.driver(DriverManagerDataSource.java:223) at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:134) at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182) at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171) at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137) at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014) at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32) at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810) at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547) java.sql.SQLException: Connections could not be acquired from the underlying database! at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106) at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:529) at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128) at com.ft.utils.C3P0Utils.getConnection(C3P0Utils.java:20) at com.ft.utils.TEST.main(TEST.java:5) Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source. at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1319) at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557) at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477) at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525) ... 3 more 2014-5-18 11:10:34 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask run 警告: com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@eac5a -- 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:264) at com.mchange.v2.c3p0.DriverManagerDataSource.driver(DriverManagerDataSource.java:223) at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:134) at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182) at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171) at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137) at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014) at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32) at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810) at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
解决了!又是小毛病!自己都悔恨呐!
sqlserver连接的URL和驱动类分2003和2005,我当时注意到这点了,但是我驱动类写对了,但是URL写成2003的了,我用的是2008,所以就报错了!
唉,自己总是不注意太粗心,大家以后可不能像我这样!谨记!!!
java.sql.SQLException:Nosuitabledriver
没选对驱动?
建议采用jtds来进行驱动。回复 @亚美碟:你帖的报错里这么说,也有可能是没找到还是怎么我2年来一直都用的这个驱动包,想不通版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。