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?
- /*
- //
- *//Here's a simple example of the Basicdatasource.
- 34//
- 35
- 36//
- PNS//Note that this example are very similiar to the Poolingdriver
- //Example.
- 39
- 40//
- Compile this example and you ll want:
- Commons-pool-1.5.6.jar//*
- //* Commons-dbcp-1.3.jar (JDK 1.4-1.5) or commons-dbcp-1.4 (JDK 1.6+)
- * J2ee.jar (for the javax.sql classes)
- Classpath//In your.
- 46//
- A/To run this example, you ll want:
- //* Commons-pool-1.5.6.jar
- //* Commons-dbcp-1.3.jar (JDK 1.4-1.5) or commons-dbcp-1.4 (JDK 1.6+)
- /* J2ee.jar (for the javax.sql classes)
- Wuyi//* The classes for your (underlying) JDBC driver
- //In your classpath.
- 53//
- Wu//Invoke the class using the arguments:
- * The connect string for your underlying JDBC driver
- +/* The query you ' d like to execute
- //You'll also want to ensure your underlying JDBC driver
- +//IS registered. can use the "jdbc.drivers"
- A.
- 60//
- Example//For:
- //java-djdbc.drivers=oracle.jdbc.driver.oracledriver \
- +//-classpath Commons-pool-1.5.6.jar:commons-dbcp-1.4.jar:j2ee.jar:oracle-jdbc.jar:. \
- //Poolingdatasourceexample
- +//"Jdbc:oracle:thin:scott/[email Protected]:1521:mysid"
- //"SELECT * from DUAL"
- */
- /*
- The Oracle connection URL for the thin client-side driver Ojdbc14.jar have the following format:
- jdbc:oracle:thin:[user/password]@[host][:p Ort]:sid
- jdbc:oracle:thin:[user/password]@//[host][:p Ort]/sid
- User-the login user name defined in the Oracle server.
- password-the password for the login user.
- Host-the host name where Oracle server is running.
- Default is 127.0.0.1-the IP address of localhost.
- Port-the port number where Oracle is listening for connection.
- Default is 1521.
- Sid-system ID of the Oracle server database instance.
- SID is a required value. By default, Oracle Database 10g Express
- Edition creates one database instance called XE.
- */
- Import Org.apache.commons.dbcp.BasicDataSource;
- Import javax.sql.*;
- Import java.sql.*;
- Public class Testdatasource
- {
- /**
- * @param args
- */
- public static void Main (string[] args)
- {
- System.out.println ("Setting up data source.");
- String url = "Jdbc:oracle:thin:scott/[email protected]:1521:ora92";
- DataSource DataSource = setupdatasource (URL);
- System.out.println ("done ...");
- //Now, we can use JDBC DataSource as we normally would.
- //
- Connection conn = null;
- Statement stmt = null;
- ResultSet rset = null;
- try {
- System.out.println ("Creating connection.");
- conn = Datasource.getconnection ();
- System.out.println ("Creating statement.");
- stmt = Conn.createstatement ();
- SYSTEM.OUT.PRINTLN ("executing statement.");
- RSet = Stmt.executequery ("Select 1 from DUAL");
- System.out.println ("Results:");
- int numcols = Rset.getmetadata (). getColumnCount ();
- While (Rset.next ()) {
- For (int i=1;i<=numcols;i++) {
- System.out.print ("\ T" + rset.getstring (i));
- }
- System.out.println ("");
- }
- } catch (SQLException e) {
- E.printstacktrace ();
- } finally {
- try { if (rset! = null) rset.close ();} catch (Exception e) {}
- try { if (stmt! = null) stmt.close ();} catch (Exception e) {}
- try { if (conn! = null) conn.close ();} catch (Exception e) {}
- }
- }
- public static DataSource Setupdatasource (String connecturi) {
- Basicdatasource ds = new Basicdatasource ();
- Ds.setdriverclassname ("Oracle.jdbc.driver.OracleDriver");
- Ds.setusername ("Scott");
- Ds.setpassword ("Tiger");
- Ds.seturl (Connecturi);
- return DS;
- }
- public static void Printdatasourcestats (DataSource ds) {
- Basicdatasource BDS = (basicdatasource) DS;
- System.out.println ("numactive:" + bds.getnumactive ());
- System.out.println ("numidle:" + bds.getnumidle ());
- }
- public static void Shutdowndatasource (DataSource ds) throws SQLException {
- Basicdatasource BDS = (basicdatasource) DS;
- Bds.close ();
- }
- }
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)