Java 如何配置Tomcat连接MySQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3485177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:12:26  来源:igfitidea点击:

How to configure Tomcat to connect with MySQL

javamysqltomcatservletsjdbc

提问by Sohail

Could someone provide a few details on how to configure Tomcat to access MySQL?

有人可以提供一些有关如何配置 Tomcat 以访问 MySQL 的详细信息吗?

  1. In which directory within Tomcat do I place mysql-connector-java-5.1.13-bin? Should I place it under Tomcat 6.0\webapps\myapp\WEB-INF\lib?

  2. Do I need to add configuration to context.xmlor server.xml?

  3. Should I create a web.xmlfile and place it under Tomcat 6.0\webapps\myapp\WEB-INF? If so, then what should the contents of this file look like?

  1. 我应该放在 Tomcat 的哪个目录中mysql-connector-java-5.1.13-bin?我应该把它放在下面Tomcat 6.0\webapps\myapp\WEB-INF\lib吗?

  2. 我需要在context.xml或 中添加配置server.xml吗?

  3. 我应该创建一个web.xml文件并将其放在Tomcat 6.0\webapps\myapp\WEB-INF. 如果是这样,那么这个文件的内容应该是什么样的?

回答by Pablo Santa Cruz

The answer to your questions:

你的问题的答案:

  1. One option is what you've mentioned: place the driver under WEB-INF/libdirectory in your WAR file. The other would be in $TOMCAT_HOME/libdirectory. The advantage of the latter would be that you don't need to copy the connector jar into every single project you deploy on that application server. Disadvantage is you will need to rememberto put the jar file in place before deploying your application in a different application server.
  2. If you need to change something in the default configuration, yes. Otherwise, there are context.xmland server.xmlfiles with default options shipped with tomcat installations.
  3. Your application's (WAR) web.xmlshould be under WEB-INFdirectory in your deploy file. You can look at the accepted content to that file in Java EE's servlet container specification. Usually, you place your servlet, filter and their corresponding mappings in that file.
  1. 一种选择是您提到的:将驱动程序放在WEB-INF/libWAR 文件中的目录下。另一个将在$TOMCAT_HOME/lib目录中。后者的优点是您不需要将连接器 jar 复制到您在该应用程序服务器上部署的每个项目中。缺点是您需要记住在将应用程序部署到不同的应用程序服务器之前将 jar 文件放置到位。
  2. 如果您需要更改默认配置中的某些内容,是的。否则,还有带有 tomcat 安装附带的默认选项的context.xmlserver.xml文件。
  3. 您的应用程序 (WAR) web.xmlWEB-INF位于部署文件的目录下。您可以在 Java EE 的 servlet 容器规范中查看该文件接受的内容。通常,您将 servlet、过滤器及其对应的映射放在该文件中。

回答by BalusC

1: Where to place mysql-connector-java-5.1.13-binin Tomcat directory? Should I place it under Tomcat 6.0\webapps\myapp\WEB-INF\lib?

1:放在mysql-connector-java-5.1.13-binTomcat目录的什么位置?我应该把它放在下面Tomcat 6.0\webapps\myapp\WEB-INF\lib吗?

That depends on where the connections are to be managed. Normally you would like to create a connection pooled JNDI datasource to improve connecting performance. In that case, Tomcat is managing the connections and need to have access to the JDBC driver. You should then drop the JAR file in Tomcat/lib.

这取决于要管理连接的位置。通常,您希望创建一个连接池 JNDI 数据源以提高连接性能。在这种情况下,Tomcat 正在管理连接并且需要访问 JDBC 驱动程序。然后,您应该将 JAR 文件放入Tomcat/lib.

But if you're doing it the basic way using DriverManager#getConnection(), then it in fact don't matter if you drop it in Tomcat/libor YourApp/WEB-INF/lib. You however need to realize that the one in Tomcat/libwill apply for alldeployed webapps and that the one in YourApp/WEB-INF/libwill override the one in Tomcat/libfor only the particular webapp.

但是,如果您以使用 的基本方式进行操作DriverManager#getConnection(),那么实际上将其放入Tomcat/lib或 中都没有关系YourApp/WEB-INF/lib。但是,您需要意识到 inTomcat/lib将适用于所有已部署的 webapp ,而 one inYourApp/WEB-INF/libTomcat/lib仅覆盖特定 webapp的 one in 。



2: Do I need to confirgure context.xmlor server.xmlfiles?

2:我需要配置context.xmlserver.xml文件吗?

That depends on where the connections are to be managed. When using a JNDI datasource, it suffices to configure it using YourApp/META-INF/context.xmllike follows (just create file if not exist):

这取决于要管理连接的位置。使用 JNDI 数据源时,YourApp/META-INF/context.xml只需使用如下配置即可(如果文件不存在,则创建文件):

<?xml version="1.0" encoding="UTF-8"?>
<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>

and the YourApp/WEB-INF/web.xmlas follows:

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>

If you're doing it the basic DriverManagerway, then it's all up to you. Hardcoded, properties file, XML file, etcetera. You should manage it youself. Tomcat won't (and can't) do anything useful for you.

如果您按照基本DriverManager方式进行操作,那么一切都取决于您。硬编码、属性文件、XML 文件等。你应该自己管理。Tomcat 不会(也不能)为您做任何有用的事情。

Noted should be that the YourApp/META-INF/context.xmlis specific to Tomcat and clones. Each servletcontainer/appserver has its own way of defining JNDI resources. In Glassfish for example, you'd like to do that through the webbased admin interface.

应该注意的是,这YourApp/META-INF/context.xml是特定于 Tomcat 和克隆的。每个 servletcontainer/appserver 都有自己定义 JNDI 资源的方式。例如,在 Glassfish 中,您希望通过基于 Web 的管理界面执行此操作。



3: Should I write web.xmlfile and need to place under Tomcat 6.0\webapps\myapp\WEB-INF? If Yes, then what should be the contents of file?

3:我应该写web.xml文件并需要放在下面Tomcat 6.0\webapps\myapp\WEB-INF吗?如果是,那么文件的内容应该是什么?

You should always supply one. It's not only to configure resources, but also to define servlets, filters, listeners and that kind of mandatory stuff to run your webapp. This file is part of the standard Servlet API.

你应该总是提供一个。不仅要配置资源,还要定义 servlet、过滤器、侦听器和那些运行 web 应用程序的必需品。该文件是标准 Servlet API 的一部分。

See also:

也可以看看: