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
How to configure Tomcat to connect with MySQL
提问by Sohail
Could someone provide a few details on how to configure Tomcat to access MySQL?
有人可以提供一些有关如何配置 Tomcat 以访问 MySQL 的详细信息吗?
In which directory within Tomcat do I place
mysql-connector-java-5.1.13-bin
? Should I place it underTomcat 6.0\webapps\myapp\WEB-INF\lib
?Do I need to add configuration to
context.xml
orserver.xml
?Should I create a
web.xml
file and place it underTomcat 6.0\webapps\myapp\WEB-INF
? If so, then what should the contents of this file look like?
我应该放在 Tomcat 的哪个目录中
mysql-connector-java-5.1.13-bin
?我应该把它放在下面Tomcat 6.0\webapps\myapp\WEB-INF\lib
吗?我需要在
context.xml
或 中添加配置server.xml
吗?我应该创建一个
web.xml
文件并将其放在Tomcat 6.0\webapps\myapp\WEB-INF
. 如果是这样,那么这个文件的内容应该是什么样的?
回答by Pablo Santa Cruz
The answer to your questions:
你的问题的答案:
- One option is what you've mentioned: place the driver under
WEB-INF/lib
directory in your WAR file. The other would be in$TOMCAT_HOME/lib
directory. 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. - 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.
- Your application's (WAR) web.xmlshould be under
WEB-INF
directory 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.
- 一种选择是您提到的:将驱动程序放在
WEB-INF/lib
WAR 文件中的目录下。另一个将在$TOMCAT_HOME/lib
目录中。后者的优点是您不需要将连接器 jar 复制到您在该应用程序服务器上部署的每个项目中。缺点是您需要记住在将应用程序部署到不同的应用程序服务器之前将 jar 文件放置到位。 - 如果您需要更改默认配置中的某些内容,是的。否则,还有带有 tomcat 安装附带的默认选项的context.xml和server.xml文件。
- 您的应用程序 (WAR) web.xml应
WEB-INF
位于部署文件的目录下。您可以在 Java EE 的 servlet 容器规范中查看该文件接受的内容。通常,您将 servlet、过滤器及其对应的映射放在该文件中。
回答by BalusC
1: Where to place
mysql-connector-java-5.1.13-bin
in Tomcat directory? Should I place it underTomcat 6.0\webapps\myapp\WEB-INF\lib
?
1:放在
mysql-connector-java-5.1.13-bin
Tomcat目录的什么位置?我应该把它放在下面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/lib
or YourApp/WEB-INF/lib
. You however need to realize that the one in Tomcat/lib
will apply for alldeployed webapps and that the one in YourApp/WEB-INF/lib
will override the one in Tomcat/lib
for only the particular webapp.
但是,如果您以使用 的基本方式进行操作DriverManager#getConnection()
,那么实际上将其放入Tomcat/lib
或 中都没有关系YourApp/WEB-INF/lib
。但是,您需要意识到 inTomcat/lib
将适用于所有已部署的 webapp ,而 one inYourApp/WEB-INF/lib
将Tomcat/lib
仅覆盖特定 webapp的 one in 。
2: Do I need to confirgure
context.xml
orserver.xml
files?
2:我需要配置
context.xml
或server.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.xml
like 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.xml
as 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 DriverManager
way, 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.xml
is 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.xml
file and need to place underTomcat 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:
也可以看看:
- 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