【问题标题】:Problems with JDBC URL in tomcat 9 context.xml for treeAnno用于treeAnno的tomcat 9 context.xml中的JDBC URL问题
【发布时间】:2020-05-30 11:28:37
【问题描述】:

我在服务器 (Ubuntu 18.04) 上安装了全新的 tomcat 9。我正在尝试设置 treeanno (https://github.com/nilsreiter/treeanno/releases) 并部署 github 存储库中提供的 Web 应用程序存档 (war)。 我使用 tomcat manager 应用程序来部署 war 文件,并按照此描述配置 tomcat 以使用相应的数据库资源:https://github.com/nilsreiter/treeanno/blob/master/INSTALL.md。 根据粗略的描述,这个要在tomcat安装的context.xml中添加:

<Resource name="treeanno/jdbc" auth="Container" type="javax.sql.DataSource"
           maxActive="100" maxIdle="30" maxWait="10000"
           username="USERNAME" password="PASSWORD" driverClassName="com.mysql.jdbc.Driver"
           url="DATABASE URL"/>

由于我是 tomcat 的新手,我不清楚我需要设置哪些属性,尤其是“url”。 如果我尝试访问该应用程序,我会收到以下错误,指出由于 url 不足,无法建立数据库连接:

    Type Exception Report

    Message An exception occurred processing [/index.jsp] at line [12]

    Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.apache.jasper.JasperException: An exception occurred processing [/index.jsp] at line [12]

9:      doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
10:         doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
11:         omit-xml-declaration="false" />
12: <sql:query var="rs" dataSource="treeanno/jdbc" sql="select id, username from treeanno_users">
13: </sql:query>
14: <html xmlns="http://www.w3.org/1999/xhtml">
15: <head>


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:626)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:500)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Root Cause

javax.servlet.ServletException: javax.servlet.jsp.JspException: Unable to get connection, DataSource invalid: "java.sql.SQLException: Cannot create JDBC driver of class 'com.mysql.jdbc.Driver' for connect URL 'jdbc:mysql:3306'"
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:666)
    org.apache.jsp.index_jsp._jspService(index_jsp.java:270)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Root Cause

javax.servlet.jsp.JspException: Unable to get connection, DataSource invalid: "java.sql.SQLException: Cannot create JDBC driver of class 'com.mysql.jdbc.Driver' for connect URL 'jdbc:mysql:3306'"
    org.apache.taglibs.standard.tag.common.sql.QueryTagSupport.getConnection(QueryTagSupport.java:285)
    org.apache.taglibs.standard.tag.common.sql.QueryTagSupport.doStartTag(QueryTagSupport.java:168)
    org.apache.jsp.index_jsp._jspx_meth_sql_005fquery_005f0(index_jsp.java:296)
    org.apache.jsp.index_jsp._jspService(index_jsp.java:134)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

我已经发现该 url 应该链接到包含密码和用户的数据库服务器,但这似乎不适合此配置文件。

我在这里找到了以下内容 (https://ci.apache.org/projects/tomcat/tomcat9/docs/jndi-datasource-examples-howto.html):

url=jdbc:mysql://localhost:3306/javatest"/

但调整端口等不起作用: url=jdbc:mysql://localhost:8080/TreeAnno-1.0.2"/

现在,我需要如何制定 url 以允许应用连接/启动 MySQL 数据库?

【问题讨论】:

  • &lt;tomcat-root&gt;/lib中有mysql驱动jar吗?
  • jdbc:mysql:3306 不是有效的 JDBC 连接 url...无论如何,听起来您在 tomcat 本身的 claspath 上没有 MySQL Connector/J 驱动程序。

标签: java url jdbc war tomcat9


【解决方案1】:

有多种方法可以为 Tomcat 托管的 Web 应用程序配置 JDBC 数据连接。

Tomcat 中的 JNDI 连接

在您的注释中,您描述了如何使用添加到 Tomcat context.xml 文件中的 Tomcat Resource 条目。当我使用这种方法时,我还在我的 Web 应用程序的 web.xml 文件中添加了一个条目,它引用了 Resource 条目。例如:

<resource-ref>
    <res-ref-name>treeanno/jdbc</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

注意我的web.xml 文件中的资源引用名称与Resource 名称的匹配方式。

完成后,我现在可以在我的应用程序的 Java 代码中引用该数据源,如下所示:

// ABSOLUTELY NOT PRODUCTION-READY CODE! JUST FOR TESTING!

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

...

Context initialContext = new InitialContext();
Context context = (Context) initialContext.lookup("java:comp/env");
DataSource ds = (DataSource) context.lookup("treeanno/jdbc");

java:comp/env 是做什么的? See here.

但首先测试您的网址

在进入上述配置之前,您可能需要单独确保您拥有正确的 JDBC 连接 URL(以及用户和密码)。

如果您的 URL 错误或数据源不可用等,再多的修改也无济于事。

如何测试?各种方式——例如:

使用诸如DBeaver 之类的工具。这将要求提供 URL、用户和密码以连接到 MySQL。

或者编写一个单独的独立 Java 应用程序(带有一个 main 方法)和一个基本的JDBC connection

或任何其他只关注 MySQL 连接细节的方式,而不是 Tomcat 配置复杂性。

正如@CHN 指出的那样:如果您没有 mysql JDBC jar 文件,这一切都没有实际意义。

就像我说的,这只是完成工作的一种方法 - 但它对我有用。

【讨论】:

    【解决方案2】:

    首先,你需要安装MySQL(如果你熟悉的话,用docker安装)。

    在安装过程中,我记得它会要求你设置 root 的密码(这是默认用户名)。安装后,create a new database

    假设您将数据库命名为 foocontext.xml 中的 &lt;Resource/&gt; 应更新为:

    <Resource name="treeanno/jdbc" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="root" password="passwordOfTheRoot" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/foo"/>
    

    然后下载MySQL JDBC driver,放到$TOMCAT_ROOT_FOLDER/lib里面。然后重启Tomcat,相信应用程序会连接到foo数据库和create the tables for you

    【讨论】:

    • 非常感谢您的帮助,现在可以使用了!基本上,主要问题是tomcat根文件夹中缺少mysql.jar。之后,修复 url 解决了问题:)
    猜你喜欢
    • 1970-01-01
    • 2013-05-07
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    • 2010-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多