0%
Theme NexT works best with JavaScript enabled
前言 C3P0数据库连接池学习笔记
导入jar包
将c3p0-0.9.1.2.jar
导入到项目的/WebContent/WEB-INF/lib
目录下
方法一:在程序中设定连接参数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 public class TestC3P0 { ComboPooledDataSource pool = new ComboPooledDataSource (); @Test public void testFindAll () { try { pool.setDriverClass("com.mysql.cj.jdbc.Driver" ); pool.setJdbcUrl("jdbc:mysql://localhost:3306/jt_db?characterEncoding=utf-8&serverTimezone=UTC" ); pool.setUser("root" ); pool.setPassword("123456" ); Connection connection = pool.getConnection(); PreparedStatement ps = connection.prepareStatement("SELECT * FROM user" ); ResultSet rs = ps.executeQuery(); while (rs.next()) { int id = rs.getInt("id" ); String username = rs.getString("username" ); String password = rs.getString("password" ); System.out.println(id+"\t" +username+"\t" +password); } rs.close(); ps.close(); connection.close(); } catch (Exception e) { e.printStackTrace(); } } }
方法二:在配置文件设定参数(properties)
1 2 3 4 c3p0.driverClass =com.mysql.cj.jdbc.Driver c3p0.jdbcUrl =jdbc:mysql://localhost:3306/jt_db?serverTimezone=UTC c3p0.user =root c3p0.password =123456
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 public class TestC3P0 { ComboPooledDataSource pool = new ComboPooledDataSource (); @Test public void testFindAll () { try { Connection connection = pool.getConnection(); PreparedStatement ps = connection.prepareStatement("SELECT * FROM user" ); ResultSet rs = ps.executeQuery(); while (rs.next()) { int id = rs.getInt("id" ); String username = rs.getString("username" ); String password = rs.getString("password" ); System.out.println(id+"\t" +username+"\t" +password); } rs.close(); ps.close(); connection.close(); } catch (Exception e) { e.printStackTrace(); } } }
方法三:在配置文件设定参数(xml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <?xml version="1.0" encoding="UTF-8" ?> <c3p0-config > <default-config > <property name ="driverClass" > com.mysql.cj.jdbc.Driver </property > <property name ="jdbcUrl" > jdbc:mysql://localhost:3306/jt_db?serverTimezone=UTC </property > <property name ="user" > root </property > <property name ="password" > 123456 </property > </default-config > </c3p0-config >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 public class TestC3P0 { ComboPooledDataSource pool = new ComboPooledDataSource (); @Test public void testFindAll () { try { Connection connection = pool.getConnection(); PreparedStatement ps = connection.prepareStatement("SELECT * FROM user" ); ResultSet rs = ps.executeQuery(); while (rs.next()) { int id = rs.getInt("id" ); String username = rs.getString("username" ); String password = rs.getString("password" ); System.out.println(id+"\t" +username+"\t" +password); } rs.close(); ps.close(); connection.close(); } catch (Exception e) { e.printStackTrace(); } } }
更多配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 <c3p0-config > <default-config > <property name ="initialPoolSize" > 3</property > <property name ="acquireIncrement" > 3</property > <property name ="acquireRetryAttempts" > 30</property > <property name ="maxIdleTime" > 60</property > <property name ="maxPoolSize" > 15</property > <property name ="idleConnectionTestPeriod" > 60</property > <property name ="acquireRetryDelay" > 1000</property > <property name ="autoCommitOnClose" > false</property > <property name ="automaticTestTable" > Test</property > <property name ="breakAfterAcquireFailure" > false</property > <property name ="checkoutTimeout" > 100</property > <property name ="connectionTesterClassName" > </property > <property name ="factoryClassLocation" > null</property > <property name ="forceIgnoreUnresolvedTransactions" > false</property > <property name ="maxStatements" > 100</property > <property name ="maxStatementsPerConnection" > </property > <property name ="numHelperThreads" > 3</property > <property name ="overrideDefaultUser" > root</property > <property name ="overrideDefaultPassword" > password</property > <property name ="preferredTestQuery" > select id from test where id=1</property > <property name ="propertyCycle" > 300</property > <property name ="testConnectionOnCheckout" > false</property > <property name ="testConnectionOnCheckin" > true</property > <property name ="usesTraditionalReflectiveProxies" > false</property > <property name ="automaticTestTable" > con_test</property > <property name ="checkoutTimeout" > 30000</property > <property name ="idleConnectionTestPeriod" > 30</property > <property name ="initialPoolSize" > 10</property > <property name ="maxIdleTime" > 30</property > <property name ="maxPoolSize" > 25</property > <property name ="minPoolSize" > 10</property > <property name ="maxStatements" > 0</property > </default-config > </c3p0-config >
完成