黑马程序员技术交流社区

标题: 使用c3p0读取配置文件无法访问数据库 [打印本页]

作者: 836563696    时间: 2016-10-20 08:47
标题: 使用c3p0读取配置文件无法访问数据库
以下代码使用c3p0没有问题
[AppleScript] 纯文本查看 复制代码
@Test
        public void demo1() throws PropertyVetoException, SQLException {
                //得到连接池
                ComboPooledDataSource cpds = new ComboPooledDataSource();
                cpds.setDriverClass("com.mysql.jdbc.Driver"); //注册驱动            
                cpds.setJdbcUrl("jdbc:mysql://localhost:3306/mydb1");
                cpds.setUser("root");                                 
                cpds.setPassword("123");
                //获取连接
                Connection con = cpds.getConnection();
                //固定格式sql语句 ,执行语句
                String sql = "select product from orders where price= ? ";
                PreparedStatement sta = con.prepareStatement(sql);
                sta.setString(1, "100");
                //获取结果集
                ResultSet res = sta.executeQuery();
                while(res.next()) {
                        System.out.println(res.getString(1));
                }
                //释放资源
                JdbcUtil.release(res, sta, con);
        }

然后换了读取xml文件的方式
xml文件如下:
[AppleScript] 纯文本查看 复制代码
<c3p0-config>
  <default-config>
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb1</property>
    <property name="user">root</property>
    <property name="password">123</property>
  </default-config>

  <!-- This app is massive! -->
  <named-config name="jdbc">
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb1</property>
    <property name="user">root</property>
    <property name="password">123</property>

    <!-- intergalactoApp adopts a different approach to configuring statement caching -->
    <property name="maxStatements">0</property>
    <property name="maxStatementsPerConnection">5</property>

  </named-config>
</c3p0-config>

运行出现错误:
java.sql.SQLException: Connections could not be acquired from the underlying database!
找了好久没发现那里错了,求大神帮忙,感激不尽!


作者: wangyuit    时间: 2016-10-20 08:57
启动报什么错了

作者: 836563696    时间: 2016-10-20 10:10
终于找到了,在文件名中多了一个空格,好坑啊{:2_43:}






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2