Jakarta Commons-Dbcp1.dbcp Introduction to the design of the network program, many of the interactive operations of the relational database, the general operation mode creates a connection to the database when used, and then performs various operations, this simple operation The problem brought about by the way is the frequent opening and closing of the database, as well as the implementation of TRANSTION, which is a very time and resource operation. So the database buffer pool DBCP appears, this package is developed from the POOL package. 2.DBCP basic process implemented in Example 1. Create enericObjectPool GenericObjectPool pool = new GenericObjectPool (null); 2. Create PoolableConnectionFactory DriverManagerConnectionFactory cf = new DriverManagerConnectionFactory ( "jdbc: mysql: // host / db", "username", "password" );
PoolableConnectionFactory pcf = new PoolableConnectionFactory (CF, pool, null, "SELECT * FROM mysql.db", false, true);. 3. Create and registered PoolingDriver new PoolingDriver () registerPool ( "myPool", pool); so far, The database buffer pool is created. 3. DataSource's implementation process DataSource has been implemented in numerous JSP containers and applications, when the container or application server is started, he read the relevant profile and automatically completes DataSource Creating a job, we generally only need to specify in the server's configuration file, which is posted here, which is convenient for everyone to understand its internal implementation mechanism for easy programming references. import javax.sql.DataSource; import java.sql.Connection; import java.sql.Statement; import java.sql.ResultSet; import java.sql.SQLException; import org.apache.commons.dbcp.BasicDataSource; public class BasicDataSourceExample {
Public static void main (String [] args) {// creates BasicDataSource System.out.println ("Setting Up Data Source."); DataSource Datasource = SetupDataSource (Args [0]); System.out.Println ("DONE. ");
// Create a JDBC Data Source 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 (args [1]); system.out.println ("Results:"); int number (). GetColumncount (); while (rset.next) )) {For (INT i = 1; i <= number) {system.out.print ("/ t" rset.getstring (i));} system.out.println (");}} Catch (Sqlexception E) {E.PrintStackTrace ();} finally {r};} catch (exception e) {} try {stmt.close ();} catch (exception e) {} try {conn .close ();} catch (exception e) {}}} // Create a data source public static datasource setupdataroup (string connecturi) {BasicDataSource DS = New BasicDat Asource (); DS.SetDriverclassName ("Oracle.jdbc.driver.OracleDriver"); DS.SETUSERNAME ("Scott"); DS.SetPassword ("Tiger"); DS.SETURL (CONNECTURI); Return DS;} // presentation data source status public static void printDataSourceStats (dataSource ds) throws SQLException {BasicDataSource bds = (BasicDataSource) ds; System.out.println ( "NumActive:" bds.getNumActive ()); System.out.println ( "NumIdle: " bds.getnumidle ());} // Turn off the data source public static void shutdowndatasource (DataSource DS) throws Sqlexception {BasicDataSource BDS = (BasicDataSource) DS;
BDS.Close ();}}} The example is relatively simple, no longer said ~ 4.Tomcat DBCP configuration here is described as a database as a database.
Use DBCP to set the relevant connection and DBCP parameter values in Server.xml.