c3p0 JDBC鏈接池 xml配置文件的書寫

須要建立  c3p0-config.xml 配置文件java

  * c3p0配置文件
  * 1.配置文件名稱:c3p0-config.xml
  * 2.配置文件的位置必定要在類路徑下mysql

複製sql

修改文件數據庫

首字母的大寫改爲小寫ide

配置文件設置完成spa

記得導入數據庫鏈接jar包code

              c3p0  jar包xml

 

 

代碼對象

 1 package test03;
 2 
 3 import java.beans.PropertyVetoException;
 4 import java.sql.Connection;
 5 import java.sql.ResultSet;
 6 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     private static 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");
29         pool.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 }
c3p0鏈接池

 

 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();blog

相關文章
相關標籤/搜索