Home >Database >Mysql Tutorial >How to Configure Tomcat for MySQL Database Access?
Configuring Tomcat for MySQL Access
Introduction
Connecting Tomcat to MySQL is essential for web applications that require database access. This article provides a step-by-step guide on configuring Tomcat to establish successful connections to MySQL.
1. Placement of MySQL Connector
The mysql-connector-java JAR file should be placed in Tomcat's shared library directory (Tomcat/lib) if you want it to be available to all web applications. Alternatively, you can place the JAR file in your specific web application's directory (YourApp/WEB-INF/lib) to override the shared library and apply it only to that particular application.
2. Configuration in Context.xml or Server.xml
If you plan to use a JNDI datasource to manage connections, you need to configure it in context.xml (YourApp/META-INF/context.xml) as follows:
<Context> <Resource name="jdbc/yourdb" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" url="jdbc:mysql://localhost:3306/yourdb" driverClassName="com.mysql.jdbc.Driver" username="yourname" password="yourpass" /> </Context>
3. Web.xml Configuration
For resource configuration in web.xml (YourApp/WEB-INF/web.xml):
<resource-env-ref> <resource-env-ref-name>jdbc/yourdb</resource-env-ref-name> <resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type> </resource-env-ref>
4. Web.xml Deployment Descriptor
A web.xml file is essential for defining servlets, filters, listeners, and other essential components of your web application. It should be placed in YourApp/WEB-INF.
Additional Resources:
The above is the detailed content of How to Configure Tomcat for MySQL Database Access?. For more information, please follow other related articles on the PHP Chinese website!