java context.xml 中 SQLite DB 的相对路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/770790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Relative path to SQLite DB in context.xml
提问by titaniumdecoy
Is it possible to use a relative path to an SQLite database file in the context.xml file of a Java web application?
是否可以在 Java Web 应用程序的 context.xml 文件中使用 SQLite 数据库文件的相对路径?
At present, I have:
目前,我有:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/mywebapp">
<Resource name="jdbc/mydb" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="myusername" password="mypassword" driverClassName="org.sqlite.JDBC"
url="jdbc:sqlite://Users/me/NetBeansProjects/MyApp/web/my.db"/>
</Context>
As you can see, the url attribute of the Resource tag currently specifies an absolute path to my.db. However, I am working on this project with a small team of developers as a class project. We are sharing the code through Subversion and currently, every time one of us updates our project, we have to manually replace the above path with the correct path for our computer.
如您所见,Resource 标签的 url 属性当前指定了 my.db 的绝对路径。但是,我正在与一个小型开发人员团队一起从事这个项目作为班级项目。我们正在通过 Subversion 共享代码,目前,每次我们更新我们的项目时,我们都必须手动将上述路径替换为我们计算机的正确路径。
Thanks in advance.
提前致谢。
UPDATE:It appears that relative paths are not relative to the context.xml file, but to CATALINA_HOME. Unfortunately that directory (where Tomcat is located) is in another user's home folder (1) to which I have ony read access:
更新:相对路径似乎不是相对于 context.xml 文件,而是相对于 CATALINA_HOME。不幸的是,该目录(Tomcat 所在的目录)位于另一个用户的主文件夹 (1) 中,我对此只有读权限:
(1) /home/myprof/Tomcat/bin/
(2) /home/me/NetBeansProjects/MyApp/web/my.db
(1) /home/myprof/Tomcat/bin/
(2) /home/me/NetBeansProjects/MyApp/web/my.db
I cannot include /me in the relative path because, as stated above, this project will run on multiple computers via a svn repository and the directory in which the project files are contained (2) will change. However, since all of the accounts are on a shared virtual network, CATALINA_HOME (1) will not change.
我不能在相对路径中包含 /me,因为如上所述,该项目将通过 svn 存储库在多台计算机上运行,并且包含项目文件的目录 (2) 将更改。但是,由于所有帐户都在共享虚拟网络上,因此 CATALINA_HOME (1) 不会更改。
Also, how can I get the path to the database file my.db from the context.xml file above inside a servlet?
另外,如何从上面的 servlet 内的 context.xml 文件中获取数据库文件 my.db 的路径?
采纳答案by titaniumdecoy
回答by honzas
I have found following examples of using JDBC with SQLite:
我发现了以下将 JDBC 与 SQLite 结合使用的示例:
For memory access:
jdbc:sqlite::memory:For relative paths:
jdbc:sqlite:relative/path.dbFor absolute paths:
jdbc:sqlite:/absolute/path.db
对于内存访问:
jdbc:sqlite::memory:对于相对路径:
jdbc:sqlite:relative/path.db对于绝对路径:
jdbc:sqlite:/absolute/path.db
回答by titaniumdecoy
I am still looking for a way to use a relative path to the database in the context.xml file.
我仍在寻找一种在 context.xml 文件中使用数据库相对路径的方法。
However, I figured out how to get extract the path to the database from the context.xml file. It's not pretty, but it works. I couldn't figure out how to parse the context.xml file as XML so this ugly hack looks for a line beginning with "url=".
但是,我想出了如何从 context.xml 文件中提取数据库的路径。它不漂亮,但它有效。我不知道如何将 context.xml 文件解析为 XML,因此这个丑陋的 hack 查找以“ url=”开头的行。
URL url = getServletContext().getResource("/META-INF/context.xml");
Properties props = new Properties();
props.load(url.openStream());
String strval = props.getProperty("url");
Pattern pattern = Pattern.compile("\\W*\"(.\*)\"\\W*");
Matcher matcher = pattern.matcher(strval);
matcher.find();
String constr = matcher.group(1);
if (constr != null)
this.jdbcConnectionString = constr;
回答by millimoose
As a different approach: did you try to use jdbc:sqlite:~/myapp.db? This shouldbe a writable location on the computers of everyone running that application. (Although I've seen the evil that are read-only home directories.)
作为一种不同的方法:您是否尝试使用jdbc:sqlite:~/myapp.db? 这应该是运行该应用程序的每个人的计算机上的可写位置。(虽然我已经看到了只读主目录的邪恶。)
I'm not exactly sure if SQLite supports ~in the database URL. I know H2does, and I can nothing but recommend it as an embedded Java DB. If you don't want to change databases, you might want to see if Tomcat allows some parameter substitution in its configuration files, like ${user.home}. (Some preliminary Googling gives me mailing list results that seem to imply that's be case in at least server.xml, so it could be worth just trying it.)
我不确定 SQLite 是否支持~数据库 URL。我知道H2可以,我只能推荐它作为嵌入式 Java DB。如果您不想更改数据库,您可能想查看 Tomcat 是否允许在其配置文件中进行某些参数替换,例如${user.home}. (一些初步的谷歌搜索给了我邮件列表结果,这似乎暗示至少在server.xml,所以它可能值得尝试。)
I'm still not exactly sure what you're trying to achieve though: are you the one running the Tomcat instance, or is it a shared tomcat instance running under some system account?
不过,我仍然不确定您要实现的目标:您是运行 Tomcat 实例的人,还是在某个系统帐户下运行的共享 tomcat 实例?
You could also try to set up another world-writable location on the network to put the database in. Somewhere under /var/maybe, or just a folder that's set to ug=rwxin one person's home directory.
您还可以尝试在网络上设置另一个世界可写的位置来放置数据库。/var/也许在某个地方,或者只是一个设置ug=rwx在一个人的主目录中的文件夹。
回答by fero46
I had the same issue with Spring and jdbc.properties I fix that by removing // and write the relative path
我在 Spring 和 jdbc.properties 上遇到了同样的问题,我通过删除 // 并写入相对路径来解决这个问题
Try this
试试这个
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/mywebapp">
<Resource name="jdbc/mydb" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="myusername" password="mypassword" driverClassName="org.sqlite.JDBC"
url="jdbc:sqlite:web/my.db"/>
</Context>
回答by gagarwa
This answer is already given by honzas. I am reiterating it to make it more clear (b/c I also didn't understand it at first).
honzas 已经给出了这个答案。我重申它以使其更清楚(b / c我一开始也不明白)。
jdbc:sqlite:relative/path.db
That is correct for a relative path. Notice the lack of a / before relative. That makes the path absolute. You can also use this:
这对于相对路径是正确的。注意在相对之前缺少 / 。这使路径成为绝对路径。你也可以使用这个:
jdbc:sqlite:./relative/path.db
The . refers to the current folder.
这 。指的是当前文件夹。

