Question:
How can I set up Tomcat to communicate with a MySQL database?
Answer:
1. mysql-connector-java-5.1.13-bin Placement
The placement of the JDBC driver depends on where connections are managed. For a connection pool managed by Tomcat, place the JAR file in Tomcat/lib. For basic connections using DriverManager#getConnection(), the driver can be placed either in Tomcat/lib (applies to all deployed webapps) or YourApp/WEB-INF/lib (overrides the Tomcat/lib driver for the specific webapp).
2. Configuration Files (context.xml or server.xml)
JNDI Datasource:
Basic DriverManager:
3. web.xml
Example (JNDI Datasource):
context.xml
<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>
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>
Additional Resources:
The above is the detailed content of How Can I Configure Tomcat to Connect to a MySQL Database?. For more information, please follow other related articles on the PHP Chinese website!