为什么demo2()运行完,没有显示结果?

为什么demo2()运行完,没有显示结果?

package com.imooc.jdbc.demo3;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.junit.Test;

import com.imooc.jdbc.utils.JDBCUtils;
import com.mchange.v2.c3p0.ComboPooledDataSource;

/*
 * 连接池的测试
 * @author jt
 */
public class DataSourceDemo1 {
   
	@Test
	/*
	 * 使用配置文件的方式
	 */
	public void demo2() {
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try {
			//获得连接
			ComboPooledDataSource dataSource=new ComboPooledDataSource();
			//获得连接
			conn=dataSource.getConnection();
			//编写SQL
			String sql="select * from user";
			//预编译SQL
			pstmt=conn.prepareStatement(sql);
		
			//设置参数
			//执行SQL
			rs=pstmt.executeQuery();
			while(rs.next()) {
				System.out.println(rs.getInt("uid")+"   "+rs.getString("username")+"   "+rs.getString("password")+"   "+rs.getString("name"));
		       }
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			JDBCUtils.release(rs, pstmt, conn);
		}
	}

报错信息

三月 09, 2019 2:31:20 下午 com.mchange.v2.log.MLog <clinit>

信息: MLog clients using java 1.4+ standard logging.

三月 09, 2019 2:31:21 下午 com.mchange.v2.c3p0.C3P0Registry banner

信息: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]

三月 09, 2019 2:31:21 下午 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager

信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1hge0w8a1qye500174v313|eb21112, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> null, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hge0w8a1qye500174v313|eb21112, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> null, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 15, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]

三月 09, 2019 2:31:50 下午 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask run

警告: com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@74ea4fd2 -- 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:223)

at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:119)

at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:143)

at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:132)

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)


三月 09, 2019 2:31:50 下午 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask run

警告: com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@17715619 -- 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:223)

at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:119)

at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:143)

at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:132)

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)


三月 09, 2019 2:31:50 下午 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask run

警告: com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@1288e02 -- 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:223)

at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:119)

at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:143)

at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:132)

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.imooc.jdbc.demo3.DataSourceDemo1.demo2(DataSourceDemo1.java:30)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)

at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)

at org.junit.runners.ParentRunner.run(ParentRunner.java:363)

at org.junit.runner.JUnitCore.run(JUnitCore.java:137)

at org.junit.runner.JUnitCore.run(JUnitCore.java:115)

at org.junit.vintage.engine.execution.RunnerExecutor.execute(RunnerExecutor.java:39)

at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)

at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)

at java.util.Iterator.forEachRemaining(Iterator.java:116)

at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)

at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)

at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)

at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)

at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)

at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)

at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)

at org.junit.vintage.engine.VintageTestEngine.executeAllChildren(VintageTestEngine.java:79)

at org.junit.vintage.engine.VintageTestEngine.execute(VintageTestEngine.java:70)

at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:220)

at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:188)

at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:202)

at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:181)

at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)

at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:89)

at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

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)

... 45 more


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

3回答
慕沐1471085 2019-05-20 23:13:17

你这个问题很明显啊你的  &  符号没报错吗?

  • 提问者 慕羲 #1
    ?????
    2019-05-22 18:13:09
提问者 慕羲 2019-03-11 16:31:25

<?xml version="1.0" encoding="UTF-8"?>

<c3p0-config>


  <default-config>

    <property name="driverClass">com.mysql.cj.jdbc.Driver</property>

<property name="jdbcUrl">jdbc:mysql:///jdbctest?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT%2B8</property>

<property name="user">root</property>

<property name="password">1234</property>

<property name="initialPoolSize">5</property>

<property name="maxPoolSize">20</property>

  </default-config>

  

</c3p0-config>

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>

  <default-config>
    <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
	<property name="jdbcUrl">jdbc:mysql:///jdbctest?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT%2B8</property>
	<property name="user">root</property>
	<property name="password">1234</property>
	<property name="initialPoolSize">5</property>
	<property name="maxPoolSize">20</property>
  </default-config>
  
</c3p0-config>


  • 你好同学,你不需要在每个问答下都提相同的问题或者相同的代码,因为这样不方便问题的跟踪,建议你同一个问题只提问一次,这样方便老师的跟踪答疑哦,祝学习愉快~
    2019-03-11 18:39:41
  • 提问者 慕羲 回复 chrismorgen #2
    我也不想这样?我是一路打代码,有时候碰到一样的代码问题?老师有时候没有及时帮我解决,我就会把问题带到下一个视频,或者自由编程。因为有的问题是包的共有问题,这里不解决下面就没有办法运行代码? 有时候我不知道是同一个问题?这也解决完才知道是同一个问题。因为都是运行的报错信息,我也不知道是同一个问题,这也是上一个问题拖到没解决,影响下一个程序的运行和理解。
    2019-03-12 08:16:55
  • chrismorgen 回复 提问者 慕羲 #3
    恩恩,理解同学的心情,我们答疑是按照时间顺序进行解答的,所以有时未能及时回复老师深感抱歉,但是遇到问题一定要解决一个才能进行下一步,否则老师就无法判断你到底是那个问题引发的错误了,而且即使解决了错误,同学对错误的理解也不深刻,所以建议同学先自己排查错误,如果有不明白的,在问答区进行详细的描述并贴上代码,这样方便老师解答,也更能增加同学对错误的理解呢,祝学习愉快~
    2019-03-12 09:49:07
chrismorgen 2019-03-10 13:39:46

你好同学,如下图所示,该报错问题是因为你的c3p0-config.xml中的配置文件出现问题,或者你没有将mysql-connector-java-****-bin.jar包加入到Java运行环境的外部jar包的存放的位置。建议你先检查一下自己的数据库驱动是否正常添加。如果是正常添加,建议你将c3p0-config.xml中的代码粘贴到我要回答中,方便我进行检查,祝学习愉快~

http://img1.sycdn.imooc.com//climg/5c84a28c0001d13f07420147.jpg

  • 提问者 慕羲 #1
    c3p0-cnfing.xml的代码,是在教铺里面找吗???
    2019-03-10 16:06:42
  • chrismorgen 回复 提问者 慕羲 #2
    你好同学,关于c3p0-cnfing.xml的代码,我在这个问答下http://class.imooc.com/course/qadetail/97769,回复你了,祝学习愉快~
    2019-03-11 10:49:39
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
Java数据库开发与实战应用2018版
  • 参与学习           人
  • 提交作业       277    份
  • 解答问题       4297    个

Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师