这个错误是怎么回事呢

这个错误是怎么回事呢

ackage com.imooc.jdbc.demo3;

import java.sql.ResultSet;

import org.junit.Test;

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

/**
 * 连接池的测试类
 * @author xiaodong
 *
 */
public class DataSourceDemo1 {
     @Test
     /**
      * 手动设置连接池
      */
     public void demo1(){
      Connection connection=null;
      PreparedStatement preparedStatement=null;
      ResultSet resultSet=null;
      try {
       //创建连接池
         ComboPooledDataSource comboPooledDataSource=new ComboPooledDataSource();
         //设置连接池的参数
         comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");
         comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/king?useUnicode=true&characterEncoding=UTF8");
         comboPooledDataSource.setUser("root");
         comboPooledDataSource.setPassword("821103");
         comboPooledDataSource.setMaxPoolSize(20); // 连接池可以连接数据库的最大数量
         comboPooledDataSource.setInitialPoolSize(3); //连接池里有多少个连接
         //获得连接
   connection=(Connection) comboPooledDataSource.getConnection();
   //编写sql
   String sql="select * from goods";
   //预编译sql
   preparedStatement=(PreparedStatement) connection.prepareStatement(sql);
   //设置参数
   //执行sql
   resultSet=preparedStatement.executeQuery();
   while(resultSet.next()){
    System.out.println(resultSet.getInt("id")+" "+resultSet.getString("name")+" "+resultSet.getFloat("price")+" "+resultSet.getString("desp"));
   }
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
   JDBCUtils.release(resultSet, preparedStatement, connection);
  }
     
     }
}



2019-2-13 14:57:18 com.mchange.v2.log.MLog <clinit>
信息: MLog clients using java 1.4+ standard logging.
2019-2-13 14:57:18 com.mchange.v2.c3p0.C3P0Registry banner
信息: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
2019-2-13 14:57:18 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 -> 1hgen40a01rpv4o4byua2u|3d9360e2, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hgen40a01rpv4o4byua2u|3d9360e2, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql://localhost:3306/king?useUnicode=true&characterEncoding=UTF8, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 20, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]
java.lang.ClassCastException: com.mchange.v2.c3p0.impl.NewProxyConnection cannot be cast to com.mysql.jdbc.Connection
 at com.imooc.jdbc.demo3.DataSourceDemo1.demo1(DataSourceDemo1.java:37)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
 at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
 at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
 at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
 at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
 at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
 at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
 at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
 at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
 at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
 at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

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

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

1回答
好帮手慕阿莹 2019-02-13 16:06:27

请问同学的数据库是哪个版本的呢?数据库的驱动是否是对应的呢?如果是8.0的,可以参考一下这个教辅修改一下

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

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

  • 提问者 努力学习的我 #1
    我是5.7版本,数据库驱动也添加好了,其他练习都可以用,就到连接池这里出问题了
    2019-02-13 17:13:44
  • 应该是导入包错误了: import com.mysql.jdbc.Connection; 改成: import java.sql.Connection 试试。如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
    2019-02-13 17:26:56
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

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

了解课程
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

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