使用配置文件的方式查询不出数据,但是手动设置连接池可以查询出数据
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 changyulong
*
*/
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);
}
}
@Test
/**
* 手动设置连接池
*/
public void demo1() {
//获得连接
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
//创建连接池
ComboPooledDataSource dataSource = new ComboPooledDataSource();
//设置连接池的参数
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql:///jdbctest");
dataSource.setUser("root");
dataSource.setPassword("langmuir");
dataSource.setMaxPoolSize(20);
dataSource.setInitialPoolSize(3);
//获得连接
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);
}
}
}
打印日志:
Feb 19, 2019 6:39:03 PM com.mchange.v2.log.MLog <clinit>
信息: MLog clients using java 1.4+ standard logging.
Feb 19, 2019 6:39:03 PM com.mchange.v2.c3p0.C3P0Registry banner
信息: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
Feb 19, 2019 6:39:04 PM 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 -> 1hge2wka11hbd7u1t9xf0u|53bd815b, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> null, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hge2wka11hbd7u1t9xf0u|53bd815b, 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 ]
正在回答 回答被采纳积分+1
你好同学,如下图所示,我测试了你的代码是可以获取数据的呀,
建议你将如下代码粘贴到自己的编译器中再试试。祝学习愉快~
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 changyulong * */ 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); } } @Test /** * 手动设置连接池 */ public void demo1() { //获得连接 Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { //创建连接池 ComboPooledDataSource dataSource = new ComboPooledDataSource(); //设置连接池的参数 dataSource.setDriverClass("com.mysql.jdbc.Driver"); dataSource.setJdbcUrl("jdbc:mysql:///jdbctest"); dataSource.setUser("root"); dataSource.setPassword("root"); dataSource.setMaxPoolSize(20); dataSource.setInitialPoolSize(3); //获得连接 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); } } }
建议同学粘贴一下配置文件的代码,方便我们检查并具有针对性的为你解答哦,祝学习愉快~
- 参与学习 人
- 提交作业 277 份
- 解答问题 4297 个
Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星