Mysql配置C3P0
需要导入的包
- c3p0-0.9.5.2.jar
- mchange-commons-0.2.15.jar
- mysql-connector.jar
1. 配置xml
创建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
2. C3P0Util工具类
public class C3P0Util { private static ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource(); public static Connection getConnection(){ try { return comboPooledDataSource.getConnection(); } catch (SQLException e) { e.printStackTrace(); } return null; } }
3. 测试
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#返回的主键 #测试成功
相关推荐
thunderstorm 2020-06-06
langyue 2020-05-31
鲁氏汤包王 2020-04-18
favouriter 2020-04-18
jakefei 2020-01-05
lightlanguage 2019-12-22
点滴技术生活 2019-12-05
herohope 2019-11-10
AKbiubiu 2016-07-06
Ali 2012-05-23
bigcactus 2012-04-17
朝军 2012-03-13
WilliamLin 2010-04-28
wujun 2011-09-14
pkzdz 2015-08-31
易鲸捷大数据库 2015-08-20