c3p0 JDBC連接池 xml配置文件的書寫
阿新 • • 發佈:2018-05-12
配置 clas out jar包 圖片 大寫 system new mchange
需要創建 c3p0-config.xml 配置文件
* c3p0配置文件
* 1.配置文件名稱:c3p0-config.xml
* 2.配置文件的位置一定要在類路徑下
復制
修改文件
首字母的大寫改成小寫
配置文件設置完成
記得導入數據庫連接jar包
c3p0 jar包
代碼
1 package test03; 2 3 import java.beans.PropertyVetoException; 4 import java.sql.Connection; 5 import java.sql.ResultSet; 6c3p0連接池import java.sql.SQLException; 7 8 import com.mchange.v2.c3p0.ComboPooledDataSource; 9 10 /** 11 * C3p0連接數據源 12 * @author star 13 * 14 */ 15 public class C3p0Test { 16 public static void main(String[] args) throws Exception { 17 //test1(); 18 test2(); 19 } 20 21 privatestatic void test1() throws SQLException, PropertyVetoException { 22 // TODO Auto-generated method stub 23 //創建連接池 24 ComboPooledDataSource pool = new ComboPooledDataSource(); 25 26 //設置的連接的四大參數 27 // 28 pool.setDriverClass("com.mysql.jdbc.Driver"); 29pool.setJdbcUrl("jdbc:mysql:///day01"); 30 pool.setUser("root"); 31 pool.setPassword("root"); 32 // 33 //獲取連接 34 Connection conn = pool.getConnection(); 35 String sql = "select * from stu"; 36 ResultSet rs = conn.createStatement().executeQuery(sql); 37 while(rs.next()) { 38 System.out.println(rs.getInt(1)+" "+rs.getString(2)); 39 } 40 rs.close(); 41 conn.close(); 42 } 43 //使用配置文件 44 /* 45 * c3p0配置文件 46 * 1.配置文件名稱:c3p0-config.xml 47 * 2.配置文件的位置一定要在類路徑下 48 */ 49 private static void test2() throws Exception { 50 // TODO Auto-generated method stub 51 //c3p0 創建連接池對象 52 ComboPooledDataSource pool = new ComboPooledDataSource(); 53 //獲取連接 54 Connection conn = pool.getConnection(); 55 String sql = "select * from stu"; 56 ResultSet rs = conn.createStatement().executeQuery(sql); 57 while(rs.next()){ 58 System.out.println(rs.getInt(1)+" "+rs.getString(2)); 59 } 60 rs.close(); 61 conn.close(); 62 63 } 64 65 }
1 <c3p0-config> 2 <default-config> 3 <property name="driverClass">com.mysql.jdbc.Driver</property> 4 <property name="jdbcUrl">jdbc:mysql:///day01</property> 5 <property name="user">root</property> 6 <property name="password">root</property> 7 8 9 </default-config> 10 </c3p0-config>c3p0-config.xml
//c3p0 創建連接池對象
ComboPooledDataSource pool = new ComboPooledDataSource();
c3p0 JDBC連接池 xml配置文件的書寫