如何在相对于应用程序文件夹的persistence.xml中指定一个jdbc.url? [英] How to specify a jdbc.url in the persistence.xml relative to the application folder?
问题描述
一旦我使用JPA部署我的应用程序,用户选择将其安装在某个地方。然后,属性设置为:
< property name =javax.persistence.jdbc.urlvalue =jdbc:德比:分贝;创建=真/>
被解释为以下异常:
无法在\db中创建数据库。
在整个开发过程中,它曾经是项目文件夹的相对路径,而不是现在的根目录。
如何使路径保持相对于安装应用程序的文件夹?
或者更糟糕的是userdir。
你应该在某处写安装位置,并设置 derby.system.home
系统属性到创建连接之前的这个位置。引用在桌面应用程序中使用Java DB 文章:
连接到Java DB数据库
...
所有连接URL都有以下
格式:jdbc:derby: dbName> [propertyList]
dbName
的URL
标识一个特定的数据库。 A
数据库可以在许多
位置之一:在当前工作的
目录中,在类路径上,在JAR
文件中,在特定的Java DB数据库
主目录,或在文件系统的绝对
位置。
在嵌入式环境中管理数据库
位置的最简单方法是
设置derby.system.home
系统
属性。该属性告诉Java DB
所有
数据库的默认归属位置。通过设置此属性,
地址簿演示确保
Java DB始终找到正确的
应用程序数据库。应用程序
数据库名为DefaultAddressBook
,
,它将存在于derby指示的目录
内。 system.home
属性。
数据库的连接URL如下所示:jdbc:derby:DefaultAddressBook
...
要连接到
DefaultAddressBook
数据库,演示必须首先设置
derby.system.home
系统属性。
演示使用用户家庭
目录的.addressbook
子目录。使用System class
找出用户的主目录。
然后再次使用该类设置
derby.system.home
属性:private void setDBSystemDir(){
$ / pre>
//决定数据库系统目录:< userhome> /。addressbook /
String userHomeDir = System.getProperty(user .home,。);
String systemDir = userHomeDir +/.addressbook;
//设置数据库系统目录。
System.setProperty(derby.system.home,systemDir);
}
Once I deploy my application with JPA the user chooses to install it somewhere. Then however the property set as:
<property name="javax.persistence.jdbc.url" value="jdbc:derby:db;create=true"/>
gets interpreted into the following exception: couldn't create database in \db. Throughout development it used to be the relative path to the project folder, and not the root as it's now. What should I do to make the path remain relative to the folder in which the application is installed? Or at the very worse, the userdir.
You should write the install location somewhere and set the derby.system.home
system property to this location before creating the connection. Quoting the Using Java DB in Desktop Applications article:
Connecting to the Java DB Database
...
All connection URLs have the following form:
jdbc:derby:<dbName>[propertyList]
The
dbName
portion of the URL identifies a specific database. A database can be in one of many locations: in the current working directory, on the classpath, in a JAR file, in a specific Java DB database home directory, or in an absolute location on your file system. The easiest way to manage your database location in an embedded environment is to set thederby.system.home
system property. This property tells Java DB the default home location of all databases. By setting this property, the Address Book demo ensures that Java DB always finds the correct application database. The application database is namedDefaultAddressBook
, and it will exist within the directory indicated by thederby.system.home
property. The connection URL for this database would look like this:jdbc:derby:DefaultAddressBook
...
To connect to the
DefaultAddressBook
database, the demo must first set thederby.system.home
system property. The demo uses the.addressbook
subdirectory of the user's home directory. Use the System class to find out the user's home directory. Then use the class again to set thederby.system.home
property:private void setDBSystemDir() { // Decide on the db system directory: <userhome>/.addressbook/ String userHomeDir = System.getProperty("user.home", "."); String systemDir = userHomeDir + "/.addressbook"; // Set the db system directory. System.setProperty("derby.system.home", systemDir); }
这篇关于如何在相对于应用程序文件夹的persistence.xml中指定一个jdbc.url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!