Java 中常用的資料來源

來源:互聯網
上載者:User

標籤:source   auth   targe   com   attr   lib   type   name   copyto   

資料來源:儲存了所有建立資料庫連接的資訊。就象通過指定檔案名稱你可以在檔案系統中找到檔案一樣,通過提供正確的資料來源名稱,你可以找到相應的資料庫連接。

1.JNDI方式建立DataSource

  1.1 配置資料來源的相關串連資訊,該配置可以在Tomcat安裝目錄下的conf/context.xml檔案中配置。
其配置如下:
  

[html] view plain copy 
  1. <Context>  
  2.     ……  
  3.    <!-- MySql -->  
  4.    <Resource name="jdbc/orclight" auth="Container"  
  5.   
  6.            type="javax.sql.DataSource"maxActive="100" maxIdle="30"  
  7.   
  8.            maxWait="10000"username="root" password="root"  
  9.   
  10.            driverClassName="com.mysql.jdbc.Driver"  
  11.   
  12.            url="jdbc:mysql://localhost:3306/orclight"/>  
  13.     ……  
  14. </Context>  

  1.2 在程式中以JNDI的方式建立資料來源,得到資料庫連接已進行相應的操作

代碼如下:      

 

[java] view plain copy 
  1. / 初始化JNDI上下文,建立DataSource對象  
  2.   
  3.     Context initContext = new InitialContext();  
  4.   
  5.     Context context = (Context)initContext.lookup("java:comp/env");  
  6.   
  7.            DataSourcedataSource =  (DataSource)context.lookup("jdbc/orclight");  

 

2.Apache提供的DBCP方式建立資料來源 

  2.1 以這種方式建立資料來源必須先準備兩個jar檔案:commons-dbcp.jar 和 commons-pool.jar。

  2.2 以這種方式建立的資料來源就不再是javax.sql.DataSource。DataSource了,而是org.apache.commons.dbcp.BasicDataSource。

代碼如下:

    

[java] view plain copy 
  1. // 建立BasicDataSource對象  
  2.   
  3.     BasicDataSource ds = new BasicDataSource();  
  4.   
  5.     ds.setDriverClassName("com.mysql.jdbc.Driver");  
  6.   
  7.     ds.setUrl("jdbc:mysql://localhost:3306/orclight");  
  8.   
  9.     ds.setUsername("root");  
  10.   
  11.     ds.setPassword("root");  
  12.   
  13.     ds.setInitialSize(50);  
  14.   
  15.     ds.setMaxActive(100);  
  16.     ds.setMaxIdle(30);  
  17.     ds.setMaxWait(10000);     
  18.     // 關閉資料來源串連  
  19.     ds.close();  

 

3.C3P0方式建立資料來源

  3.1 使用C3P0方式建立資料來源應該首先準備一個jar檔案:c3p0-0.9.1.2.jar,將其放到web/lib目錄下,

就可以在項目中使用C3P0建立資料來源

  3.2 3.2 C3P0建立的資料來源對象也不是DataSource對象,而是ComboPooledDataSource。

代碼如下:

    

[java] view plain copy 
    1. // 建立ComboPooledDataSource對象  
    2.   
    3.    ComboPooledDataSource ds = new ComboPooledDataSource();  
    4.   
    5.    ds.setDriverClass("com.mysql.jdbc.Driver");  
    6.   
    7.    ds.setJdbcUrl("jdbc:mysql://localhost:3306/orclight");  
    8.   
    9.    ds.setUser("root");  
    10.   
    11.    ds.setPassword("root");  
    12.   
    13.    ds.setInitialPoolSize(50);  
    14.   
    15.    ds.setMaxPoolSize(100);  
    16.   
    17.    ds.setMaxIdleTime(10000);  

Java 中常用的資料來源

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.