1. Build context.xml files under the Webroot/meta-inf of the project, note that it must be in this directory, Tomcat will automatically find this file, Tomcat6.0 will not be configured in Web.xml later:
<?xml version= "1.0" encoding= "UTF-8"?> <context reloadable=
"true" crosscontext= "true" >
< Resource
name= "jdbc/test"----------Note: test for the project name
auth= "Container"----------the item is immutable
type= " Javax.sql.DataSource "----------This item is invariant
driverclassname=" com.mysql.jdbc.Driver "----------database driver
url=" Jdbc:mysql://localhost:3306/haotian?autoreconnect=true "-------url
username=" root-----username
password= " Root "------Password
maxactive=" "------Maximum number of connections
maxidle=" 5 "--------Maximum number of idle connections
maxwait="-1 "/>------max Wait Number of seconds,-1 for infinite wait
</Context>
2. Connection class:
public class DbConnection {
private Connection conn=null;
Public Connection getconnection () {
//Generates a context object through which it can send aliases to the container.
Context context;
try {Context
= new InitialContext ();
Find Object
DataSource ds= (DataSource) context.lookup ("Java:comp/env/jdbc/test");//jdbc/test to name
//Get in config file Connect
try {
conn=ds.getconnection ();
catch (SQLException e) {
e.printstacktrace ();
The
catch (Namingexception e) {
//TODO auto-generated catch block
e.printstacktrace ();
return
conn;
}
3. Place the MySQL5.0 drive pack under Tomcat's Lib directory, note: Tomcat Lib, not project Lib. (Do not know why this, before the use of this connection pool in the project's Lib can be connected to success, but now can not, personally think it may be context.xml make Tomcat find the driver package in their lib, not the driver package in the project)