以下代码使用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!
找了好久没发现那里错了,求大神帮忙,感激不尽!
|