1. Copy the database driver jar package to the tomcat\common\lib directory.
2. Modify the server.xml file and add a resource node under the Context configuration node, as follows:
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="root" password="" driverClassName="org.gjt.mm.mysql.Driver" url="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=GBK&useUnicode=TRUE"/>
Note: If there is an & character, it needs to be converted to & (XML file specification)
3. Modify the web application WEB-INF\web.xml file and add Resource-Def as follows:
<resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/TestDB</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
4. Restart the web application. In the web application, the data source and database connection can be obtained by the following code:
javax.naming.InitialContext context = new javax.naming.InitialContext(); DataSource ds = (DataSource) cxt.lookup( "java:/comp/env/jdbc/TestDB" ); connection = (Connection)ds.getConnection();