[轉]DBCP串連池的最簡單應用(用於ORACLE資料庫)

來源:互聯網
上載者:User

標籤:

http://blog.csdn.net/iihero/article/details/8254107

 http://www.programgo.com/article/81693457907/

鑒於有人問起DBCP直接用於JDBC串連的問題,我做了一個最簡單的樣本。所有資源來源於網上。它不需要什麼Web容器,就是一簡單的控制台應用。

資源:
http://apache.etoak.com//commons/pool/binaries/commons-pool-1.5.6-bin.zip
http://labs.renren.com/apache-mirror//commons/dbcp/binaries/commons-dbcp-1.4-bin.zip
http://download.java.net/maven/1/javaee/jars/javaee-api-5.jar
當然,還有oracle jdbc要用的ojdbc14.jar (適用於oracle9i及以上版本)

工程檔案:放到這裡了。http://dl.iteye.com/topics/download/210279f0-f752-37a6-969f-d58ba13cc394

資料庫連接資訊:
jdbc:oracle:thin:scott/[email protected]:1521:ora92
sean-m700是主機名稱,ora92是oracle資料庫的instance ID. 我手頭的機器上沒有安裝oracle資料庫,用的是很早以前的一個oracle9.2的拷貝,重新安裝執行個體和相應服務得來的。 

源碼如下:借化獻佛,源碼也是從網上得來的。(http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/doc/BasicDataSourceExample.Java?revision=1100136&view=markup)

 

[java] view plain copy  print?
  1. /*  
  2. //  
  3. 33  // Here‘s a simple example of how to use the BasicDataSource.  
  4. 34  //  
  5. 35    
  6. 36  //  
  7. 37  // Note that this example is very similiar to the PoolingDriver  
  8. 38  // example.  
  9. 39    
  10. 40  //  
  11. 41  // To compile this example, you‘ll want:  
  12. 42  //  * commons-pool-1.5.6.jar  
  13. 43  //  * commons-dbcp-1.3.jar (JDK 1.4-1.5) or commons-dbcp-1.4 (JDK 1.6+)  
  14. 44  //  * j2ee.jar (for the javax.sql classes)  
  15. 45  // in your classpath.  
  16. 46  //  
  17. 47  // To run this example, you‘ll want:  
  18. 48  //  * commons-pool-1.5.6.jar  
  19. 49  //  * commons-dbcp-1.3.jar (JDK 1.4-1.5) or commons-dbcp-1.4 (JDK 1.6+)  
  20. 50  //  * j2ee.jar (for the javax.sql classes)  
  21. 51  //  * the classes for your (underlying) JDBC driver  
  22. 52  // in your classpath.  
  23. 53  //  
  24. 54  // Invoke the class using two arguments:  
  25. 55  //  * the connect string for your underlying JDBC driver  
  26. 56  //  * the query you‘d like to execute  
  27. 57  // You‘ll also want to ensure your underlying JDBC driver  
  28. 58  // is registered.  You can use the "jdbc.drivers"  
  29. 59  // property to do this.  
  30. 60  //  
  31. 61  // For example:  
  32. 62  //  java -Djdbc.drivers=oracle.jdbc.driver.OracleDriver \  
  33. 63  //       -classpath commons-pool-1.5.6.jar:commons-dbcp-1.4.jar:j2ee.jar:oracle-jdbc.jar:. \  
  34. 64  //       PoolingDataSourceExample  
  35. 65  //       "jdbc:oracle:thin:scott/[email protected]:1521:mysid"  
  36. 66  //       "SELECT * FROM DUAL"  
  37. */    
  38. /*  
  39. The Oracle connection URL for the thin client-side driver ojdbc14.jar has the following format:  
  40. jdbc:oracle:thin:[user/password]@[host][:port]:SID  
  41. jdbc:oracle:thin:[user/password]@//[host][:port]/SID  
  42.   
  43.   user - The login user name defined in the Oracle server.  
  44.   
  45.   password - The password for the login user.  
  46.   
  47.   host - The host name where Oracle server is running.   
  48.          Default is 127.0.0.1 - the IP address of localhost.  
  49.   
  50.   port - The port number where Oracle is listening for connection.  
  51.          Default is 1521.  
  52.   
  53.   SID  - System ID of the Oracle server database instance.   
  54.          SID is a required value. By default, Oracle Database 10g Express   
  55.          Edition creates one database instance called XE.  
  56. */    
  57.     
  58. import org.apache.commons.dbcp.BasicDataSource;    
  59. import javax.sql.*;    
  60. import java.sql.*;    
  61.     
  62. public class TestDataSource    
  63. {    
  64.     
  65.     /**  
  66.      * @param args  
  67.      */    
  68.     public static void main(String[] args)    
  69.     {    
  70.         System.out.println("Setting up data source.");    
  71.         String url = "jdbc:oracle:thin:scott/[email protected]:1521:ora92";    
  72.         DataSource dataSource = setupDataSource(url);    
  73.         System.out.println("Done...");    
  74.     
  75.         // Now, we can use JDBC DataSource as we normally would.    
  76.         //    
  77.         Connection conn = null;    
  78.         Statement stmt = null;    
  79.         ResultSet rset = null;    
  80.     
  81.         try {    
  82.             System.out.println("Creating connection.");    
  83.             conn = dataSource.getConnection();    
  84.             System.out.println("Creating statement.");    
  85.             stmt = conn.createStatement();    
  86.             System.out.println("Executing statement.");    
  87.             rset = stmt.executeQuery("select 1 from DUAL");    
  88.             System.out.println("Results:");    
  89.             int numcols = rset.getMetaData().getColumnCount();    
  90.             while(rset.next()) {    
  91.                 for(int i=1;i<=numcols;i++) {    
  92.                     System.out.print("\t" + rset.getString(i));    
  93.                 }    
  94.                 System.out.println("");    
  95.             }    
  96.         } catch(SQLException e) {    
  97.             e.printStackTrace();    
  98.         } finally {    
  99.             try { if (rset != null) rset.close(); } catch(Exception e) { }    
  100.             try { if (stmt != null) stmt.close(); } catch(Exception e) { }    
  101.             try { if (conn != null) conn.close(); } catch(Exception e) { }    
  102.         }    
  103.     }    
  104.     
  105.     public static DataSource setupDataSource(String connectURI) {    
  106.         BasicDataSource ds = new BasicDataSource();    
  107.         ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");    
  108.         ds.setUsername("scott");    
  109.         ds.setPassword("tiger");    
  110.         ds.setUrl(connectURI);    
  111.         return ds;    
  112.     }    
  113.     
  114.     public static void printDataSourceStats(DataSource ds) {    
  115.         BasicDataSource bds = (BasicDataSource) ds;    
  116.         System.out.println("NumActive: " + bds.getNumActive());    
  117.         System.out.println("NumIdle: " + bds.getNumIdle());    
  118.     }    
  119.     
  120.     public static void shutdownDataSource(DataSource ds) throws SQLException {    
  121.         BasicDataSource bds = (BasicDataSource) ds;    
  122.         bds.close();    
  123.     }    
  124.     
  125. }    


不過,需要說明的是,DBCP串連池是幾個開源串連池裡最不適合用於生產環境的,經常會出現死串連現象。 而cp30和proxool都是不錯的選擇。DBCP用於測評開發環境,還是比較便利的。

[轉]DBCP串連池的最簡單應用(用於ORACLE資料庫)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.