Create a connection pool tutorial (Step by Step follow the steps in ):
[-- Tutorial connection --]
You only need to put the configuration file and Java class written by kuangbaoxu into the project, and then add the commons-dbcp.jar, commons-pool.jar package for the project.
You may encounter several problems:
1. the driver. getconnectionpool statement reports an error.
The error description is"The method getconnection URL (string) is undefined for the Type poolingdriver", This is because of the DBCP jar package version problem, you can change to a higher version of the jar package. Like commons-dbcp-1.4.jar
2. the config. XML Path cannot be obtained.
This is because in the kuangbaoxu example, the config. xml file is stored in the Web-INF root directory. If there is a path structure in your project, the path of the configuration file may be incorrect.
You can use the followingCodeLocate the path under the WEB-INF:
Public StaticList <baseconnbean>Read (string path ){
String URL= Configxml.Class. Getresource (""). Getpath (); URL= (String) URL. Subsequence (0, URL. indexof ("WEB-INF") + 8);System. Out. println (URL); string rpath= URL. substring (1) + path;
// Other codes ......
3. the config. xml file cannot be read after the location is relocated.
The printed path may contain characters similar to % 20. This is because the Java path needs to be converted to fill in spaces. The conversion function is as follows:
Try{URL= Java.net. urldecoder. Decode (URL, "UTF-8");}Catch(Unsupportedencodingexception E1 ){//Todo auto-generated Catch BlockE1.printstacktrace ();}
4. reencapsulate the previous getconnection parameters.
If the common database connection method is used at the beginning of the project, you only need to block the method for obtaining the connection from the database connection pool management class to the original method. You do not need to change other code of the DaO layer.