How can I specify a RELATIVE JDBC SQLite url (path?)

只谈情不闲聊 提交于 2019-12-24 00:44:53

问题


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

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

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.

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.

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, 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


回答1:


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.

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.

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.



来源:https://stackoverflow.com/questions/8202841/how-can-i-specify-a-relative-jdbc-sqlite-url-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!