Home  >  Article  >  Database  >  How to Configure Tomcat to Connect to MySQL?

How to Configure Tomcat to Connect to MySQL?

Barbara Streisand
Barbara StreisandOriginal
2024-11-24 13:26:11193browse

How to Configure Tomcat to Connect to MySQL?

How to Configure Tomcat for MySQL Connectivity

Tomcat requires specific configurations to facilitate communication with MySQL. This guide will provide step-by-step instructions for deploying the necessary components and configurations:

Managing Connections

Connections to MySQL can be managed either through a connection pooled JNDI datasource or the basic DriverManager#getConnection(). For optimal performance, it is recommended to use JNDI datasource.

JDBC Driver Placement

  • Tomcat/lib: Place the JDBC driver (mysql-connector-java-5.1.13-bin) here if you intend to use a JNDI datasource.
  • YourApp/WEB-INF/lib: Place the JDBC driver here if you are using the basic DriverManager method.

Configuration Options

1. Context.xml (JNDI Datasource)

<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"/>

2. web.xml (JNDI Datasource)

<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>

3. web.xml (DriverManager Method)

Configuration is not required by Tomcat for this method, as the connections will be managed manually.

Web.xml

Creating a web.xml file is crucial for defining servlets, filters, listeners, and other essential elements for your web application.

Additional Resources

  • [MySQL Connector/J Documentation](https://dev.mysql.com/doc/connector-j/8.0/en/)
  • [Tomcat JDBC Example](https://tomcat.apache.org/tomcat-7.0-doc/jdbc-examples-howto.html)
  • [Glassfish JNDI configuration](https://docs.oracle.com/cd/E18930_01/wls/docs103/jndi/ojndiserv.html)

The above is the detailed content of How to Configure Tomcat to Connect to MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn