如何指定相对的JDBC SQLite网址(路径?) [英] How can I specify a RELATIVE JDBC SQLite url (path?)

查看:140
本文介绍了如何指定相对的JDBC SQLite网址(路径?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力进行这项工作两天,并尝试了所有我能找到的建议. las,到目前为止,还没有成功.

I've been trying to get this work for two days and tried all suggestions that I could find. Alas, without success, so far.

这就是我要做的事情:我正在构建一个小型(java swing)桌面数据库应用程序.我正在使用Netbeans 7.0和SQLite,并从 http://www找到了JDBC驱动程序.xerial.org/trac/Xerial/wiki/SQLiteJDBC 可以正常工作,只要我在JDBC URL中指定了绝对路径即可.

This is what I'm trying to do: I am building a small (java swing) desktop database application. I'm using Netbeans 7.0 and SQLite, and found the JDBC driver from http://www.xerial.org/trac/Xerial/wiki/SQLiteJDBC to work fine as long as I specify an absolute path in the JDBC URL:

jdbc:sqlite://Users.../lib/db/ithildin.db

这是使用绝对URL在Netbeans的服务"->数据库"选项卡中的外观:

Here's how it looks in Netbeans' Services -> Databases tab, using an absolute URL:

打开连接,然后表节点显示可用表.

opening the connection and then the tables node shows the available tables.

但是因为我打算将此应用程序分发给其他人,所以使用绝对路径是行不通的.根据我在这里和那里已经阅读的内容,似乎还可以指定SQLite数据库文件的相对路径,但是无论如何,我都无法使其正常工作.

But because I intend to distribute this application to other people, using an absolute path is not going to work. From what I have read here and there it seems to be possible to also specify a relative path to the SQLite database file, but I cannot get it to work whatever I try.

奇怪的是,当我在Netbeans中创建连接时,最初将sqlite jdbc驱动程序jar和sqlite数据库文件都放在/lib(在Netbeans项目目录中)的/db子目录中似乎可以正常工作:

The strange thing is that when I create a connection in Netbeans it initially seems to work when I put both the sqlite jdbc driver jar and the sqlite database file in a /db subdirectory in /lib (in the Netbeans project directory):

当我打开新连接时,它会保持这种错觉-

and when I open the new connection it maintains that illusion -

但是当我打开表"节点时,结果却是空的:

but when I open the "Tables" node it turns out to be empty:

Similary,此连接似乎有效,但是一旦我尝试使用它,例如在从数据库生成实体类"中,它告诉我该数据库中没有任何表.

Similary, this connection seems valid but as soon as I try to use it - for instance in "Generating Entity Classes From Database" it tells me that there aren't any tables in that database.

有人知道解决办法吗?我会很感激.

Does anyone know a way out of this? I'd much appreciate it.

Lúthien

推荐答案

我已经按照与您相同的步骤进行操作,我的发现表明,新建连接向导"中的相对路径相对于NetBeans的工作目录. NetBeans安装到的目录.

I've followed the same steps that you have and my findings suggest that the relative path in the New Connection Wizard is relative to NetBeans' working directory which is the directory that NetBeans is installed to.

例如,在您的连接字符串中,您正在连接"到位于<NetBeans-Install-Dir>/ithildin-13-11-11.db的数据库.由于此数据库不存在,因此SQLite会为您创建数据库,这会给您带来幻觉".在测试中,我尝试使用相对路径连接的数据库是在NetBeans安装目录中创建的.

For example, in your connection string you are "connecting" to a database that resides at <NetBeans-Install-Dir>/ithildin-13-11-11.db. Since this database does not exist, SQLite is creating it for you which is giving you the "illusion" that you are seeing. In my tests, the database that I was trying to connect to with a relative path was created in the NetBeans install directory.

换句话说,一切都按设计工作.

In other words, everything is working as designed.

在代码中,您可以使用相对路径,并且应该相对于项目目录的顶部.在NetBeans的数据库服务中,您将需要使用绝对路径连接到(在您的项目中)存在的数据库,或者您需要提供从NetBeans的安装目录开始的相对路径.

In your code you can use a relative path and it should be relative to the top of your project's directory. In NetBeans' Databases Service you will need to either use an absolute path to connect to the database that exists (in your project) or you will need to provide a relative path that begins at NetBeans' install directory.

我的建议是在代码中使用相对路径,在新建连接向导"中使用绝对路径.

My suggestion is that you use a relative path in your code and an absolute path in the New Connection Wizard.

这篇关于如何指定相对的JDBC SQLite网址(路径?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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