简体   繁体   English

Web 应用程序无法建立与数据库的连接

[英]Web application cannot establish connection to database

My application, when running on Tomcat, is not able to connect to my database when deployed as a.war in the webapps folder, but it can connect when I am running it through Eclipse directly.我的应用程序在 Tomcat 上运行时,在 webapps 文件夹中部署为 a.war 时无法连接到我的数据库,但是当我直接通过 Eclipse 运行它时,它可以连接。 The code is exactly the same in both cases.两种情况下的代码完全相同。 In addition, other apps in the webapps folder, which originally could connect to the database, can no longer do so.此外,webapps 文件夹中的其他应用程序原本可以连接到数据库,现在也无法连接。 The jdbc code is correct as I have tested it with offline applications or when running it through eclipse, but not when I access it on, say, Chrome, using localhost. jdbc 代码是正确的,因为我已经使用离线应用程序或通过 eclipse 运行它时对其进行了测试,但当我使用 localhost 在 Chrome 上访问它时则不然。 What has happened to my tomcat server?我的 tomcat 服务器发生了什么事?

Note: the JDBC driver is the MS Access one.注意:JDBC 驱动程序是 MS Access 驱动程序之一。

The code is exactly the same in both cases.两种情况下的代码完全相同。

No it's not;不,这不对; if it were exactly the same you'd be connecting properly.如果它完全相同,您将正确连接。

but it can connect when I am running it through Eclipse directly但是当我直接通过 Eclipse 运行它时它可以连接

Does this mean that you have a main method that drives the code that connects properly?这是否意味着您有一个主要方法来驱动正确连接的代码? The way you do it is usually different from a web app - you know that, right?您执行此操作的方式通常不同于 web 应用程序 - 您知道,对吧?

If you could post an exception or log message it would help a great deal.如果您可以发布异常或日志消息,那将有很大帮助。

I'm guessing it could be any one of the following;我猜它可能是以下任何一种; guessing is necessary because you haven't provided enough information for a solid answer:猜测是必要的,因为您没有提供足够的信息来获得可靠的答案:

  1. The JDBC driver JAR is not available when you run under one configuration;在一种配置下运行时,JDBC 驱动程序 JAR 不可用; you'll see a ClassNotFoundException in that case.在这种情况下,您会看到ClassNotFoundException
  2. You haven't configured a JNDI data source properly in Tomcat.您尚未在 Tomcat 中正确配置JNDI 数据源
  3. You didn't put the JDBC driver JAR in the Tomcat /lib directory.您没有将 JDBC 驱动程序 JAR 放在 Tomcat /lib 目录中。

Could be other possibilities.可能是其他可能性。

UPDATE:更新:

Since you're using Access, and providing no other information, I'm guessing that you're using a relative file path to get to the Access .mdb file.由于您使用的是 Access,并且没有提供其他信息,我猜您正在使用相对文件路径来访问 Access .mdb文件。 Your command line app that runs successfully in Eclipse works because you give a relative file path that is correct relative to the project root.您在 Eclipse 中成功运行的命令行应用程序可以正常工作,因为您提供了相对于项目根目录正确的相对文件路径。 When you deploy to Tomcat, that relative path is no longer correct, so your application can't find the database anymore.当您部署到 Tomcat 时,该相对路径不再正确,因此您的应用程序无法再找到数据库。

Sounds like you aren't printing the stack trace for errors, so you're losing out on information that might help you figure this out.听起来您没有打印堆栈跟踪以查找错误,因此您丢失了可能帮助您解决这个问题的信息。

Microsoft Access is not a multi-user database. Microsoft Access 不是多用户数据库。 A web based application running on Tomcat is certainly multi-threaded.在 Tomcat 上运行的基于 web 的应用程序肯定是多线程的。 You should not be using Access for a web application.您不应将 Access 用于 web 应用程序。 I'd consider switching to another database.我会考虑切换到另一个数据库。

First, you need to verify the path to your database file.首先,您需要验证数据库文件的路径。 In fact, I believe it has to be an absolute path for tomcat to work correctly.事实上,我相信它必须是 tomcat 正常工作的绝对路径。 The simplest thing to do is to put your database file in C:\data or similar and then hard code the path in your code.最简单的做法是将数据库文件放在 C:\data 或类似文件中,然后在代码中硬编码路径。 Of course, the file will be outside of your war and thus not portable (ie, deployable to a remote server).当然,该文件将在您的战争之外,因此不可移植(即,可部署到远程服务器)。

Second, you do need to make sure that the JDBC driver is available.其次,您确实需要确保 JDBC 驱动程序可用。 If you use Class.forName in your code to load the driver, you will only need to make sure that the jar containing the driver is in the tomcat classpath (which would include the lib directory of your webapp).如果您在代码中使用 Class.forName 来加载驱动程序,您只需确保包含驱动程序的 jar 位于 tomcat 类路径(其中将包括您的 webapp)目录中。 If you use the -Djdbc.drivers JVM flag approach, you will need to add it to the tomcat startup scripts.如果您使用 -Djdbc.drivers JVM 标志方法,则需要将其添加到 tomcat 启动脚本中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM