使用配置文件的方式查询不出数据,但是手动设置连接池可以查询出数据

使用配置文件的方式查询不出数据,但是手动设置连接池可以查询出数据

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

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

2回答
chrismorgen 2019-02-20 15:14:47

你好同学,如下图所示,我测试了你的代码是可以获取数据的呀,

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

建议你将如下代码粘贴到自己的编译器中再试试。祝学习愉快~

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);

        }

    }

}


chrismorgen 2019-02-19 19:05:24

建议同学粘贴一下配置文件的代码,方便我们检查并具有针对性的为你解答哦,祝学习愉快~

  • 提问者 慕斯8050152 #1
    配置文件 <?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql:///jdbctest</property> <property name="user">root</property> <property name="password">langmuir</property> <property name="initialPoolSize">5</property> <property name="maxPoolSize">20</property> </default-config> </c3p0-config>
    2019-02-20 09:23:39
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

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

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

在线咨询

领取优惠

免费试听

领取大纲

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