报错了,一直就没调通
来源:7-2 C3P0连接池的使用
96年的nash
2019-04-26 16:39:01
package com.hxh.jdbc.demo3;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.junit.Test;
import com.hxh.utils.JDBCUtils;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
* 连接池的测试类
* @author 胡小虎
*
*/
public class DataSourceDemo1 {
@Test
/**
* 使用配置文件的方式
*/
public void demo2() {
Connection connection = null;
PreparedStatement pStatement = null;
ResultSet resultSet = null;
try {
//获得连接
ComboPooledDataSource dataSource = new ComboPooledDataSource();
//从连接池中获得
connection =dataSource.getConnection();
//编写SQL
String sql = "SELECT * FROM user";
//预编译SQL
pStatement = connection.prepareStatement(sql);
//设置参数
//执行SQL
resultSet = pStatement.executeQuery();
while(resultSet.next()) {
int uid = resultSet.getInt("uid");
String username = resultSet.getString("username");
String password = resultSet.getString("password");
String name = resultSet.getString("name");
System.out.println(uid + " " + username + " " + password + " " + name);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
JDBCUtils.release(resultSet, pStatement, connection);
}
}
// @Test
// /**
// * 手动设置连接池
// */
// public void demo1() {
//
//
// //获得连接
// Connection connection = null;
// PreparedStatement pStatement = null;
// ResultSet resultSet = null;
// try {
// //创建连接池
// ComboPooledDataSource dataSource = new ComboPooledDataSource();
// //获得连接池的参数
// dataSource.setDriverClass("com.mysql.jdbc.Driver");
// dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/jdbctest?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8");
// dataSource.setUser("root");
// dataSource.setPassword("djh,.525787961");
//
// dataSource.setMaxPoolSize(20);
// dataSource.setInitialPoolSize(3);
//
// //获得连接
// //从连接池中获得
// connection =dataSource.getConnection();
// //编写SQL
// String sql = "SELECT * FROM user";
// //预编译SQL
// pStatement = connection.prepareStatement(sql);
// //设置参数
// //执行SQL
// resultSet = pStatement.executeQuery();
// while(resultSet.next()) {
// int uid = resultSet.getInt("uid");
// String username = resultSet.getString("username");
// String password = resultSet.getString("password");
// String name = resultSet.getString("name");
// System.out.println(uid + " " + username + " " + password + " " + name);
// }
// } catch (Exception e) {
// e.printStackTrace();
// }finally {
// JDBCUtils.release(resultSet, pStatement, connection);
// }
// }
}<?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://localhost:3306/jdbctest?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8&autoReconnect=true</property> <property name="user">root</property> <property name="password">abc</property> <property name="initialPoolSize">5</property> <property name="maxPoolSize">20</property> </default-config> </c3p0-config>

四月 26, 2019 4:35:26 下午 com.mchange.v2.log.MLog <clinit>
信息: MLog clients using java 1.4+ standard logging.
四月 26, 2019 4:35:26 下午 com.mchange.v2.c3p0.C3P0Registry banner
信息: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
四月 26, 2019 4:35:26 下午 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 -> 1hgesxta2omujns157fk5m|4926097b, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.cj.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hgesxta2omujns157fk5m|4926097b, idleConnectionTestPeriod -> 0, initialPoolSize -> 5, jdbcUrl -> jdbc:mysql://localhost:3306/jdbctest?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8&autoReconnect=true, 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 ]
四月 26, 2019 4:35:46 下午 com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run
警告: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@4d9a1c8b -- APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks!
四月 26, 2019 4:35:46 下午 com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run
警告: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@4d9a1c8b -- APPARENT DEADLOCK!!! Complete Status:
Managed Threads: 3
Active Threads: 3
Active Tasks:
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@260f173 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0)
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@1f03cbf0 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2)
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@3328f4e3 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1)
Pending Tasks:
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@67a190d2
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@624f0e3c
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-#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)
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)
四月 26, 2019 4:36:46 下午 com.mchange.v2.async.ThreadPoolAsynchronousRunner processReplacedThreads
警告: Task com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@1f03cbf0 (in deadlocked PoolThread) failed to complete in maximum time 60000ms. Trying interrupt().
四月 26, 2019 4:36:46 下午 com.mchange.v2.async.ThreadPoolAsynchronousRunner processReplacedThreads
警告: Task com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@260f173 (in deadlocked PoolThread) failed to complete in maximum time 60000ms. Trying interrupt().
四月 26, 2019 4:36:46 下午 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask run
警告: com.mchange.v2.resourcepool.BasicResourcePool@75bd9247 -- Thread unexpectedly interrupted while performing an acquisition attempt.
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1805)
at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
四月 26, 2019 4:36:46 下午 com.mchange.v2.async.ThreadPoolAsynchronousRunner processReplacedThreads
警告: Task com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@3328f4e3 (in deadlocked PoolThread) failed to complete in maximum time 60000ms. Trying interrupt().
四月 26, 2019 4:36:46 下午 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask run
警告: com.mchange.v2.resourcepool.BasicResourcePool@75bd9247 -- Thread unexpectedly interrupted while performing an acquisition attempt.
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1805)
at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
四月 26, 2019 4:36:46 下午 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask run
警告: com.mchange.v2.resourcepool.BasicResourcePool@75bd9247 -- Thread unexpectedly interrupted while performing an acquisition attempt.
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1805)
at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
package com.hxh.utils;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
* JDBC的工具类
*/
public class JDBCUtils {
private static final String driverClass;
private static final String url;
private static final String username;
private static final String password;
static {
//加载属性文件并解析:
Properties properties = new Properties();
//如何获得属性文件的输入流?
//通常情况下使用类的加载器的方式进行获取
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
try {
properties.load(is);
} catch (Exception e) {
System.out.println("输入流发生错误");
}
driverClass = properties.getProperty("driverClass");
System.out.println(driverClass);
url = properties.getProperty("url");
System.out.println(url);
username = properties.getProperty("username");
System.out.println(username);
password = properties.getProperty("password");
System.out.println(password);
}
/**
* 注册驱动的方法
* @throws ClassNotFoundException
*
*/
public static void loadDriver() throws ClassNotFoundException {
Class.forName(driverClass);
}
/**
* 获得连接的方法
* @throws Exception
*/
public static Connection getConnection() throws Exception {
loadDriver();
Connection connection = DriverManager.getConnection(url,username,password);
return connection;
}
/**
* 获得Statement的方法
* @throws Exception
*
*/
public static Statement getStatement() throws Exception {
Connection connection = getConnection();
Statement statement = connection.createStatement();
return statement;
}
/**
* 资源释放
*
*/
public static void release(Statement statement,Connection connection) {
if(statement!=null) {
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
statement=null;
}
}
if(connection!=null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
connection=null;
}
}
}
public static void release(ResultSet resultSet,Statement statement,Connection connection) {
if(resultSet!=null) {
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
resultSet=null;
}
}
if(statement!=null) {
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
statement=null;
}
}
if(connection!=null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
connection=null;
}
}
}
}1回答
好帮手慕阿满
2019-04-26
同学你好,试了同学的代码并没有太大的问题,不过同学的JDBCUtils中的代码有点问题,使用c3p0数据库连接池后,不需要配置定义driverClass驱动等,这里建议同学新定义一个JDBCUtils1类,然后存放连接数据库连接池的配置信息,具体内容如下:
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class JDBCUtils1 {
private static final ComboPooledDataSource dataSource = new ComboPooledDataSource();
/**
* 获得连接的方法:
* @throws SQLException
*/
public static Connection getConnection() throws Exception{
Connection conn = dataSource.getConnection();
return conn;
}
/**
* 资源释放
*/
public static void release(Statement stmt,Connection conn){
if(stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
public static void release(ResultSet rs,Statement stmt,Connection conn){
if(rs!= null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
if(stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
}然后在DataSourceDemo1类中,使用JDBCUtils1调用release()方法再试试。
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
相似问题