Go The simplest application of the DBCP connection pool (for Oracle database)

Source: Internet
Author: User
Tags stmt

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

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

Given the question of DBCP directly for the JDBC connection, I made one of the simplest examples. All resources come from the Internet. It doesn't need a web container, it's a simple console app.

Resources:
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
Of course, there are ojdbc14.jar for Oracle JDBC (for oracle9i and above)

Project file: put it here. http://dl.iteye.com/topics/download/210279f0-f752-37a6-969f-d58ba13cc394

Database connection Information:
Jdbc:oracle:thin:scott/[email Protected]:1521:ora92
SEAN-M700 is the hostname and ora92 is the instance ID of the Oracle database. I don't have an Oracle database installed on my machine, using a oracle9.2 copy of a long-ago, reinstalling the instance and the corresponding service.

The source code is as follows: Borrow the Buddha, the source code is also obtained from the Internet. (http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/doc/basicdatasourceexample.java?revision=1100136& View=markup)

[Java]View PlainCopyprint?
  1. /*
  2. //
  3. *//Here's a simple example of the Basicdatasource.
  4. 34//
  5. 35
  6. 36//
  7. PNS//Note that this example are very similiar to the Poolingdriver
  8. //Example.
  9. 39
  10. 40//
  11. Compile this example and you ll want:
  12. Commons-pool-1.5.6.jar//*
  13. //* Commons-dbcp-1.3.jar (JDK 1.4-1.5) or commons-dbcp-1.4 (JDK 1.6+)
  14. * J2ee.jar (for the javax.sql classes)
  15. Classpath//In your.
  16. 46//
  17. A/To run this example, you ll want:
  18. //* Commons-pool-1.5.6.jar
  19. //* Commons-dbcp-1.3.jar (JDK 1.4-1.5) or commons-dbcp-1.4 (JDK 1.6+)
  20. /* J2ee.jar (for the javax.sql classes)
  21. Wuyi//* The classes for your (underlying) JDBC driver
  22. //In your classpath.
  23. 53//
  24. Wu//Invoke the class using the arguments:
  25. * The connect string for your underlying JDBC driver
  26. +/* The query you ' d like to execute
  27. //You'll also want to ensure your underlying JDBC driver
  28. +//IS registered. can use the "jdbc.drivers"
  29. A.
  30. 60//
  31. Example//For:
  32. //java-djdbc.drivers=oracle.jdbc.driver.oracledriver \
  33. +//-classpath Commons-pool-1.5.6.jar:commons-dbcp-1.4.jar:j2ee.jar:oracle-jdbc.jar:. \
  34. //Poolingdatasourceexample
  35. +//"Jdbc:oracle:thin:scott/[email Protected]:1521:mysid"
  36. //"SELECT * from DUAL"
  37. */
  38. /*
  39. The Oracle connection URL for the thin client-side driver Ojdbc14.jar have the following format:
  40. jdbc:oracle:thin:[user/password]@[host][:p Ort]:sid
  41. jdbc:oracle:thin:[user/password]@//[host][:p Ort]/sid
  42. User-the login user name defined in the Oracle server.
  43. password-the password for the login user.
  44. Host-the host name where Oracle server is running.
  45. Default is 127.0.0.1-the IP address of localhost.
  46. Port-the port number where Oracle is listening for connection.
  47. Default is 1521.
  48. Sid-system ID of the Oracle server database instance.
  49. SID is a required value. By default, Oracle Database 10g Express
  50. Edition creates one database instance called XE.
  51. */
  52. Import Org.apache.commons.dbcp.BasicDataSource;
  53. Import javax.sql.*;
  54. Import java.sql.*;
  55. Public class Testdatasource
  56. {
  57. /**  
  58. * @param args
  59. */
  60. public static void Main (string[] args)
  61. {
  62. System.out.println ("Setting up data source.");
  63. String url = "Jdbc:oracle:thin:scott/[email protected]:1521:ora92";
  64. DataSource DataSource = setupdatasource (URL);
  65. System.out.println ("done ...");
  66. //Now, we can use JDBC DataSource as we normally would.
  67. //    
  68. Connection conn = null;
  69. Statement stmt = null;
  70. ResultSet rset = null;
  71. try {
  72. System.out.println ("Creating connection.");
  73. conn = Datasource.getconnection ();
  74. System.out.println ("Creating statement.");
  75. stmt = Conn.createstatement ();
  76. SYSTEM.OUT.PRINTLN ("executing statement.");
  77. RSet = Stmt.executequery ("Select 1 from DUAL");
  78. System.out.println ("Results:");
  79. int numcols = Rset.getmetadata (). getColumnCount ();
  80. While (Rset.next ()) {
  81. For (int i=1;i<=numcols;i++) {
  82. System.out.print ("\ T" + rset.getstring (i));
  83. }
  84. System.out.println ("");
  85. }
  86. } catch (SQLException e) {
  87. E.printstacktrace ();
  88. } finally {
  89. try { if (rset! = null) rset.close ();} catch (Exception e) {}
  90. try { if (stmt! = null) stmt.close ();} catch (Exception e) {}
  91. try { if (conn! = null) conn.close ();} catch (Exception e) {}
  92. }
  93. }
  94. public static DataSource Setupdatasource (String connecturi) {
  95. Basicdatasource ds = new Basicdatasource ();
  96. Ds.setdriverclassname ("Oracle.jdbc.driver.OracleDriver");
  97. Ds.setusername ("Scott");
  98. Ds.setpassword ("Tiger");
  99. Ds.seturl (Connecturi);
  100. return DS;
  101. }
  102. public static void Printdatasourcestats (DataSource ds) {
  103. Basicdatasource BDS = (basicdatasource) DS;
  104. System.out.println ("numactive:" + bds.getnumactive ());
  105. System.out.println ("numidle:" + bds.getnumidle ());
  106. }
  107. public static void Shutdowndatasource (DataSource ds) throws SQLException {
  108. Basicdatasource BDS = (basicdatasource) DS;
  109. Bds.close ();
  110. }
  111. }


However, it is necessary to note that the DBCP connection pool is the most unsuitable for production environments in several open source connection pools, and there are often dead connections. and Cp30 and Proxool are good choices. DBCP is used to assess the development environment, or is more convenient.

Go The simplest application of the DBCP connection pool (for Oracle database)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.