Home > Database > Mysql Tutorial > How to Connect Tomcat to MySQL: JNDI vs. DriverManager?

How to Connect Tomcat to MySQL: JNDI vs. DriverManager?

Patricia Arquette
Release: 2024-11-23 00:19:15
Original
978 people have browsed it

How to Connect Tomcat to MySQL: JNDI vs. DriverManager?

How to Configure Tomcat for MySQL Connectivity

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"
/>
Copy after login

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>
Copy after login

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

  • Is it safe to use a static java.sql.Connection instance in a multithreaded system?
  • How should I connect to JDBC database / datasource in a servlet based application?
  • Where do I have to place the JDBC driver for Tomcat's connection pool?
  • DAO Tutorial - basic JDBC/DAO tutorial, targeted on Tomcat/JSP/Servlet

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template