Placing the MySQL Connector Jar
Depending on the connection management approach, you can place the mysql-connector-java-5.1.13-bin JAR file either in Tomcat/lib for Tomcat-managed connections or in Tomcat 6.0webappsmyappWEB-INFlib for basic DriverManager-based connections. The后者 overrides the former for the specific webapp.
Resource Configuration
JNDI Datasource with context.xml (Tomcat-managed):
Configure the datasource in YourApp/META-INF/context.xml:
<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" />
Basic Driver Loading with web.xml (DriverManager):
Add a resource environment reference to 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>
web.xml and Servlet API
Always provide a web.xml file for defining servlets, filters, listeners, and resource configurations. This file is required for running the webapp according to the Servlet API.
Additional Resources
The above is the detailed content of How to Connect Tomcat to MySQL: JNDI vs. DriverManager?. For more information, please follow other related articles on the PHP Chinese website!