标签:地方 ack cat data class 查看 关闭 exception prope
需要导入的包
创建c3p0-config.xml文件,名字不能改动,并且放到src下,c3p0包会自动到src下查找c3p0-config.xml,名字错了,地方不对都不能配置成功
配置
<c3p0-config>
<!-- 默认配置,如果没有指定使用则使用这个配置 -->
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
<property name="user">root</property>
<property name="password"></property>
<property name="acquireIncrement">5</property>
<property name="initialPoolSize">10</property>
<property name="minPoolSize">10</property>
<property name="maxPoolSize">50</property>
<property name="maxStatements">5</property>
<property name="maxStatementsPerConnection">5</property>
</default-config>
</c3p0-config>
或者到官网查看标准配置 C3P0
public class C3P0Util {
private static ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
public static Connection getConnection(){
try {
return comboPooledDataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
public class Test {
public static void main(String[] args) {
//这里的关闭函数被c3p0动态代理了,被改写为放入连接池
try(Connection conn = C3P0Util.getConnection()) {
String sql = "INSERT INTO account (`money`) VALUES (100)";
PreparedStatement ps = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
if (ps.executeUpdate() > 0){
ResultSet rs1 = ps.getGeneratedKeys();
while(rs1.next()){
System.out.println(rs1.getInt(1));
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
输出结果
9
s#返回的主键
#测试成功
标签:地方 ack cat data class 查看 关闭 exception prope
原文地址:https://www.cnblogs.com/Howlet/p/12063609.html