Server.xml's modifications I recommend using Tomcat's admin component to complete, fully graphical configuration, fairly easy. or according to the following writing in < globalnamingresources></globalnamingresources> between the following configuration (for reference only, when you use, please change to their own database configuration):
Copy Code code as follows:
<resource
Name= "Jdbc/mysql"
Type= "Javax.sql.DataSource"
password= "Hu Jintao"
Driverclassname= "Org.gjt.mm.mysql.Driver"
Maxidle= "2"
maxwait= "5000"
Username= "Root"
Url= "Jdbc:mysql://127.0.0.1/test"
Maxactive= "4"/>
Then, create an XML file with the same name as your Web site folder in the/conf/catalina/localhost folder under the Tomcat installation directory. For example: Testsourse.xml. The contents are as follows:
Copy Code code as follows:
<?xml version= "1.0" encoding= "UTF-8"?>
<Context>
<resource
Name= "Jdbc/mysql"
Type= "Javax.sql.DataSource"
password= "Hu Jintao"
Driverclassname= "Org.gjt.mm.mysql.Driver"
Maxidle= "2"
maxwait= "5000"
Username= "Root"
Url= "Jdbc:mysql://127.0.0.1/test"
Maxactive= "4"/>
</Context>
The fact is to repeat the contents of the Server.xml. This step is very important, if you do not have this step will be wrong, will appear org.apache.tomcat.dbcp.dbcp.SQLNestedException:Cannot create JDBC driver of class ' for connect URL ' null ' ERROR.
Finally, in the/web-inf/web.xml file of your own website, add the Code (Reference):
Copy Code code 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. The configuration is all complete. Now you can write code to test this database connection pool. Such as:
Copy Code code 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 was obtained.