如何配置Tomcat连接MySQL

问题描述:

谁能提供一些关于如何配置Tomcat访问MySQL的细节?

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

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

  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?

我需要在 context.xmlserver.xml 中添加配置吗?

Do I need to add configuration to context.xml or server.xml?

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

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

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

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

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

但如果你使用 DriverManager#getConnection(),那其实掉线也无所谓它在 Tomcat/libYourApp/WEB-INF/lib 中.但是,您需要意识到 Tomcat/lib 中的一个将适用于所有部署的 webapps 而 YourApp/WEB-INF/lib 中的一个> 将仅针对特定的 web 应用覆盖 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 all deployed webapps and that the one in YourApp/WEB-INF/lib will override the one in Tomcat/lib for only the particular webapp.

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

2: Do I need to confirgure context.xml or server.xml files?

这取决于要管理连接的位置.使用 JNDI 数据源时,只需使用 YourApp/META-INF/context.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):

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

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>

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

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.

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

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.

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

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

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

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.