我在一台服务器上安装了一个新的tomcat9(Ubuntu18.04)。我正在尝试设置treeanno (https://github.com/nilsreiter/treeanno/releases)并部署github repos中给出的web应用程序存档(war)。我使用tomcat管理器应用程序来部署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数据库?
发布于 2020-02-15 02:08:45
首先,您需要安装MySQL (如果您熟悉docker,也可以使用它进行安装)。
在安装过程中,我记得它会要求您设置root的密码(这是默认用户名)。安装完成后,执行create a new database。
假设您将数据库命名为foo,则context.xml中的<Resource/>应更新为:
<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 .Then restart Tomcat中,然后我相信应用程序将连接到foo数据库和create the tables for you。
发布于 2020-02-15 01:30:36
有多种方法可以为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。
但是首先测试你的URL
在进入上述配置之前,您可能希望独立地确保拥有正确的JDBC连接URL (以及user和pass)。
如果您有错误的URL,或者数据源不可用,那么再多的修修补补也不会有帮助。
如何测试?多种方式-例如:
使用诸如DBeaver之类的工具。这将要求提供连接到MySQL的网址、用户和密码。
或者编写一个独立的Java app (使用一个main方法)和一个基本的JDBC connection。
或者只关注MySQL连接细节的任何其他方式,而不是Tomcat配置复杂性。
正如@CHN指出的:如果没有mysql JDBC jar文件,这一切都是没有意义的。
就像我说的,这只是完成工作的一种方法--但它对我很有效。
https://stackoverflow.com/questions/60230139
复制相似问题