第一个坑很明显就是找不到数据库,说明你没有database,或者database写错。
1
2
3
4
5
6
7com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@7f31c43f -- APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks! 6月 25, 2019 4:32:15 下午 com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run ... java.sql.SQLSyntaxErrorException: Unknown database 'ja'
解决方法就很easy了,检查是不是有以上两个问题,修改即可。
第二个坑也是比较明显的,时区报错,从而引起的c3p0连接数据库失败。
1
2
3
4
5
6
7
8java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support. Exception in thread "main" java.sql.SQLException: Connections could not be acquired from the underlying database! Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
解决办法:连接数据库的url改为
1
2jdbc:mysql://127.0.0.1:3306/javabook?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
第三个坑,更加明显驱动器的更改了,mysql80以上的版本驱动器改为
1
2com.mysql.cj.jdbc.Driver
1
2Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
第四个坑就是数据库服务器没打开,这个坑不是我遇到,但是确实有这样的情况,记录一下。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835) at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455) at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240) at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:207) 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) Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
解决办法,打开数据库服务器,dos窗口管理员模式运行下,运行
1
2net start mysql
第五个坑就是最大的坑了吧
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValuesForSimpleResultMap(DefaultResultSetHandler.java:352) at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValues(DefaultResultSetHandler.java:328) at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSet(DefaultResultSetHandler.java:301) at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSets(DefaultResultSetHandler.java:194) at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:65) at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:79) at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:63) at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:324) at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156) at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109) at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:83) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:77) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:564) at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433) at com.sun.proxy.$Proxy20.selectOne(Unknown Source) at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:166) at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:84) at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:58) at com.sun.proxy.$Proxy22.queryById(Unknown Source) at com.li.test.BookTest.testQueryById(BookTest.java:17) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:564) 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.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74) at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97) 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.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
解决办法,使用这个jar包
链接:https://pan.baidu.com/s/1itV4ru-qog-yNpIuizgQCQ
提取码:iokd
第六个坑:web项目中的jar包应该放在tomcat的lib文件下,因为tomcat默认使用的是自己的jar包。
如果出现单用没有问题,但是使用tomcat有问题,就应该更新或者增加tomcat中的lib中的jar包
接下来说一说使用的方法
不使用配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22import com.mchange.v2.c3p0.ComboPooledDataSource; import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManager; public class TEST { public static void main(String[] args) throws Exception{ // Class.forName("com.mysql.cj.jdbc.Driver"); // // Connection c= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/javabook?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=UTF-8","root","1230"); // ComboPooledDataSource source = new ComboPooledDataSource(); source.setDriverClass("com.mysql.cj.jdbc.Driver"); source.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/javabook?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8"); source.setUser("root"); source.setPassword("1230"); source.getConnection(); } }
使用C3P0的配置文件
这里有坑,名字一定要为
1
2c3p0-config,xml
并且一定要放在source文件夹下,因为默认会从这个地方找这个文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14<?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://127.0.0.1:3306/javabook?serverTimezone=GMT%2B8&useSSL=false&useUnicode=true&characterEncoding=utf-8</property> <property name="user">root</property> <property name="password">1230</property> <property name="initialPoolSize">10</property> <property name="maxIdleTime">30</property> <property name="maxPoolSize">100</property> <property name="minPoolSize">10</property> </default-config> </c3p0-config>
创建C3P0Utils
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64package utils; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.sql.DataSource; import com.mchange.v2.c3p0.ComboPooledDataSource; public class C3P0Utils { private static ComboPooledDataSource source = new ComboPooledDataSource(); public static DataSource getDataSource(){ return source; } public static Connection getConnection(){ try { return source.getConnection(); } catch (Exception e) { // TODO Auto-generated catch block throw new RuntimeException("服务器错误"); } } public static void closeAll(Connection conn,Statement statement,ResultSet resultSet){ if(resultSet != null){ try { resultSet.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } resultSet = null; } if(statement != null){ try { statement.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } statement = null; } if(conn != null){ try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); conn = null; } } } }
其中的closeAll不是很有必要,可以直接删除
接下来就可以使用QueryRunner进行执行sql,或者使用Statement进行执行sql语句.
1
2
3
4
5
6
7
8
9QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); String sql = "INSERT INTO user " + "(username,PASSWORD,gender,email,telephone,introduce,activeCode,state,role) " + "VALUES (?,?,?,?,?,?,?,?,?)"; qr.update(sql,"fa","fa","男", "fd","15","fd", "fd",0,"user");
最后说一下就是tomcat中使用的c3p0时候不应该只把WEB-INF中的libs文件夹中,也应该放在jre 的路径下,就是
eclipse中的
1
2build to path
IDEA中的
1
2Add AS Library
最后
以上就是落后小土豆最近收集整理的关于C3P0踩坑的全部内容,更多相关C3P0踩坑内容请搜索靠谱客的其他文章。
发表评论 取消回复