如何配置Tomcat以连接MySQL [英] How to configure Tomcat to connect with MySQL

查看:615
本文介绍了如何配置Tomcat以连接MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


  1. 我在Tomcat中的哪个目录放置 mysql-connector-java-5.1.13-bin ?我应该把它放在 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.xml server.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:在Tomcat目录中放置 mysql-connector-java-5.1.13-bin 的位置?我应该把它放在 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 / lib YourApp / WEB-INF / lib中它实际上并不重要。但是你需要意识到 Tomcat / lib 中的那个将申请所有部署的webapps以及 YourApp中的那个/ WEB-INF / lib 将覆盖 Tomcat / lib 中仅适用于特定webapp的那个。

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.xml server.xml files?

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,过滤器,监听器以及运行webapp的强制性内容。此文件是标准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.

  • 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

这篇关于如何配置Tomcat以连接MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆