Server. xml modification I recommend that you use the admin component of Tomcat to complete the configuration in full graphical mode, which is quite easy. Alternatively, write the following configuration between <globalnamingresources> </globalnamingresources> (for your reference only, change it to your own database configuration ):
CopyCode The Code is as follows: <Resource
Name = "JDBC/MySQL"
Type = "javax. SQL. datasource"
Password = "******"
Driverclassname = "org. gjt. Mm. MySQL. Driver"
Maxidle = "2"
Maxwait = "5000"
Username = "root"
Url = "JDBC: mysql: // 127.0.0.1/test"
Maxactive = "4"/>
Create an XML file with the same name as your website folder in the/CONF/Catalina/localhost folder under the tomcat installation directory. For example, testsourse. xml. The content is as follows: copy the Code the code is as follows:
name = "JDBC/MySQL"
type = "javax. SQL. datasource "
Password =" ******* "
driverclassname =" org. gjt. mm. mySQL. driver "
maxidle =" 2 "
maxwait =" 5000 "
username =" root "
url =" JDBC: mysql: // 127.0.0.1/test "
maxactive =" 4 "/>
In fact, it is to repeat the content of server. xml. This step is very important. If this step does not exist, an error will occur, and org. apache. tomcat. DBCP. DBCP. sqlnestedexception: cannot create JDBC driver of class ''for connect URL 'null' error.
Finally, in your own website's/WEB-INF/Web. xml file, add the code (refer ):Copy codeThe Code is as follows: <resource-ref>
<Description> dB connection </description>
<Res-ref-Name> JDBC/MySQL </RES-ref-Name>
<Res-type> javax. SQL. datasource </RES-type>
<Res-auth> container </RES-auth>
</Resource-ref>
OK. All configurations are complete. Now you can write code to test the database connection pool. For example:Copy codeThe Code is as follows: datasource DS = NULL;
Initialcontext CTX = new initialcontext ();
DS = (datasource) CTX. Lookup ("Java: COMP/ENV/jdbc/MySQL ");
Connection conn = Ds. getconnection ();
The database connection object is obtained.