You need to import the relevant jar package before accessing the database using a connection pool.
First, you need to import the Oracle driver package, usually in the directory where the Oracle database is installed, there is a Ojdbc6.jar driver package in the reader's directory.
You then need to import and connect pool-related jar packages, such as: Commons-dbcp-1.4.jar, and Commons-pool-1.5.6.jar packages.
Here is a toolkit that is packaged with a connection pool:
Driverclassname=oracle.jdbc.OracleDriverurl=jdbc:oracle:thin: @localhost: 1521: Xepassword= 517839username=system
db.properties
Note: This db.properties file can not be changed casually, because driverclassname,url,password,username these values can be exactly the same as the property name inside the wrapper class, if the inconsistency can be set using the following method:
Setdriverclassname () SetUrl () Setusername () SetPassword ()
PackageCom.xdl.util;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.sql.Connection;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement;Importjava.util.Properties;ImportJavax.sql.DataSource;Importorg.apache.commons.dbcp.BasicDataSourceFactory; Public classdbcputilproperties {Private StaticDataSource DataSource; Static{InputStream InputStream= Dbcputilproperties.class. getClassLoader (). getResourceAsStream ("Db.properties"); Properties Pro=NewProperties (); Try{pro.load (InputStream); DataSource=basicdatasourcefactory. CreateDataSource (PRO); /*** can also set the driver, user name, account number, password, if the property name in the db.properties changed, can be manually set by the following way * DATASOURCE.SETDRIVERCLASSNAME (pr O.getproperty ("Driverclassname")); * Datasource.seturl (pro.getproperty ("url")); * Datasource.setusername (Pro.getproperty ("username")); * Datasource.setpassword (Pro.getproperty ("password")); */ } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } Public StaticConnection getconnection () {Try { returndatasource.getconnection (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } return NULL; } Public Static voidReleaseresource (Connection conn,statement st,resultset rs) {if(rs! =NULL) { Try{rs.close (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{RS=NULL; } } if(St! =NULL) { Try{st.close (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{St=NULL; } } if(Conn! =NULL) { Try{conn.close (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{conn=NULL; } } } }
Dbcputilproperties.java
The Dbcputilproperties.java is packaged to load the driver, get the connection, release the resources.
Here is a test class:
Package com.xdl.test; Import java.sql.Connection; Import com.xdl.util.DBCPUtilProperties; Public class dbcputilpropertiestest { publicstaticvoid main (string[] args) { // TODO auto-generated Method stub Connection conn = dbcputilproperties.getconnection (); SYSTEM.OUT.PRINTLN (conn); }}
Dbcputilpropertiestest.java
"JDBC" uses connection pooling technology to access Oracle